Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / url_request / url_request_job_unittest.cc
blobbdce8bdcca9a3f5befd6e81a7e41f2e9c22d3082
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"
13 namespace net {
15 namespace {
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",
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,
55 const MockTransaction kRedirect_Transaction = {
56 "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,
73 } // namespace
75 TEST(URLRequestJob, TransactionNotifiedWhenDone) {
76 MockNetworkLayer network_layer;
77 TestURLRequestContext context;
78 context.set_http_transaction_factory(&network_layer);
80 TestDelegate d;
81 TestURLRequest req(
82 GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d, &context);
83 AddMockTransaction(&kGZip_Transaction);
85 req.set_method("GET");
86 req.Start();
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);
100 TestDelegate d;
101 TestURLRequest req(
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");
108 req.Start();
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);
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);
131 req.set_method("GET");
132 req.Start();
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);
146 TestDelegate d;
147 TestURLRequest req(
148 GURL(kRedirect_Transaction.url), DEFAULT_PRIORITY, &d, &context);
149 AddMockTransaction(&kRedirect_Transaction);
151 req.set_method("GET");
152 req.Start();
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);
169 TestDelegate d;
170 TestURLRequest req(GURL(kGZip_Transaction.url), DEFAULT_PRIORITY, &d,
171 &context);
172 AddMockTransaction(&kGZip_Transaction);
174 req.set_method("GET");
175 req.Start();
177 base::RunLoop().Run();
179 EXPECT_TRUE(network_layer.stop_caching_called());
181 RemoveMockTransaction(&kGZip_Transaction);
184 } // namespace net