url_request_job_unittest.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 11namespace { 12 13// This is a header that signals the end of the data. 14const char kGzipGata[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; 15 16void 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 22const 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 40const MockTransaction kRedirect_Transaction = { 41 "http://www.google.com/redirect", 42 "GET", 43 base::Time(), 44 "", 45 net::LOAD_NORMAL, 46 "HTTP/1.1 302 Found", 47 "Cache-Control: max-age=10000\n" 48 "Location: http://www.google.com/destination\n" 49 "Content-Length: 5\n", 50 base::Time(), 51 "hello", 52 TEST_MODE_NORMAL, 53 NULL, 54 0, 55 net::OK 56}; 57 58} // namespace 59 60TEST(URLRequestJob, TransactionNotifiedWhenDone) { 61 MockNetworkLayer network_layer; 62 net::TestURLRequestContext context; 63 context.set_http_transaction_factory(&network_layer); 64 65 net::TestDelegate d; 66 net::TestURLRequest req(GURL(kGZip_Transaction.url), &d, &context, NULL); 67 AddMockTransaction(&kGZip_Transaction); 68 69 req.set_method("GET"); 70 req.Start(); 71 72 base::MessageLoop::current()->Run(); 73 74 EXPECT_TRUE(network_layer.done_reading_called()); 75 76 RemoveMockTransaction(&kGZip_Transaction); 77} 78 79TEST(URLRequestJob, SyncTransactionNotifiedWhenDone) { 80 MockNetworkLayer network_layer; 81 net::TestURLRequestContext context; 82 context.set_http_transaction_factory(&network_layer); 83 84 net::TestDelegate d; 85 net::TestURLRequest req(GURL(kGZip_Transaction.url), &d, &context, NULL); 86 MockTransaction transaction(kGZip_Transaction); 87 transaction.test_mode = TEST_MODE_SYNC_ALL; 88 AddMockTransaction(&transaction); 89 90 req.set_method("GET"); 91 req.Start(); 92 93 base::MessageLoop::current()->Run(); 94 95 EXPECT_TRUE(network_layer.done_reading_called()); 96 97 RemoveMockTransaction(&transaction); 98} 99 100TEST(URLRequestJob, RedirectTransactionNotifiedWhenDone) { 101 MockNetworkLayer network_layer; 102 net::TestURLRequestContext context; 103 context.set_http_transaction_factory(&network_layer); 104 105 net::TestDelegate d; 106 net::TestURLRequest req(GURL(kRedirect_Transaction.url), &d, &context, NULL); 107 AddMockTransaction(&kRedirect_Transaction); 108 109 req.set_method("GET"); 110 req.Start(); 111 112 base::MessageLoop::current()->Run(); 113 114 EXPECT_TRUE(network_layer.done_reading_called()); 115 116 RemoveMockTransaction(&kRedirect_Transaction); 117} 118