| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_request_job.h" | 5 #include "net/url_request/url_request_job.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "net/base/request_priority.h" | 10 #include "net/base/request_priority.h" |
| 11 #include "net/http/http_transaction_test_util.h" | 11 #include "net/http/http_transaction_test_util.h" |
| 12 #include "net/test/cert_test_util.h" | 12 #include "net/test/cert_test_util.h" |
| 13 #include "net/test/gtest_util.h" | 13 #include "net/test/gtest_util.h" |
| 14 #include "net/test/test_data_directory.h" | 14 #include "net/test/test_data_directory.h" |
| 15 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
| 16 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 using net::test::IsError; | 20 using net::test::IsError; |
| 21 using net::test::IsOk; | 21 using net::test::IsOk; |
| 22 | 22 |
| 23 namespace net { | 23 namespace net { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // Data encoded in kBrotliHelloData. | 27 // Data encoded in kBrotliHelloData. |
| 28 const char kBrotliDecodedHelloData[] = "hello, world!\n"; | 28 const char kHelloData[] = "hello, world!\n"; |
| 29 // kBrotliDecodedHelloData encoded with brotli. | 29 // kHelloData encoded with brotli. |
| 30 const char kBrotliHelloData[] = | 30 const char kBrotliHelloData[] = |
| 31 "\033\015\0\0\244\024\102\152\020\111\152\072\235\126\034"; | 31 "\033\015\0\0\244\024\102\152\020\111\152\072\235\126\034"; |
| 32 | 32 |
| 33 // This is a header that signals the end of the data. | 33 // This is a header that signals the end of the data. |
| 34 const char kGzipData[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; | 34 const char kGzipData[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; |
| 35 const char kGzipDataWithName[] = | 35 const char kGzipDataWithName[] = |
| 36 "\x1f\x08b\x08\x08\0\0\0\0\0\0name\0\3\0\0\0\0\0\0\0\0"; | 36 "\x1f\x08b\x08\x08\0\0\0\0\0\0name\0\3\0\0\0\0\0\0\0\0"; |
| 37 // Gzip data that contains the word hello with a newline character. | 37 // kHelloData encoded with gzip. |
| 38 const char kGzipHelloData[] = | 38 const char kGzipHelloData[] = |
| 39 "\x1f\x8b\x08\x08\x46\x7d\x4e\x56\x00\x03\x67\x7a\x69\x70\x2e\x74\x78\x74" | 39 "\x1f\x8b\x08\x08\x46\x7d\x4e\x56\x00\x03\x67\x7a\x69\x70\x2e\x74\x78\x74" |
| 40 "\x00\xcb\x48\xcd\xc9\xc9\xe7\x02\x00\x20\x30\x3a\x36\x06\x00\x00\x00"; | 40 "\x00\xcb\x48\xcd\xc9\xc9\xe7\x02\x00\x20\x30\x3a\x36\x06\x00\x00\x00"; |
| 41 | 41 |
| 42 void GZipServer(const HttpRequestInfo* request, | 42 void GZipServer(const HttpRequestInfo* request, |
| 43 std::string* response_status, | 43 std::string* response_status, |
| 44 std::string* response_headers, | 44 std::string* response_headers, |
| 45 std::string* response_data) { | 45 std::string* response_data) { |
| 46 response_data->assign(kGzipData, sizeof(kGzipData)); | 46 response_data->assign(kGzipData, sizeof(kGzipData)); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void GZipHelloServer(const HttpRequestInfo* request, | 49 void GZipHelloServer(const HttpRequestInfo* request, |
| 50 std::string* response_status, | 50 std::string* response_status, |
| 51 std::string* response_headers, | 51 std::string* response_headers, |
| 52 std::string* response_data) { | 52 std::string* response_data) { |
| 53 response_data->assign(kGzipHelloData, sizeof(kGzipHelloData)); | 53 response_data->assign(kGzipHelloData, sizeof(kGzipHelloData) - 1); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void BigGZipServer(const HttpRequestInfo* request, | 56 void BigGZipServer(const HttpRequestInfo* request, |
| 57 std::string* response_status, | 57 std::string* response_status, |
| 58 std::string* response_headers, | 58 std::string* response_headers, |
| 59 std::string* response_data) { | 59 std::string* response_data) { |
| 60 response_data->assign(kGzipDataWithName, sizeof(kGzipDataWithName)); | 60 response_data->assign(kGzipDataWithName, sizeof(kGzipDataWithName)); |
| 61 response_data->insert(10, 64 * 1024, 'a'); | 61 response_data->insert(10, 64 * 1024, 'a'); |
| 62 } | 62 } |
| 63 | 63 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 88 transaction->cert = | 88 transaction->cert = |
| 89 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); | 89 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem"); |
| 90 } else { | 90 } else { |
| 91 transaction->cert = nullptr; | 91 transaction->cert = nullptr; |
| 92 } | 92 } |
| 93 transaction->cert_status = 0; | 93 transaction->cert_status = 0; |
| 94 transaction->ssl_connection_status = 0; | 94 transaction->ssl_connection_status = 0; |
| 95 transaction->return_code = OK; | 95 transaction->return_code = OK; |
| 96 } | 96 } |
| 97 | 97 |
| 98 const MockTransaction kNoFilter_Transaction = { |
| 99 "http://www.google.com/gzyp", "GET", base::Time(), "", LOAD_NORMAL, |
| 100 "HTTP/1.1 200 OK", |
| 101 "Cache-Control: max-age=10000\n" |
| 102 "Content-Length: 30\n", // Intentionally wrong. |
| 103 base::Time(), |
| 104 "hello", TEST_MODE_NORMAL, nullptr, nullptr, 0, 0, OK, |
| 105 }; |
| 106 |
| 98 const MockTransaction kGZip_Transaction = { | 107 const MockTransaction kGZip_Transaction = { |
| 99 "http://www.google.com/gzyp", "GET", base::Time(), "", LOAD_NORMAL, | 108 "http://www.google.com/gzyp", "GET", base::Time(), "", LOAD_NORMAL, |
| 100 "HTTP/1.1 200 OK", | 109 "HTTP/1.1 200 OK", |
| 101 "Cache-Control: max-age=10000\n" | 110 "Cache-Control: max-age=10000\n" |
| 102 "Content-Encoding: gzip\n" | 111 "Content-Encoding: gzip\n" |
| 103 "Content-Length: 30\n", // Intentionally wrong. | 112 "Content-Length: 30\n", // Intentionally wrong. |
| 104 base::Time(), | 113 base::Time(), |
| 105 "", TEST_MODE_NORMAL, &GZipServer, nullptr, nullptr, 0, 0, OK, | 114 "", TEST_MODE_NORMAL, &GZipServer, nullptr, nullptr, 0, 0, OK, |
| 106 }; | 115 }; |
| 107 | 116 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 "http://www.google.com/brotli", "GET", base::Time(), "", LOAD_NORMAL, | 165 "http://www.google.com/brotli", "GET", base::Time(), "", LOAD_NORMAL, |
| 157 "HTTP/1.1 200 OK", | 166 "HTTP/1.1 200 OK", |
| 158 "Cache-Control: max-age=10000\n" | 167 "Cache-Control: max-age=10000\n" |
| 159 "Content-Encoding: br\n", | 168 "Content-Encoding: br\n", |
| 160 base::Time(), "", TEST_MODE_SLOW_READ, &BrotliHelloServer, nullptr, nullptr, | 169 base::Time(), "", TEST_MODE_SLOW_READ, &BrotliHelloServer, nullptr, nullptr, |
| 161 0, 0, OK, | 170 0, 0, OK, |
| 162 }; | 171 }; |
| 163 | 172 |
| 164 } // namespace | 173 } // namespace |
| 165 | 174 |
| 175 TEST(URLRequestJob, TransactionNoFilter) { |
| 176 MockNetworkLayer network_layer; |
| 177 TestURLRequestContext context; |
| 178 context.set_http_transaction_factory(&network_layer); |
| 179 |
| 180 TestDelegate d; |
| 181 std::unique_ptr<URLRequest> req(context.CreateRequest( |
| 182 GURL(kNoFilter_Transaction.url), DEFAULT_PRIORITY, &d)); |
| 183 AddMockTransaction(&kNoFilter_Transaction); |
| 184 |
| 185 req->set_method("GET"); |
| 186 req->Start(); |
| 187 |
| 188 base::RunLoop().Run(); |
| 189 |
| 190 EXPECT_FALSE(d.request_failed()); |
| 191 EXPECT_EQ(200, req->GetResponseCode()); |
| 192 EXPECT_EQ("hello", d.data_received()); |
| 193 EXPECT_TRUE(network_layer.done_reading_called()); |
| 194 |
| 195 RemoveMockTransaction(&kNoFilter_Transaction); |
| 196 } |
| 197 |
| 166 TEST(URLRequestJob, TransactionNotifiedWhenDone) { | 198 TEST(URLRequestJob, TransactionNotifiedWhenDone) { |
| 167 MockNetworkLayer network_layer; | 199 MockNetworkLayer network_layer; |
| 168 TestURLRequestContext context; | 200 TestURLRequestContext context; |
| 169 context.set_http_transaction_factory(&network_layer); | 201 context.set_http_transaction_factory(&network_layer); |
| 170 | 202 |
| 171 TestDelegate d; | 203 TestDelegate d; |
| 172 std::unique_ptr<URLRequest> req( | 204 std::unique_ptr<URLRequest> req( |
| 173 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d)); | 205 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d)); |
| 174 AddMockTransaction(&kGZip_Transaction); | 206 AddMockTransaction(&kGZip_Transaction); |
| 175 | 207 |
| 176 req->set_method("GET"); | 208 req->set_method("GET"); |
| 177 req->Start(); | 209 req->Start(); |
| 178 | 210 |
| 179 base::RunLoop().Run(); | 211 base::RunLoop().Run(); |
| 180 | 212 |
| 213 EXPECT_FALSE(d.request_failed()); |
| 214 EXPECT_EQ(200, req->GetResponseCode()); |
| 215 EXPECT_EQ("", d.data_received()); |
| 181 EXPECT_TRUE(network_layer.done_reading_called()); | 216 EXPECT_TRUE(network_layer.done_reading_called()); |
| 182 | 217 |
| 183 RemoveMockTransaction(&kGZip_Transaction); | 218 RemoveMockTransaction(&kGZip_Transaction); |
| 184 } | 219 } |
| 185 | 220 |
| 186 TEST(URLRequestJob, SyncTransactionNotifiedWhenDone) { | 221 TEST(URLRequestJob, SyncTransactionNotifiedWhenDone) { |
| 187 MockNetworkLayer network_layer; | 222 MockNetworkLayer network_layer; |
| 188 TestURLRequestContext context; | 223 TestURLRequestContext context; |
| 189 context.set_http_transaction_factory(&network_layer); | 224 context.set_http_transaction_factory(&network_layer); |
| 190 | 225 |
| 191 TestDelegate d; | 226 TestDelegate d; |
| 192 std::unique_ptr<URLRequest> req( | 227 std::unique_ptr<URLRequest> req( |
| 193 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d)); | 228 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d)); |
| 194 MockTransaction transaction(kGZip_Transaction); | 229 MockTransaction transaction(kGZip_Transaction); |
| 195 transaction.test_mode = TEST_MODE_SYNC_ALL; | 230 transaction.test_mode = TEST_MODE_SYNC_ALL; |
| 196 AddMockTransaction(&transaction); | 231 AddMockTransaction(&transaction); |
| 197 | 232 |
| 198 req->set_method("GET"); | 233 req->set_method("GET"); |
| 199 req->Start(); | 234 req->Start(); |
| 200 | 235 |
| 201 base::RunLoop().Run(); | 236 base::RunLoop().Run(); |
| 202 | 237 |
| 238 EXPECT_FALSE(d.request_failed()); |
| 239 EXPECT_EQ(200, req->GetResponseCode()); |
| 240 EXPECT_EQ("", d.data_received()); |
| 203 EXPECT_TRUE(network_layer.done_reading_called()); | 241 EXPECT_TRUE(network_layer.done_reading_called()); |
| 204 | 242 |
| 205 RemoveMockTransaction(&transaction); | 243 RemoveMockTransaction(&transaction); |
| 206 } | 244 } |
| 207 | 245 |
| 208 // Tests processing a large gzip header one byte at a time. | 246 // Tests processing a large gzip header one byte at a time. |
| 209 TEST(URLRequestJob, SyncSlowTransaction) { | 247 TEST(URLRequestJob, SyncSlowTransaction) { |
| 210 MockNetworkLayer network_layer; | 248 MockNetworkLayer network_layer; |
| 211 TestURLRequestContext context; | 249 TestURLRequestContext context; |
| 212 context.set_http_transaction_factory(&network_layer); | 250 context.set_http_transaction_factory(&network_layer); |
| 213 | 251 |
| 214 TestDelegate d; | 252 TestDelegate d; |
| 215 std::unique_ptr<URLRequest> req( | 253 std::unique_ptr<URLRequest> req( |
| 216 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d)); | 254 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d)); |
| 217 MockTransaction transaction(kGZip_Transaction); | 255 MockTransaction transaction(kGZip_Transaction); |
| 218 transaction.test_mode = TEST_MODE_SYNC_ALL | TEST_MODE_SLOW_READ; | 256 transaction.test_mode = TEST_MODE_SYNC_ALL | TEST_MODE_SLOW_READ; |
| 219 transaction.handler = &BigGZipServer; | 257 transaction.handler = &BigGZipServer; |
| 220 AddMockTransaction(&transaction); | 258 AddMockTransaction(&transaction); |
| 221 | 259 |
| 222 req->set_method("GET"); | 260 req->set_method("GET"); |
| 223 req->Start(); | 261 req->Start(); |
| 224 | 262 |
| 225 base::RunLoop().Run(); | 263 base::RunLoop().Run(); |
| 226 | 264 |
| 265 EXPECT_FALSE(d.request_failed()); |
| 266 EXPECT_EQ(200, req->GetResponseCode()); |
| 267 EXPECT_EQ("", d.data_received()); |
| 227 EXPECT_TRUE(network_layer.done_reading_called()); | 268 EXPECT_TRUE(network_layer.done_reading_called()); |
| 228 | 269 |
| 229 RemoveMockTransaction(&transaction); | 270 RemoveMockTransaction(&transaction); |
| 230 } | 271 } |
| 231 | 272 |
| 232 TEST(URLRequestJob, RedirectTransactionNotifiedWhenDone) { | 273 TEST(URLRequestJob, RedirectTransactionNotifiedWhenDone) { |
| 233 MockNetworkLayer network_layer; | 274 MockNetworkLayer network_layer; |
| 234 TestURLRequestContext context; | 275 TestURLRequestContext context; |
| 235 context.set_http_transaction_factory(&network_layer); | 276 context.set_http_transaction_factory(&network_layer); |
| 236 | 277 |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 GURL(kBrotli_Slow_Transaction.url), DEFAULT_PRIORITY, &d)); | 594 GURL(kBrotli_Slow_Transaction.url), DEFAULT_PRIORITY, &d)); |
| 554 AddMockTransaction(&kBrotli_Slow_Transaction); | 595 AddMockTransaction(&kBrotli_Slow_Transaction); |
| 555 | 596 |
| 556 req->set_method("GET"); | 597 req->set_method("GET"); |
| 557 req->Start(); | 598 req->Start(); |
| 558 | 599 |
| 559 base::RunLoop().RunUntilIdle(); | 600 base::RunLoop().RunUntilIdle(); |
| 560 | 601 |
| 561 EXPECT_FALSE(d.request_failed()); | 602 EXPECT_FALSE(d.request_failed()); |
| 562 EXPECT_EQ(200, req->GetResponseCode()); | 603 EXPECT_EQ(200, req->GetResponseCode()); |
| 563 EXPECT_EQ(kBrotliDecodedHelloData, d.data_received()); | 604 EXPECT_EQ(kHelloData, d.data_received()); |
| 564 EXPECT_TRUE(network_layer.done_reading_called()); | 605 EXPECT_TRUE(network_layer.done_reading_called()); |
| 565 | 606 |
| 566 RemoveMockTransaction(&kBrotli_Slow_Transaction); | 607 RemoveMockTransaction(&kBrotli_Slow_Transaction); |
| 567 } | 608 } |
| 568 | 609 |
| 569 } // namespace net | 610 } // namespace net |
| OLD | NEW |