1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "net/url_request/url_request_job.h" 6 7 #include "net/http/http_transaction_unittest.h" 8 #include "net/url_request/url_request_test_util.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 namespace { 12 13 // This is a header that signals the end of the data. 14 const char kGzipGata[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; 15 16 void GZipServer(const net::HttpRequestInfo* request, 17 std::string* response_status, std::string* response_headers, 18 std::string* response_data) { 19 response_data->assign(kGzipGata, sizeof(kGzipGata)); 20 } 21 22 const MockTransaction kGZip_Transaction = { 23 "http://www.google.com/gzyp", 24 "GET", 25 base::Time(), 26 "", 27 net::LOAD_NORMAL, 28 "HTTP/1.1 200 OK", 29 "Cache-Control: max-age=10000\n" 30 "Content-Encoding: gzip\n" 31 "Content-Length: 30\n", // Intentionally wrong. 32 base::Time(), 33 "", 34 TEST_MODE_NORMAL, 35 &GZipServer, 36 0, 37 net::OK 38 }; 39 40 } // namespace 41 42 TEST(URLRequestJob, TransactionNotifiedWhenDone) { 43 MockNetworkLayer network_layer; 44 net::TestURLRequestContext context; 45 context.set_http_transaction_factory(&network_layer); 46 47 net::TestDelegate d; 48 net::TestURLRequest req(GURL(kGZip_Transaction.url), &d, &context, NULL); 49 AddMockTransaction(&kGZip_Transaction); 50 51 req.set_method("GET"); 52 req.Start(); 53 54 base::MessageLoop::current()->Run(); 55 56 EXPECT_TRUE(network_layer.done_reading_called()); 57 58 RemoveMockTransaction(&kGZip_Transaction); 59 } 60 61 TEST(URLRequestJob, SyncTransactionNotifiedWhenDone) { 62 MockNetworkLayer network_layer; 63 net::TestURLRequestContext context; 64 context.set_http_transaction_factory(&network_layer); 65 66 net::TestDelegate d; 67 net::TestURLRequest req(GURL(kGZip_Transaction.url), &d, &context, NULL); 68 MockTransaction transaction(kGZip_Transaction); 69 transaction.test_mode = TEST_MODE_SYNC_ALL; 70 AddMockTransaction(&transaction); 71 72 req.set_method("GET"); 73 req.Start(); 74 75 base::MessageLoop::current()->Run(); 76 77 EXPECT_TRUE(network_layer.done_reading_called()); 78 79 RemoveMockTransaction(&transaction); 80 } 81