Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: net/url_request/url_request_http_job_unittest.cc

Issue 1153093002: Implement URLRequestBackoffManager for managing Backoff headers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Matt's comments Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/url_request/url_request_http_job.h" 5 #include "net/url_request/url_request_http_job.h"
6 6
7 #include <cstddef> 7 #include <cstddef>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 void EnableSdch() { 86 void EnableSdch() {
87 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); 87 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass());
88 } 88 }
89 89
90 MockNetworkLayer network_layer_; 90 MockNetworkLayer network_layer_;
91 TestURLRequestContext context_; 91 TestURLRequestContext context_;
92 TestDelegate delegate_; 92 TestDelegate delegate_;
93 scoped_ptr<URLRequest> req_; 93 scoped_ptr<URLRequest> req_;
94 }; 94 };
95 95
96 class URLRequestHttpJobBackoffTest : public ::testing::Test {
97 protected:
98 URLRequestHttpJobBackoffTest() : context_(new TestURLRequestContext(true)) {
99 context_->set_client_socket_factory(&socket_factory_);
100 context_->set_network_delegate(&network_delegate_);
101 context_->set_backoff_manager(&manager_);
102 context_->Init();
103 }
104
105 MockClientSocketFactory socket_factory_;
106 TestNetworkDelegate network_delegate_;
107 URLRequestBackoffManager manager_;
108 scoped_ptr<TestURLRequestContext> context_;
109 };
110
111 TEST_F(URLRequestHttpJobBackoffTest, BackoffHeader) {
112 MockWrite writes[] = {MockWrite(
113 "GET / HTTP/1.1\r\n"
114 "Host: www.example.com\r\n"
115 "Connection: keep-alive\r\n"
116 "User-Agent:\r\n"
117 "Accept-Encoding: gzip, deflate\r\n"
118 "Accept-Language: en-us,fr\r\n\r\n")};
119 MockRead reads[] = {MockRead(
120 "HTTP/1.1 200 OK\r\n"
121 "Backoff: 3600\r\n"
122 "Content-Length: 9\r\n\r\n"),
123 MockRead("test.html")};
124
125 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
126 arraysize(writes));
127 socket_factory_.AddSocketDataProvider(&socket_data);
128
129 TestDelegate delegate1;
130 scoped_ptr<URLRequest> request1 =
131 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
132 &delegate1).Pass();
133
134 request1->Start();
135 ASSERT_TRUE(request1->is_pending());
136 base::RunLoop().Run();
137
138 EXPECT_TRUE(request1->status().is_success());
139 EXPECT_EQ("test.html", delegate1.data_received());
140 EXPECT_EQ(1, delegate1.received_before_network_start_count());
141
142 // Issue another request, and backoff logic should apply.
143 TestDelegate delegate2;
144 scoped_ptr<URLRequest> request2 =
145 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
146 &delegate2).Pass();
147
148 request2->Start();
149 ASSERT_TRUE(request2->is_pending());
150 base::RunLoop().Run();
151
152 EXPECT_FALSE(request2->status().is_success());
153 EXPECT_EQ(ERR_TEMPORARY_BACKOFF, request2->status().error());
154 EXPECT_EQ(0, delegate2.received_before_network_start_count());
155 }
156
157 TEST_F(URLRequestHttpJobBackoffTest, BackoffHeaderCachedResponse) {
158 MockWrite writes[] = {MockWrite(
159 "GET / HTTP/1.1\r\n"
160 "Host: www.example.com\r\n"
161 "Connection: keep-alive\r\n"
162 "User-Agent:\r\n"
163 "Accept-Encoding: gzip, deflate\r\n"
164 "Accept-Language: en-us,fr\r\n\r\n")};
165 MockRead reads[] = {MockRead(
166 "HTTP/1.1 200 OK\r\n"
167 "Backoff: 3600\r\n"
168 "Cache-Control: max-age=120\r\n"
169 "Content-Length: 9\r\n\r\n"),
170 MockRead("test.html")};
171
172 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
173 arraysize(writes));
174 socket_factory_.AddSocketDataProvider(&socket_data);
175
176 TestDelegate delegate1;
177 scoped_ptr<URLRequest> request1 =
178 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
179 &delegate1).Pass();
180
181 request1->Start();
182 ASSERT_TRUE(request1->is_pending());
183 base::RunLoop().Run();
184
185 EXPECT_TRUE(request1->status().is_success());
186 EXPECT_EQ("test.html", delegate1.data_received());
187 EXPECT_EQ(1, delegate1.received_before_network_start_count());
188
189 // Backoff logic does not apply to a second request, since it is fetched
190 // from cache.
191 TestDelegate delegate2;
192 scoped_ptr<URLRequest> request2 =
193 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
194 &delegate2).Pass();
195
196 request2->Start();
197 ASSERT_TRUE(request2->is_pending());
198 base::RunLoop().Run();
199 EXPECT_TRUE(request2->was_cached());
200 EXPECT_TRUE(request2->status().is_success());
201 EXPECT_EQ(0, delegate2.received_before_network_start_count());
202 }
203
96 // Make sure that SetPriority actually sets the URLRequestHttpJob's 204 // Make sure that SetPriority actually sets the URLRequestHttpJob's
97 // priority, both before and after start. 205 // priority, both before and after start.
98 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) { 206 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) {
99 scoped_refptr<TestURLRequestHttpJob> job( 207 scoped_refptr<TestURLRequestHttpJob> job(
100 new TestURLRequestHttpJob(req_.get())); 208 new TestURLRequestHttpJob(req_.get()));
101 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); 209 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
102 210
103 job->SetPriority(LOWEST); 211 job->SetPriority(LOWEST);
104 EXPECT_EQ(LOWEST, job->priority()); 212 EXPECT_EQ(LOWEST, job->priority());
105 213
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 req_->SetLoadFlags(LOAD_DISABLE_CACHE); 451 req_->SetLoadFlags(LOAD_DISABLE_CACHE);
344 job->Start(); 452 job->Start();
345 base::RunLoop().RunUntilIdle(); 453 base::RunLoop().RunUntilIdle();
346 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); 454 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status());
347 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); 455 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called());
348 } 456 }
349 457
350 } // namespace 458 } // namespace
351 459
352 } // namespace net 460 } // namespace net
OLDNEW
« net/url_request/url_request_backoff_manager.cc ('K') | « net/url_request/url_request_http_job.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698