Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 7 | #include "base/run_loop.h" |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 8 | #include "net/base/request_priority.h" |
Torne (Richard Coles) | cedac22 | 2014-06-03 10:58:34 +0100 | [diff] [blame] | 9 | #include "net/http/http_transaction_test_util.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | #include "net/url_request/url_request_test_util.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 13 | namespace net { |
| 14 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | // This is a header that signals the end of the data. |
Torne (Richard Coles) | 010d83a | 2014-05-14 12:12:37 +0100 | [diff] [blame] | 18 | const char kGzipData[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; |
| 19 | const char kGzipDataWithName[] = |
| 20 | "\x1f\x08b\x08\x08\0\0\0\0\0\0name\0\3\0\0\0\0\0\0\0\0"; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 21 | |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 22 | void GZipServer(const HttpRequestInfo* request, |
| 23 | std::string* response_status, |
| 24 | std::string* response_headers, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 25 | std::string* response_data) { |
Torne (Richard Coles) | 010d83a | 2014-05-14 12:12:37 +0100 | [diff] [blame] | 26 | response_data->assign(kGzipData, sizeof(kGzipData)); |
| 27 | } |
| 28 | |
| 29 | void BigGZipServer(const HttpRequestInfo* request, |
| 30 | std::string* response_status, |
| 31 | std::string* response_headers, |
| 32 | std::string* response_data) { |
| 33 | response_data->assign(kGzipDataWithName, sizeof(kGzipDataWithName)); |
| 34 | response_data->insert(10, 64 * 1024, 'a'); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | const MockTransaction kGZip_Transaction = { |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 38 | "http://www.google.com/gzyp", |
| 39 | "GET", |
| 40 | base::Time(), |
| 41 | "", |
| 42 | LOAD_NORMAL, |
| 43 | "HTTP/1.1 200 OK", |
| 44 | "Cache-Control: max-age=10000\n" |
| 45 | "Content-Encoding: gzip\n" |
| 46 | "Content-Length: 30\n", // Intentionally wrong. |
| 47 | base::Time(), |
| 48 | "", |
| 49 | TEST_MODE_NORMAL, |
| 50 | &GZipServer, |
| 51 | 0, |
| 52 | OK |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 55 | const MockTransaction kRedirect_Transaction = { |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 56 | "http://www.google.com/redirect", |
| 57 | "GET", |
| 58 | base::Time(), |
| 59 | "", |
| 60 | LOAD_NORMAL, |
| 61 | "HTTP/1.1 302 Found", |
| 62 | "Cache-Control: max-age=10000\n" |
| 63 | "Location: http://www.google.com/destination\n" |
| 64 | "Content-Length: 5\n", |
| 65 | base::Time(), |
| 66 | "hello", |
| 67 | TEST_MODE_NORMAL, |
| 68 | NULL, |
| 69 | 0, |
| 70 | OK |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 73 | } // namespace |
| 74 | |
| 75 | TEST(URLRequestJob, TransactionNotifiedWhenDone) { |
| 76 | MockNetworkLayer network_layer; |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 77 | TestURLRequestContext context; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 78 | context.set_http_transaction_factory(&network_layer); |
| 79 | |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 80 | TestDelegate d; |
| 81 | TestURLRequest req( |
| 82 | GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 83 | AddMockTransaction(&kGZip_Transaction); |
| 84 | |
| 85 | req.set_method("GET"); |
| 86 | req.Start(); |
| 87 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 88 | base::MessageLoop::current()->Run(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 89 | |
| 90 | EXPECT_TRUE(network_layer.done_reading_called()); |
| 91 | |
| 92 | RemoveMockTransaction(&kGZip_Transaction); |
| 93 | } |
| 94 | |
| 95 | TEST(URLRequestJob, SyncTransactionNotifiedWhenDone) { |
| 96 | MockNetworkLayer network_layer; |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 97 | TestURLRequestContext context; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 98 | context.set_http_transaction_factory(&network_layer); |
| 99 | |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 100 | TestDelegate d; |
| 101 | TestURLRequest req( |
| 102 | GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 103 | MockTransaction transaction(kGZip_Transaction); |
| 104 | transaction.test_mode = TEST_MODE_SYNC_ALL; |
| 105 | AddMockTransaction(&transaction); |
| 106 | |
| 107 | req.set_method("GET"); |
| 108 | req.Start(); |
| 109 | |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 110 | base::RunLoop().Run(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 111 | |
| 112 | EXPECT_TRUE(network_layer.done_reading_called()); |
| 113 | |
| 114 | RemoveMockTransaction(&transaction); |
| 115 | } |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 116 | |
Torne (Richard Coles) | 010d83a | 2014-05-14 12:12:37 +0100 | [diff] [blame] | 117 | // Tests processing a large gzip header one byte at a time. |
| 118 | TEST(URLRequestJob, SyncSlowTransaction) { |
| 119 | MockNetworkLayer network_layer; |
| 120 | TestURLRequestContext context; |
| 121 | context.set_http_transaction_factory(&network_layer); |
| 122 | |
| 123 | TestDelegate d; |
| 124 | TestURLRequest req( |
| 125 | GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context); |
| 126 | MockTransaction transaction(kGZip_Transaction); |
| 127 | transaction.test_mode = TEST_MODE_SYNC_ALL | TEST_MODE_SLOW_READ; |
| 128 | transaction.handler = &BigGZipServer; |
| 129 | AddMockTransaction(&transaction); |
| 130 | |
| 131 | req.set_method("GET"); |
| 132 | req.Start(); |
| 133 | |
| 134 | base::RunLoop().Run(); |
| 135 | |
| 136 | EXPECT_TRUE(network_layer.done_reading_called()); |
| 137 | |
| 138 | RemoveMockTransaction(&transaction); |
| 139 | } |
| 140 | |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 141 | TEST(URLRequestJob, RedirectTransactionNotifiedWhenDone) { |
| 142 | MockNetworkLayer network_layer; |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 143 | TestURLRequestContext context; |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 144 | context.set_http_transaction_factory(&network_layer); |
| 145 | |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 146 | TestDelegate d; |
| 147 | TestURLRequest req( |
| 148 | GURL(kRedirect_Transaction.url), DEFAULT_PRIORITY, &d, &context); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 149 | AddMockTransaction(&kRedirect_Transaction); |
| 150 | |
| 151 | req.set_method("GET"); |
| 152 | req.Start(); |
| 153 | |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 154 | base::RunLoop().Run(); |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 155 | |
| 156 | EXPECT_TRUE(network_layer.done_reading_called()); |
| 157 | |
| 158 | RemoveMockTransaction(&kRedirect_Transaction); |
| 159 | } |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 160 | |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 161 | TEST(URLRequestJob, TransactionNotCachedWhenNetworkDelegateRedirects) { |
| 162 | MockNetworkLayer network_layer; |
| 163 | TestNetworkDelegate network_delegate; |
| 164 | network_delegate.set_redirect_on_headers_received_url(GURL("http://foo")); |
| 165 | TestURLRequestContext context; |
| 166 | context.set_http_transaction_factory(&network_layer); |
| 167 | context.set_network_delegate(&network_delegate); |
| 168 | |
| 169 | TestDelegate d; |
| 170 | TestURLRequest req(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, |
| 171 | &context); |
| 172 | AddMockTransaction(&kGZip_Transaction); |
| 173 | |
| 174 | req.set_method("GET"); |
| 175 | req.Start(); |
| 176 | |
| 177 | base::RunLoop().Run(); |
| 178 | |
| 179 | EXPECT_TRUE(network_layer.stop_caching_called()); |
| 180 | |
| 181 | RemoveMockTransaction(&kGZip_Transaction); |
| 182 | } |
| 183 | |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 184 | } // namespace net |