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

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: 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 void EnableSdch() { 85 void EnableSdch() {
86 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass()); 86 context_.SetSdchManager(scoped_ptr<SdchManager>(new SdchManager).Pass());
87 } 87 }
88 88
89 MockNetworkLayer network_layer_; 89 MockNetworkLayer network_layer_;
90 TestURLRequestContext context_; 90 TestURLRequestContext context_;
91 TestDelegate delegate_; 91 TestDelegate delegate_;
92 scoped_ptr<URLRequest> req_; 92 scoped_ptr<URLRequest> req_;
93 }; 93 };
94 94
95 class URLRequestHttpJobBackoffTest : public ::testing::Test {
96 protected:
97 URLRequestHttpJobBackoffTest() : context_(new TestURLRequestContext(true)) {
98 context_->set_client_socket_factory(&socket_factory_);
99 context_->set_network_delegate(&network_delegate_);
100 context_->set_backoff_manager(&manager_);
101 context_->Init();
102 }
103
104 MockClientSocketFactory socket_factory_;
105 TestNetworkDelegate network_delegate_;
106 URLRequestBackoffManager manager_;
107 scoped_ptr<TestURLRequestContext> context_;
108 };
109
110 TEST_F(URLRequestHttpJobBackoffTest, BackoffHeader) {
111 MockWrite writes[] = {MockWrite(
112 "GET / HTTP/1.1\r\n"
113 "Host: www.example.com\r\n"
114 "Connection: keep-alive\r\n"
115 "User-Agent:\r\n"
116 "Accept-Encoding: gzip, deflate\r\n"
117 "Accept-Language: en-us,fr\r\n\r\n")};
118 MockRead reads[] = {MockRead(
119 "HTTP/1.1 200 OK\r\n"
120 "Backoff: 3600\r\n"
121 "Content-Length: 9\r\n\r\n"),
122 MockRead("test.html")};
123
124 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
125 arraysize(writes));
126 socket_factory_.AddSocketDataProvider(&socket_data);
127
128 TestDelegate delegate1;
129 scoped_ptr<URLRequest> request1 =
130 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
131 &delegate1).Pass();
132
133 request1->Start();
134 ASSERT_TRUE(request1->is_pending());
135 base::RunLoop().Run();
136
137 EXPECT_TRUE(request1->status().is_success());
138 EXPECT_EQ("test.html", delegate1.data_received());
139 EXPECT_EQ(1, delegate1.received_before_network_start_count());
140
141 // Issue another request, and backoff logic should apply.
142 TestDelegate delegate2;
143 scoped_ptr<URLRequest> request2 =
144 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
145 &delegate2).Pass();
146
147 request2->Start();
148 ASSERT_TRUE(request2->is_pending());
149 base::RunLoop().Run();
150
151 EXPECT_FALSE(request2->status().is_success());
152 EXPECT_EQ(ERR_TEMPORARY_BACKOFF, request2->status().error());
153 EXPECT_EQ(0, delegate2.received_before_network_start_count());
154 }
155
156 TEST_F(URLRequestHttpJobBackoffTest, BackoffHeaderCachedResponse) {
157 MockWrite writes[] = {MockWrite(
158 "GET / HTTP/1.1\r\n"
159 "Host: www.example.com\r\n"
160 "Connection: keep-alive\r\n"
161 "User-Agent:\r\n"
162 "Accept-Encoding: gzip, deflate\r\n"
163 "Accept-Language: en-us,fr\r\n\r\n")};
164 MockRead reads[] = {MockRead(
165 "HTTP/1.1 200 OK\r\n"
166 "Backoff: 3600\r\n"
167 "Cache-Control: max-age=120\r\n"
168 "Content-Length: 9\r\n\r\n"),
169 MockRead("test.html")};
170
171 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes,
172 arraysize(writes));
173 socket_factory_.AddSocketDataProvider(&socket_data);
174
175 TestDelegate delegate1;
176 scoped_ptr<URLRequest> request1 =
177 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
178 &delegate1).Pass();
179
180 request1->Start();
181 ASSERT_TRUE(request1->is_pending());
182 base::RunLoop().Run();
183
184 EXPECT_TRUE(request1->status().is_success());
185 EXPECT_EQ("test.html", delegate1.data_received());
186 EXPECT_EQ(1, delegate1.received_before_network_start_count());
187
188 // Backoff logic does not apply to a second request, since it is fetched
189 // from cache.
190 TestDelegate delegate2;
191 scoped_ptr<URLRequest> request2 =
192 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
193 &delegate2).Pass();
194
195 request2->Start();
196 ASSERT_TRUE(request2->is_pending());
197 base::RunLoop().Run();
198 EXPECT_TRUE(request2->was_cached());
199 EXPECT_TRUE(request2->status().is_success());
200 EXPECT_EQ(0, delegate2.received_before_network_start_count());
201 }
202
95 // Make sure that SetPriority actually sets the URLRequestHttpJob's 203 // Make sure that SetPriority actually sets the URLRequestHttpJob's
96 // priority, both before and after start. 204 // priority, both before and after start.
97 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) { 205 TEST_F(URLRequestHttpJobTest, SetPriorityBasic) {
98 scoped_refptr<TestURLRequestHttpJob> job( 206 scoped_refptr<TestURLRequestHttpJob> job(
99 new TestURLRequestHttpJob(req_.get())); 207 new TestURLRequestHttpJob(req_.get()));
100 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); 208 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
101 209
102 job->SetPriority(LOWEST); 210 job->SetPriority(LOWEST);
103 EXPECT_EQ(LOWEST, job->priority()); 211 EXPECT_EQ(LOWEST, job->priority());
104 212
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 req_->SetLoadFlags(LOAD_DISABLE_CACHE); 450 req_->SetLoadFlags(LOAD_DISABLE_CACHE);
343 job->Start(); 451 job->Start();
344 base::RunLoop().RunUntilIdle(); 452 base::RunLoop().RunUntilIdle();
345 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); 453 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status());
346 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); 454 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called());
347 } 455 }
348 456
349 } // namespace 457 } // namespace
350 458
351 } // namespace net 459 } // namespace net
OLDNEW
« net/url_request/url_request_context_builder.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