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