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.
5 #include "net/url_request/url_request_job.h"
7 #include "base/run_loop.h"
8 #include "net/base/request_priority.h"
9 #include "net/http/http_transaction_test_util.h"
10 #include "net/url_request/url_request_test_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 // This is a header that signals the end of the data.
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";
22 void GZipServer(const HttpRequestInfo
* request
,
23 std::string
* response_status
,
24 std::string
* response_headers
,
25 std::string
* response_data
) {
26 response_data
->assign(kGzipData
, sizeof(kGzipData
));
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');
37 const MockTransaction kGZip_Transaction
= {
38 "http://www.google.com/gzyp",
44 "Cache-Control: max-age=10000\n"
45 "Content-Encoding: gzip\n"
46 "Content-Length: 30\n", // Intentionally wrong.
55 const MockTransaction kRedirect_Transaction
= {
56 "http://www.google.com/redirect",
62 "Cache-Control: max-age=10000\n"
63 "Location: http://www.google.com/destination\n"
64 "Content-Length: 5\n",
75 TEST(URLRequestJob
, TransactionNotifiedWhenDone
) {
76 MockNetworkLayer network_layer
;
77 TestURLRequestContext context
;
78 context
.set_http_transaction_factory(&network_layer
);
82 GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
, &context
);
83 AddMockTransaction(&kGZip_Transaction
);
85 req
.set_method("GET");
88 base::MessageLoop::current()->Run();
90 EXPECT_TRUE(network_layer
.done_reading_called());
92 RemoveMockTransaction(&kGZip_Transaction
);
95 TEST(URLRequestJob
, SyncTransactionNotifiedWhenDone
) {
96 MockNetworkLayer network_layer
;
97 TestURLRequestContext context
;
98 context
.set_http_transaction_factory(&network_layer
);
102 GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
, &context
);
103 MockTransaction
transaction(kGZip_Transaction
);
104 transaction
.test_mode
= TEST_MODE_SYNC_ALL
;
105 AddMockTransaction(&transaction
);
107 req
.set_method("GET");
110 base::RunLoop().Run();
112 EXPECT_TRUE(network_layer
.done_reading_called());
114 RemoveMockTransaction(&transaction
);
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
);
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
);
131 req
.set_method("GET");
134 base::RunLoop().Run();
136 EXPECT_TRUE(network_layer
.done_reading_called());
138 RemoveMockTransaction(&transaction
);
141 TEST(URLRequestJob
, RedirectTransactionNotifiedWhenDone
) {
142 MockNetworkLayer network_layer
;
143 TestURLRequestContext context
;
144 context
.set_http_transaction_factory(&network_layer
);
148 GURL(kRedirect_Transaction
.url
), DEFAULT_PRIORITY
, &d
, &context
);
149 AddMockTransaction(&kRedirect_Transaction
);
151 req
.set_method("GET");
154 base::RunLoop().Run();
156 EXPECT_TRUE(network_layer
.done_reading_called());
158 RemoveMockTransaction(&kRedirect_Transaction
);
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
);
170 TestURLRequest
req(GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
,
172 AddMockTransaction(&kGZip_Transaction
);
174 req
.set_method("GET");
177 base::RunLoop().Run();
179 EXPECT_TRUE(network_layer
.stop_caching_called());
181 RemoveMockTransaction(&kGZip_Transaction
);