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 "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "net/base/request_priority.h" | 9 #include "net/base/request_priority.h" |
10 #include "net/http/http_transaction_test_util.h" | 10 #include "net/http/http_transaction_test_util.h" |
11 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
12 #include "net/url_request/url_request_test_util.h" | 12 #include "net/url_request/url_request_test_util.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // This is a header that signals the end of the data. | 19 // This is a header that signals the end of the data. |
20 const char kGzipData[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; | 20 const char kGzipData[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0"; |
21 const char kGzipDataWithName[] = | 21 const char kGzipDataWithName[] = |
22 "\x1f\x08b\x08\x08\0\0\0\0\0\0name\0\3\0\0\0\0\0\0\0\0"; | 22 "\x1f\x08b\x08\x08\0\0\0\0\0\0name\0\3\0\0\0\0\0\0\0\0"; |
23 // Gzip data that contains the word hello with a newline character. | 23 // Gzip data that contains the word hello with a newline character. |
24 const char kGzipHelloData[] = | 24 const char kGzipHelloData[] = |
25 "\x1f\x8b\x08\x08\x46\x7d\x4e\x56\x00\x03\x67\x7a\x69\x70\x2e\x74\x78\x74" | 25 "\x1f\x8b\x08\x08\x46\x7d\x4e\x56\x00\x03\x67\x7a\x69\x70\x2e\x74\x78\x74" |
26 "\x00\xcb\x48\xcd\xc9\xc9\xe7\x02\x00\x20\x30\x3a\x36\x06\x00\x00\x00"; | 26 "\x00\xcb\x48\xcd\xc9\xc9\xe7\x02\x00\x20\x30\x3a\x36\x06\x00\x00\x00"; |
| 27 const char kBrotliHelloData[] = |
| 28 "\033\015\0\0\244\024\102\152\020\111\152\072\235\126\034"; |
27 | 29 |
28 void GZipServer(const HttpRequestInfo* request, | 30 void GZipServer(const HttpRequestInfo* request, |
29 std::string* response_status, | 31 std::string* response_status, |
30 std::string* response_headers, | 32 std::string* response_headers, |
31 std::string* response_data) { | 33 std::string* response_data) { |
32 response_data->assign(kGzipData, sizeof(kGzipData)); | 34 response_data->assign(kGzipData, sizeof(kGzipData)); |
33 } | 35 } |
34 | 36 |
35 void GZipHelloServer(const HttpRequestInfo* request, | 37 void GZipHelloServer(const HttpRequestInfo* request, |
36 std::string* response_status, | 38 std::string* response_status, |
37 std::string* response_headers, | 39 std::string* response_headers, |
38 std::string* response_data) { | 40 std::string* response_data) { |
39 response_data->assign(kGzipHelloData, sizeof(kGzipHelloData)); | 41 response_data->assign(kGzipHelloData, sizeof(kGzipHelloData)); |
40 } | 42 } |
41 | 43 |
42 void BigGZipServer(const HttpRequestInfo* request, | 44 void BigGZipServer(const HttpRequestInfo* request, |
43 std::string* response_status, | 45 std::string* response_status, |
44 std::string* response_headers, | 46 std::string* response_headers, |
45 std::string* response_data) { | 47 std::string* response_data) { |
46 response_data->assign(kGzipDataWithName, sizeof(kGzipDataWithName)); | 48 response_data->assign(kGzipDataWithName, sizeof(kGzipDataWithName)); |
47 response_data->insert(10, 64 * 1024, 'a'); | 49 response_data->insert(10, 64 * 1024, 'a'); |
48 } | 50 } |
49 | 51 |
| 52 void BrotliHelloServer(const HttpRequestInfo* request, |
| 53 std::string* response_status, |
| 54 std::string* response_headers, |
| 55 std::string* response_data) { |
| 56 response_data->assign(kBrotliHelloData, sizeof(kBrotliHelloData) - 1); |
| 57 } |
| 58 |
50 const MockTransaction kGZip_Transaction = { | 59 const MockTransaction kGZip_Transaction = { |
51 "http://www.google.com/gzyp", | 60 "http://www.google.com/gzyp", |
52 "GET", | 61 "GET", |
53 base::Time(), | 62 base::Time(), |
54 "", | 63 "", |
55 LOAD_NORMAL, | 64 LOAD_NORMAL, |
56 "HTTP/1.1 200 OK", | 65 "HTTP/1.1 200 OK", |
57 "Cache-Control: max-age=10000\n" | 66 "Cache-Control: max-age=10000\n" |
58 "Content-Encoding: gzip\n" | 67 "Content-Encoding: gzip\n" |
59 "Content-Length: 30\n", // Intentionally wrong. | 68 "Content-Length: 30\n", // Intentionally wrong. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 base::Time(), | 115 base::Time(), |
107 "", | 116 "", |
108 TEST_MODE_NORMAL, | 117 TEST_MODE_NORMAL, |
109 nullptr, | 118 nullptr, |
110 nullptr, | 119 nullptr, |
111 0, | 120 0, |
112 0, | 121 0, |
113 OK, | 122 OK, |
114 }; | 123 }; |
115 | 124 |
| 125 const MockTransaction kBrotli_Slow_Transaction = { |
| 126 "http://www.google.com/brotli", "GET", base::Time(), "", LOAD_NORMAL, |
| 127 "HTTP/1.1 200 OK", |
| 128 "Cache-Control: max-age=10000\n" |
| 129 "Content-Encoding: br\n", |
| 130 base::Time(), "", TEST_MODE_SLOW_READ, &BrotliHelloServer, nullptr, 0, 0, |
| 131 OK, |
| 132 }; |
| 133 |
116 } // namespace | 134 } // namespace |
117 | 135 |
118 TEST(URLRequestJob, TransactionNotifiedWhenDone) { | 136 TEST(URLRequestJob, TransactionNotifiedWhenDone) { |
119 MockNetworkLayer network_layer; | 137 MockNetworkLayer network_layer; |
120 TestURLRequestContext context; | 138 TestURLRequestContext context; |
121 context.set_http_transaction_factory(&network_layer); | 139 context.set_http_transaction_factory(&network_layer); |
122 | 140 |
123 TestDelegate d; | 141 TestDelegate d; |
124 scoped_ptr<URLRequest> req( | 142 scoped_ptr<URLRequest> req( |
125 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d)); | 143 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d)); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 base::MessageLoop::current()->Run(); | 285 base::MessageLoop::current()->Run(); |
268 | 286 |
269 EXPECT_FALSE(d.request_failed()); | 287 EXPECT_FALSE(d.request_failed()); |
270 EXPECT_EQ(200, req->GetResponseCode()); | 288 EXPECT_EQ(200, req->GetResponseCode()); |
271 EXPECT_EQ("hello\n", d.data_received()); | 289 EXPECT_EQ("hello\n", d.data_received()); |
272 EXPECT_TRUE(network_layer.done_reading_called()); | 290 EXPECT_TRUE(network_layer.done_reading_called()); |
273 | 291 |
274 RemoveMockTransaction(&kGzip_Slow_Transaction); | 292 RemoveMockTransaction(&kGzip_Slow_Transaction); |
275 } | 293 } |
276 | 294 |
| 295 TEST(URLRequestJob, SlowBrotliRead) { |
| 296 MockNetworkLayer network_layer; |
| 297 TestURLRequestContext context; |
| 298 context.set_http_transaction_factory(&network_layer); |
| 299 |
| 300 TestDelegate d; |
| 301 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 302 GURL(kBrotli_Slow_Transaction.url), DEFAULT_PRIORITY, &d)); |
| 303 AddMockTransaction(&kBrotli_Slow_Transaction); |
| 304 |
| 305 req->set_method("GET"); |
| 306 req->Start(); |
| 307 |
| 308 base::MessageLoop::current()->Run(); |
| 309 |
| 310 EXPECT_FALSE(d.request_failed()); |
| 311 EXPECT_EQ(200, req->GetResponseCode()); |
| 312 |
| 313 #if !defined(DISABLE_BROTLI_SUPPORT) |
| 314 EXPECT_EQ("hello, world!\n", d.data_received()); |
| 315 #endif |
| 316 |
| 317 EXPECT_TRUE(network_layer.done_reading_called()); |
| 318 |
| 319 RemoveMockTransaction(&kBrotli_Slow_Transaction); |
| 320 } |
| 321 |
277 } // namespace net | 322 } // namespace net |
OLD | NEW |