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.
59 const MockTransaction kRedirect_Transaction
= {
60 "http://www.google.com/redirect",
66 "Cache-Control: max-age=10000\n"
67 "Location: http://www.google.com/destination\n"
68 "Content-Length: 5\n",
81 TEST(URLRequestJob
, TransactionNotifiedWhenDone
) {
82 MockNetworkLayer network_layer
;
83 TestURLRequestContext context
;
84 context
.set_http_transaction_factory(&network_layer
);
87 scoped_ptr
<URLRequest
> req(
88 context
.CreateRequest(GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
89 AddMockTransaction(&kGZip_Transaction
);
91 req
->set_method("GET");
94 base::MessageLoop::current()->Run();
96 EXPECT_TRUE(network_layer
.done_reading_called());
98 RemoveMockTransaction(&kGZip_Transaction
);
101 TEST(URLRequestJob
, SyncTransactionNotifiedWhenDone
) {
102 MockNetworkLayer network_layer
;
103 TestURLRequestContext context
;
104 context
.set_http_transaction_factory(&network_layer
);
107 scoped_ptr
<URLRequest
> req(
108 context
.CreateRequest(GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
109 MockTransaction
transaction(kGZip_Transaction
);
110 transaction
.test_mode
= TEST_MODE_SYNC_ALL
;
111 AddMockTransaction(&transaction
);
113 req
->set_method("GET");
116 base::RunLoop().Run();
118 EXPECT_TRUE(network_layer
.done_reading_called());
120 RemoveMockTransaction(&transaction
);
123 // Tests processing a large gzip header one byte at a time.
124 TEST(URLRequestJob
, SyncSlowTransaction
) {
125 MockNetworkLayer network_layer
;
126 TestURLRequestContext context
;
127 context
.set_http_transaction_factory(&network_layer
);
130 scoped_ptr
<URLRequest
> req(
131 context
.CreateRequest(GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
132 MockTransaction
transaction(kGZip_Transaction
);
133 transaction
.test_mode
= TEST_MODE_SYNC_ALL
| TEST_MODE_SLOW_READ
;
134 transaction
.handler
= &BigGZipServer
;
135 AddMockTransaction(&transaction
);
137 req
->set_method("GET");
140 base::RunLoop().Run();
142 EXPECT_TRUE(network_layer
.done_reading_called());
144 RemoveMockTransaction(&transaction
);
147 TEST(URLRequestJob
, RedirectTransactionNotifiedWhenDone
) {
148 MockNetworkLayer network_layer
;
149 TestURLRequestContext context
;
150 context
.set_http_transaction_factory(&network_layer
);
153 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
154 GURL(kRedirect_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
155 AddMockTransaction(&kRedirect_Transaction
);
157 req
->set_method("GET");
160 base::RunLoop().Run();
162 EXPECT_TRUE(network_layer
.done_reading_called());
164 RemoveMockTransaction(&kRedirect_Transaction
);
167 TEST(URLRequestJob
, TransactionNotCachedWhenNetworkDelegateRedirects
) {
168 MockNetworkLayer network_layer
;
169 TestNetworkDelegate network_delegate
;
170 network_delegate
.set_redirect_on_headers_received_url(GURL("http://foo"));
171 TestURLRequestContext context
;
172 context
.set_http_transaction_factory(&network_layer
);
173 context
.set_network_delegate(&network_delegate
);
176 scoped_ptr
<URLRequest
> req(
177 context
.CreateRequest(GURL(kGZip_Transaction
.url
), DEFAULT_PRIORITY
, &d
));
178 AddMockTransaction(&kGZip_Transaction
);
180 req
->set_method("GET");
183 base::RunLoop().Run();
185 EXPECT_TRUE(network_layer
.stop_caching_called());
187 RemoveMockTransaction(&kGZip_Transaction
);