blob: bdce8bdcca9a3f5befd6e81a7e41f2e9c22d3082 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include "net/url_request/url_request_job.h"
6
Ben Murdocheffb81e2014-03-31 11:51:25 +01007#include "base/run_loop.h"
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +00008#include "net/base/request_priority.h"
Torne (Richard Coles)cedac222014-06-03 10:58:34 +01009#include "net/http/http_transaction_test_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010#include "net/url_request/url_request_test_util.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000013namespace net {
14
Torne (Richard Coles)58218062012-11-14 11:43:16 +000015namespace {
16
17// This is a header that signals the end of the data.
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +010018const char kGzipData[] = "\x1f\x08b\x08\0\0\0\0\0\0\3\3\0\0\0\0\0\0\0\0";
19const char kGzipDataWithName[] =
20 "\x1f\x08b\x08\x08\0\0\0\0\0\0name\0\3\0\0\0\0\0\0\0\0";
Torne (Richard Coles)58218062012-11-14 11:43:16 +000021
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000022void GZipServer(const HttpRequestInfo* request,
23 std::string* response_status,
24 std::string* response_headers,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025 std::string* response_data) {
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +010026 response_data->assign(kGzipData, sizeof(kGzipData));
27}
28
29void 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');
Torne (Richard Coles)58218062012-11-14 11:43:16 +000035}
36
37const MockTransaction kGZip_Transaction = {
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000038 "http://www.google.com/gzyp",
39 "GET",
40 base::Time(),
41 "",
42 LOAD_NORMAL,
43 "HTTP/1.1 200 OK",
44 "Cache-Control: max-age=10000\n"
45 "Content-Encoding: gzip\n"
46 "Content-Length: 30\n", // Intentionally wrong.
47 base::Time(),
48 "",
49 TEST_MODE_NORMAL,
50 &GZipServer,
51 0,
52 OK
Torne (Richard Coles)58218062012-11-14 11:43:16 +000053};
54
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +010055const MockTransaction kRedirect_Transaction = {
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000056 "http://www.google.com/redirect",
57 "GET",
58 base::Time(),
59 "",
60 LOAD_NORMAL,
61 "HTTP/1.1 302 Found",
62 "Cache-Control: max-age=10000\n"
63 "Location: http://www.google.com/destination\n"
64 "Content-Length: 5\n",
65 base::Time(),
66 "hello",
67 TEST_MODE_NORMAL,
68 NULL,
69 0,
70 OK
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +010071};
72
Torne (Richard Coles)58218062012-11-14 11:43:16 +000073} // namespace
74
75TEST(URLRequestJob, TransactionNotifiedWhenDone) {
76 MockNetworkLayer network_layer;
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000077 TestURLRequestContext context;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000078 context.set_http_transaction_factory(&network_layer);
79
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000080 TestDelegate d;
81 TestURLRequest req(
82 GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000083 AddMockTransaction(&kGZip_Transaction);
84
85 req.set_method("GET");
86 req.Start();
87
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010088 base::MessageLoop::current()->Run();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000089
90 EXPECT_TRUE(network_layer.done_reading_called());
91
92 RemoveMockTransaction(&kGZip_Transaction);
93}
94
95TEST(URLRequestJob, SyncTransactionNotifiedWhenDone) {
96 MockNetworkLayer network_layer;
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000097 TestURLRequestContext context;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000098 context.set_http_transaction_factory(&network_layer);
99
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +0000100 TestDelegate d;
101 TestURLRequest req(
102 GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000103 MockTransaction transaction(kGZip_Transaction);
104 transaction.test_mode = TEST_MODE_SYNC_ALL;
105 AddMockTransaction(&transaction);
106
107 req.set_method("GET");
108 req.Start();
109
Ben Murdocheffb81e2014-03-31 11:51:25 +0100110 base::RunLoop().Run();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000111
112 EXPECT_TRUE(network_layer.done_reading_called());
113
114 RemoveMockTransaction(&transaction);
115}
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100116
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +0100117// Tests processing a large gzip header one byte at a time.
118TEST(URLRequestJob, SyncSlowTransaction) {
119 MockNetworkLayer network_layer;
120 TestURLRequestContext context;
121 context.set_http_transaction_factory(&network_layer);
122
123 TestDelegate d;
124 TestURLRequest req(
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);
130
131 req.set_method("GET");
132 req.Start();
133
134 base::RunLoop().Run();
135
136 EXPECT_TRUE(network_layer.done_reading_called());
137
138 RemoveMockTransaction(&transaction);
139}
140
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100141TEST(URLRequestJob, RedirectTransactionNotifiedWhenDone) {
142 MockNetworkLayer network_layer;
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +0000143 TestURLRequestContext context;
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100144 context.set_http_transaction_factory(&network_layer);
145
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +0000146 TestDelegate d;
147 TestURLRequest req(
148 GURL(kRedirect_Transaction.url), DEFAULT_PRIORITY, &d, &context);
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100149 AddMockTransaction(&kRedirect_Transaction);
150
151 req.set_method("GET");
152 req.Start();
153
Ben Murdocheffb81e2014-03-31 11:51:25 +0100154 base::RunLoop().Run();
Torne (Richard Coles)d0247b12013-09-19 22:36:51 +0100155
156 EXPECT_TRUE(network_layer.done_reading_called());
157
158 RemoveMockTransaction(&kRedirect_Transaction);
159}
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +0000160
Ben Murdocheffb81e2014-03-31 11:51:25 +0100161TEST(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);
168
169 TestDelegate d;
170 TestURLRequest req(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d,
171 &context);
172 AddMockTransaction(&kGZip_Transaction);
173
174 req.set_method("GET");
175 req.Start();
176
177 base::RunLoop().Run();
178
179 EXPECT_TRUE(network_layer.stop_caching_called());
180
181 RemoveMockTransaction(&kGZip_Transaction);
182}
183
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +0000184} // namespace net