Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / url_request / url_request_job_unittest.cc
blobf9ea1fe6170e34d08926435bb08cf97daeb3f0b4
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"
15 namespace net {
17 namespace {
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",
41 "GET",
42 base::Time(),
43 "",
44 LOAD_NORMAL,
45 "HTTP/1.1 200 OK",
46 "Cache-Control: max-age=10000\n"
47 "Content-Encoding: gzip\n"
48 "Content-Length: 30\n", // Intentionally wrong.
49 base::Time(),
50 "",
51 TEST_MODE_NORMAL,
52 &GZipServer,
53 nullptr,
56 OK,
59 const MockTransaction kRedirect_Transaction = {
60 "http://www.google.com/redirect",
61 "GET",
62 base::Time(),
63 "",
64 LOAD_NORMAL,
65 "HTTP/1.1 302 Found",
66 "Cache-Control: max-age=10000\n"
67 "Location: http://www.google.com/destination\n"
68 "Content-Length: 5\n",
69 base::Time(),
70 "hello",
71 TEST_MODE_NORMAL,
72 nullptr,
73 nullptr,
76 OK,
79 } // namespace
81 TEST(URLRequestJob, TransactionNotifiedWhenDone) {
82 MockNetworkLayer network_layer;
83 TestURLRequestContext context;
84 context.set_http_transaction_factory(&network_layer);
86 TestDelegate d;
87 scoped_ptr<URLRequest> req(
88 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d));
89 AddMockTransaction(&kGZip_Transaction);
91 req->set_method("GET");
92 req->Start();
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);
106 TestDelegate d;
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");
114 req->Start();
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);
129 TestDelegate d;
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");
138 req->Start();
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);
152 TestDelegate d;
153 scoped_ptr<URLRequest> req(context.CreateRequest(
154 GURL(kRedirect_Transaction.url), DEFAULT_PRIORITY, &d));
155 AddMockTransaction(&kRedirect_Transaction);
157 req->set_method("GET");
158 req->Start();
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);
175 TestDelegate d;
176 scoped_ptr<URLRequest> req(
177 context.CreateRequest(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d));
178 AddMockTransaction(&kGZip_Transaction);
180 req->set_method("GET");
181 req->Start();
183 base::RunLoop().Run();
185 EXPECT_TRUE(network_layer.stop_caching_called());
187 RemoveMockTransaction(&kGZip_Transaction);
190 } // namespace net