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

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

Issue 1284993005: Notify NetworkDelegate when bytes have been received over the network. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed mmenke comments Created 5 years, 4 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
« no previous file with comments | « net/url_request/url_request_http_job.cc ('k') | net/url_request/url_request_job.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, 126 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
127 &delegate) 127 &delegate)
128 .Pass(); 128 .Pass();
129 129
130 request->Start(); 130 request->Start();
131 ASSERT_TRUE(request->is_pending()); 131 ASSERT_TRUE(request->is_pending());
132 base::RunLoop().Run(); 132 base::RunLoop().Run();
133 133
134 EXPECT_TRUE(request->status().is_success()); 134 EXPECT_TRUE(request->status().is_success());
135 EXPECT_EQ(12, request->received_response_content_length()); 135 EXPECT_EQ(12, request->received_response_content_length());
136 EXPECT_EQ(51, network_delegate_.total_network_bytes_received());
137 }
138
139 TEST_F(URLRequestHttpJobWithMockSocketsTest,
140 TestContentLengthSuccessfulHttp09Request) {
141 MockRead reads[] = {MockRead("HTTP/0.9 200 OK\r\n"
142 "Content-Length: 12\r\n\r\n"),
mmenke 2015/08/20 15:59:33 Sorry, should have actually said what HTTP/0.9 loo
sclittle 2015/08/20 18:18:23 Done.
143 MockRead("Test Content")};
144
145 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0);
146 socket_factory_.AddSocketDataProvider(&socket_data);
147
148 TestDelegate delegate;
149 scoped_ptr<URLRequest> request =
150 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
151 &delegate)
152 .Pass();
153
154 request->Start();
155 ASSERT_TRUE(request->is_pending());
156 base::RunLoop().Run();
157
158 EXPECT_TRUE(request->status().is_success());
159 EXPECT_EQ(12, request->received_response_content_length());
160 EXPECT_EQ(51, network_delegate_.total_network_bytes_received());
136 } 161 }
137 162
138 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestContentLengthAbortedRequest) { 163 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestContentLengthAbortedRequest) {
139 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" 164 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
140 "Content-Length: 20\r\n\r\n"), 165 "Content-Length: 20\r\n\r\n"),
141 MockRead("Test Content"), 166 MockRead("Test Content"),
142 MockRead(net::SYNCHRONOUS, net::ERR_FAILED)}; 167 MockRead(net::SYNCHRONOUS, net::ERR_FAILED)};
143 168
144 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0); 169 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0);
145 socket_factory_.AddSocketDataProvider(&socket_data); 170 socket_factory_.AddSocketDataProvider(&socket_data);
146 171
147 TestDelegate delegate; 172 TestDelegate delegate;
148 scoped_ptr<URLRequest> request = 173 scoped_ptr<URLRequest> request =
149 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, 174 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
150 &delegate) 175 &delegate)
151 .Pass(); 176 .Pass();
152 177
153 request->Start(); 178 request->Start();
154 ASSERT_TRUE(request->is_pending()); 179 ASSERT_TRUE(request->is_pending());
155 base::RunLoop().Run(); 180 base::RunLoop().Run();
156 181
157 EXPECT_EQ(URLRequestStatus::FAILED, request->status().status()); 182 EXPECT_EQ(URLRequestStatus::FAILED, request->status().status());
158 EXPECT_EQ(12, request->received_response_content_length()); 183 EXPECT_EQ(12, request->received_response_content_length());
184 EXPECT_EQ(51, network_delegate_.total_network_bytes_received());
159 } 185 }
160 186
161 TEST_F(URLRequestHttpJobWithMockSocketsTest, 187 TEST_F(URLRequestHttpJobWithMockSocketsTest,
162 TestContentLengthCancelledRequest) { 188 TestContentLengthCancelledRequest) {
163 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" 189 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
164 "Content-Length: 20\r\n\r\n"), 190 "Content-Length: 20\r\n\r\n"),
165 MockRead("Test Content"), 191 MockRead("Test Content"),
166 MockRead(net::SYNCHRONOUS, net::ERR_IO_PENDING)}; 192 MockRead(net::SYNCHRONOUS, net::ERR_IO_PENDING)};
167 193
168 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0); 194 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0);
169 socket_factory_.AddSocketDataProvider(&socket_data); 195 socket_factory_.AddSocketDataProvider(&socket_data);
170 196
171 TestDelegate delegate; 197 TestDelegate delegate;
172 scoped_ptr<URLRequest> request = 198 scoped_ptr<URLRequest> request =
173 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, 199 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
174 &delegate) 200 &delegate)
175 .Pass(); 201 .Pass();
176 202
203 delegate.set_cancel_in_received_data(true);
204 request->Start();
205 base::RunLoop().RunUntilIdle();
206
207 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status());
208 EXPECT_EQ(12, request->received_response_content_length());
209 EXPECT_EQ(51, network_delegate_.total_network_bytes_received());
210 }
211
212 TEST_F(URLRequestHttpJobWithMockSocketsTest,
213 TestNetworkBytesRedirectedRequest) {
214 MockRead redirect_read(
215 "HTTP/1.1 302 Found\r\n"
216 "Location: http://www.example.com\r\n\r\n");
217 StaticSocketDataProvider redirect_socket_data(&redirect_read, 1, nullptr, 0);
218 socket_factory_.AddSocketDataProvider(&redirect_socket_data);
219
220 MockRead response_reads[] = {MockRead("HTTP/1.1 200 OK\r\n"
221 "Content-Length: 12\r\n\r\n"),
222 MockRead("Test Content")};
223 StaticSocketDataProvider response_socket_data(
224 response_reads, arraysize(response_reads), nullptr, 0);
225 socket_factory_.AddSocketDataProvider(&response_socket_data);
226
227 TestDelegate delegate;
228 scoped_ptr<URLRequest> request =
229 context_->CreateRequest(GURL("http://www.redirect.com"), DEFAULT_PRIORITY,
230 &delegate)
231 .Pass();
232
177 request->Start(); 233 request->Start();
234 ASSERT_TRUE(request->is_pending());
178 base::RunLoop().RunUntilIdle(); 235 base::RunLoop().RunUntilIdle();
179 request->Cancel(); 236
180 base::RunLoop().Run(); 237 EXPECT_TRUE(request->status().is_success());
238 EXPECT_EQ(12, request->received_response_content_length());
239 EXPECT_EQ(107, network_delegate_.total_network_bytes_received());
240 }
241
242 TEST_F(URLRequestHttpJobWithMockSocketsTest,
243 TestNetworkBytesCancelledAfterHeaders) {
244 MockRead read("HTTP/1.1 200 OK\r\n\r\n");
245 StaticSocketDataProvider socket_data(&read, 1, nullptr, 0);
246 socket_factory_.AddSocketDataProvider(&socket_data);
247
248 TestDelegate delegate;
249 scoped_ptr<URLRequest> request =
250 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
251 &delegate)
252 .Pass();
253
254 delegate.set_cancel_in_response_started(true);
255 request->Start();
256 base::RunLoop().RunUntilIdle();
181 257
182 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status()); 258 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status());
183 EXPECT_EQ(12, request->received_response_content_length()); 259 EXPECT_EQ(19, network_delegate_.total_network_bytes_received());
260 }
261
262 TEST_F(URLRequestHttpJobWithMockSocketsTest,
263 TestNetworkBytesCancelledImmediately) {
264 StaticSocketDataProvider socket_data(nullptr, 0, nullptr, 0);
265 socket_factory_.AddSocketDataProvider(&socket_data);
266
267 TestDelegate delegate;
268 scoped_ptr<URLRequest> request =
269 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY,
270 &delegate)
271 .Pass();
272
273 request->Start();
274 request->Cancel();
275 base::RunLoop().RunUntilIdle();
276
277 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status());
278 EXPECT_EQ(0, network_delegate_.total_network_bytes_received());
184 } 279 }
185 280
186 TEST_F(URLRequestHttpJobWithMockSocketsTest, BackoffHeader) { 281 TEST_F(URLRequestHttpJobWithMockSocketsTest, BackoffHeader) {
187 MockWrite writes[] = {MockWrite( 282 MockWrite writes[] = {MockWrite(
188 "GET / HTTP/1.1\r\n" 283 "GET / HTTP/1.1\r\n"
189 "Host: www.example.com\r\n" 284 "Host: www.example.com\r\n"
190 "Connection: keep-alive\r\n" 285 "Connection: keep-alive\r\n"
191 "User-Agent:\r\n" 286 "User-Agent:\r\n"
192 "Accept-Encoding: gzip, deflate\r\n" 287 "Accept-Encoding: gzip, deflate\r\n"
193 "Accept-Language: en-us,fr\r\n\r\n")}; 288 "Accept-Language: en-us,fr\r\n\r\n")};
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 req_->SetLoadFlags(LOAD_DISABLE_CACHE); 667 req_->SetLoadFlags(LOAD_DISABLE_CACHE);
573 job->Start(); 668 job->Start();
574 base::RunLoop().RunUntilIdle(); 669 base::RunLoop().RunUntilIdle();
575 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); 670 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status());
576 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); 671 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called());
577 } 672 }
578 673
579 } // namespace 674 } // namespace
580 675
581 } // namespace net 676 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_http_job.cc ('k') | net/url_request/url_request_job.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698