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/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "net/base/request_priority.h"
10 #include "net/http/http_transaction_test_util.h"
11 #include "net/url_request/url_request.h"
12 #include "net/url_request/url_request_test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
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";
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";
24 void GZipServer(const HttpRequestInfo
* request
,
25 std::string
* response_status
,
26 std::string
* response_headers
,
27 std::string
* response_data
) {
28 response_data
->assign(kGzipData
, sizeof(kGzipData
));
31 void BigGZipServer(const HttpRequestInfo
* request
,
32 std::string
* response_status
,
33 std::string
* response_headers
,
34 std::string
* response_data
) {
35 response_data
->assign(kGzipDataWithName
, sizeof(kGzipDataWithName
));
36 response_data
->insert(10, 64 * 1024, 'a');
39 const MockTransaction kGZip_Transaction
= {
40 "http://www.google.com/gzyp",
46 "Cache-Control: max-age=10000\n"
47 "Content-Encoding: gzip\n"
48 "Content-Length: 30\n", // Intentionally wrong.
57 const MockTransaction kRedirect_Transaction
= {
58 "http://www.google.com/redirect",
64 "Cache-Control: max-age=10000\n"
65 "Location: http://www.google.com/destination\n"
66 "Content-Length: 5\n",
77 TEST(URLRequestJob
, TransactionNotifiedWhenDone
) {
78 MockNetworkLayer network_layer
;
79 TestURLRequestContext context
;
80 context
.set_http_transaction_factory(&network_layer
);
83 scoped_ptr
<URLRequest
> req(
84 context
.CreateRequest(GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
85 AddMockTransaction(&kGZip_Transaction
);
87 req
->set_method("GET");
90 base::MessageLoop::current()->Run();
92 EXPECT_TRUE(network_layer
.done_reading_called());
94 RemoveMockTransaction(&kGZip_Transaction
);
97 TEST(URLRequestJob
, SyncTransactionNotifiedWhenDone
) {
98 MockNetworkLayer network_layer
;
99 TestURLRequestContext context
;
100 context
.set_http_transaction_factory(&network_layer
);
103 scoped_ptr
<URLRequest
> req(
104 context
.CreateRequest(GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
105 MockTransaction
transaction(kGZip_Transaction
);
106 transaction
.test_mode
= TEST_MODE_SYNC_ALL
;
107 AddMockTransaction(&transaction
);
109 req
->set_method("GET");
112 base::RunLoop().Run();
114 EXPECT_TRUE(network_layer
.done_reading_called());
116 RemoveMockTransaction(&transaction
);
119 // Tests processing a large gzip header one byte at a time.
120 TEST(URLRequestJob
, SyncSlowTransaction
) {
121 MockNetworkLayer network_layer
;
122 TestURLRequestContext context
;
123 context
.set_http_transaction_factory(&network_layer
);
126 scoped_ptr
<URLRequest
> req(
127 context
.CreateRequest(GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
128 MockTransaction
transaction(kGZip_Transaction
);
129 transaction
.test_mode
= TEST_MODE_SYNC_ALL
| TEST_MODE_SLOW_READ
;
130 transaction
.handler
= &BigGZipServer
;
131 AddMockTransaction(&transaction
);
133 req
->set_method("GET");
136 base::RunLoop().Run();
138 EXPECT_TRUE(network_layer
.done_reading_called());
140 RemoveMockTransaction(&transaction
);
143 TEST(URLRequestJob
, RedirectTransactionNotifiedWhenDone
) {
144 MockNetworkLayer network_layer
;
145 TestURLRequestContext context
;
146 context
.set_http_transaction_factory(&network_layer
);
149 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
150 GURL(kRedirect_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
151 AddMockTransaction(&kRedirect_Transaction
);
153 req
->set_method("GET");
156 base::RunLoop().Run();
158 EXPECT_TRUE(network_layer
.done_reading_called());
160 RemoveMockTransaction(&kRedirect_Transaction
);
163 TEST(URLRequestJob
, TransactionNotCachedWhenNetworkDelegateRedirects
) {
164 MockNetworkLayer network_layer
;
165 TestNetworkDelegate network_delegate
;
166 network_delegate
.set_redirect_on_headers_received_url(GURL("http://foo"));
167 TestURLRequestContext context
;
168 context
.set_http_transaction_factory(&network_layer
);
169 context
.set_network_delegate(&network_delegate
);
172 scoped_ptr
<URLRequest
> req(
173 context
.CreateRequest(GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
174 AddMockTransaction(&kGZip_Transaction
);
176 req
->set_method("GET");
179 base::RunLoop().Run();
181 EXPECT_TRUE(network_layer
.stop_caching_called());
183 RemoveMockTransaction(&kGZip_Transaction
);