| OLD | NEW |
| 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 Loading... |
| 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("Test Content"), |
| 142 MockRead(net::SYNCHRONOUS, net::OK)}; |
| 143 |
| 144 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0); |
| 145 socket_factory_.AddSocketDataProvider(&socket_data); |
| 146 |
| 147 TestDelegate delegate; |
| 148 scoped_ptr<URLRequest> request = |
| 149 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, |
| 150 &delegate) |
| 151 .Pass(); |
| 152 |
| 153 request->Start(); |
| 154 ASSERT_TRUE(request->is_pending()); |
| 155 base::RunLoop().Run(); |
| 156 |
| 157 EXPECT_TRUE(request->status().is_success()); |
| 158 EXPECT_EQ(12, request->received_response_content_length()); |
| 159 EXPECT_EQ(12, network_delegate_.total_network_bytes_received()); |
| 136 } | 160 } |
| 137 | 161 |
| 138 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestContentLengthAbortedRequest) { | 162 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestContentLengthAbortedRequest) { |
| 139 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" | 163 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| 140 "Content-Length: 20\r\n\r\n"), | 164 "Content-Length: 20\r\n\r\n"), |
| 141 MockRead("Test Content"), | 165 MockRead("Test Content"), |
| 142 MockRead(net::SYNCHRONOUS, net::ERR_FAILED)}; | 166 MockRead(net::SYNCHRONOUS, net::ERR_FAILED)}; |
| 143 | 167 |
| 144 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0); | 168 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0); |
| 145 socket_factory_.AddSocketDataProvider(&socket_data); | 169 socket_factory_.AddSocketDataProvider(&socket_data); |
| 146 | 170 |
| 147 TestDelegate delegate; | 171 TestDelegate delegate; |
| 148 scoped_ptr<URLRequest> request = | 172 scoped_ptr<URLRequest> request = |
| 149 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, | 173 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, |
| 150 &delegate) | 174 &delegate) |
| 151 .Pass(); | 175 .Pass(); |
| 152 | 176 |
| 153 request->Start(); | 177 request->Start(); |
| 154 ASSERT_TRUE(request->is_pending()); | 178 ASSERT_TRUE(request->is_pending()); |
| 155 base::RunLoop().Run(); | 179 base::RunLoop().Run(); |
| 156 | 180 |
| 157 EXPECT_EQ(URLRequestStatus::FAILED, request->status().status()); | 181 EXPECT_EQ(URLRequestStatus::FAILED, request->status().status()); |
| 158 EXPECT_EQ(12, request->received_response_content_length()); | 182 EXPECT_EQ(12, request->received_response_content_length()); |
| 183 EXPECT_EQ(51, network_delegate_.total_network_bytes_received()); |
| 159 } | 184 } |
| 160 | 185 |
| 161 TEST_F(URLRequestHttpJobWithMockSocketsTest, | 186 TEST_F(URLRequestHttpJobWithMockSocketsTest, |
| 162 TestContentLengthCancelledRequest) { | 187 TestContentLengthCancelledRequest) { |
| 163 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" | 188 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| 164 "Content-Length: 20\r\n\r\n"), | 189 "Content-Length: 20\r\n\r\n"), |
| 165 MockRead("Test Content"), | 190 MockRead("Test Content"), |
| 166 MockRead(net::SYNCHRONOUS, net::ERR_IO_PENDING)}; | 191 MockRead(net::SYNCHRONOUS, net::ERR_IO_PENDING)}; |
| 167 | 192 |
| 168 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0); | 193 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0); |
| 169 socket_factory_.AddSocketDataProvider(&socket_data); | 194 socket_factory_.AddSocketDataProvider(&socket_data); |
| 170 | 195 |
| 171 TestDelegate delegate; | 196 TestDelegate delegate; |
| 172 scoped_ptr<URLRequest> request = | 197 scoped_ptr<URLRequest> request = |
| 173 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, | 198 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, |
| 174 &delegate) | 199 &delegate) |
| 175 .Pass(); | 200 .Pass(); |
| 176 | 201 |
| 202 delegate.set_cancel_in_received_data(true); |
| 203 request->Start(); |
| 204 base::RunLoop().RunUntilIdle(); |
| 205 |
| 206 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status()); |
| 207 EXPECT_EQ(12, request->received_response_content_length()); |
| 208 EXPECT_EQ(51, network_delegate_.total_network_bytes_received()); |
| 209 } |
| 210 |
| 211 TEST_F(URLRequestHttpJobWithMockSocketsTest, |
| 212 TestNetworkBytesRedirectedRequest) { |
| 213 MockRead redirect_read( |
| 214 "HTTP/1.1 302 Found\r\n" |
| 215 "Location: http://www.example.com\r\n\r\n"); |
| 216 StaticSocketDataProvider redirect_socket_data(&redirect_read, 1, nullptr, 0); |
| 217 socket_factory_.AddSocketDataProvider(&redirect_socket_data); |
| 218 |
| 219 MockRead response_reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| 220 "Content-Length: 12\r\n\r\n"), |
| 221 MockRead("Test Content")}; |
| 222 StaticSocketDataProvider response_socket_data( |
| 223 response_reads, arraysize(response_reads), nullptr, 0); |
| 224 socket_factory_.AddSocketDataProvider(&response_socket_data); |
| 225 |
| 226 TestDelegate delegate; |
| 227 scoped_ptr<URLRequest> request = |
| 228 context_->CreateRequest(GURL("http://www.redirect.com"), DEFAULT_PRIORITY, |
| 229 &delegate) |
| 230 .Pass(); |
| 231 |
| 177 request->Start(); | 232 request->Start(); |
| 233 ASSERT_TRUE(request->is_pending()); |
| 178 base::RunLoop().RunUntilIdle(); | 234 base::RunLoop().RunUntilIdle(); |
| 179 request->Cancel(); | 235 |
| 180 base::RunLoop().Run(); | 236 EXPECT_TRUE(request->status().is_success()); |
| 237 EXPECT_EQ(12, request->received_response_content_length()); |
| 238 EXPECT_EQ(107, network_delegate_.total_network_bytes_received()); |
| 239 } |
| 240 |
| 241 TEST_F(URLRequestHttpJobWithMockSocketsTest, |
| 242 TestNetworkBytesCancelledAfterHeaders) { |
| 243 MockRead read("HTTP/1.1 200 OK\r\n\r\n"); |
| 244 StaticSocketDataProvider socket_data(&read, 1, nullptr, 0); |
| 245 socket_factory_.AddSocketDataProvider(&socket_data); |
| 246 |
| 247 TestDelegate delegate; |
| 248 scoped_ptr<URLRequest> request = |
| 249 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, |
| 250 &delegate) |
| 251 .Pass(); |
| 252 |
| 253 delegate.set_cancel_in_response_started(true); |
| 254 request->Start(); |
| 255 base::RunLoop().RunUntilIdle(); |
| 181 | 256 |
| 182 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status()); | 257 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status()); |
| 183 EXPECT_EQ(12, request->received_response_content_length()); | 258 EXPECT_EQ(19, network_delegate_.total_network_bytes_received()); |
| 259 } |
| 260 |
| 261 TEST_F(URLRequestHttpJobWithMockSocketsTest, |
| 262 TestNetworkBytesCancelledImmediately) { |
| 263 StaticSocketDataProvider socket_data(nullptr, 0, nullptr, 0); |
| 264 socket_factory_.AddSocketDataProvider(&socket_data); |
| 265 |
| 266 TestDelegate delegate; |
| 267 scoped_ptr<URLRequest> request = |
| 268 context_->CreateRequest(GURL("http://www.example.com"), DEFAULT_PRIORITY, |
| 269 &delegate) |
| 270 .Pass(); |
| 271 |
| 272 request->Start(); |
| 273 request->Cancel(); |
| 274 base::RunLoop().RunUntilIdle(); |
| 275 |
| 276 EXPECT_EQ(URLRequestStatus::CANCELED, request->status().status()); |
| 277 EXPECT_EQ(0, network_delegate_.total_network_bytes_received()); |
| 184 } | 278 } |
| 185 | 279 |
| 186 TEST_F(URLRequestHttpJobWithMockSocketsTest, BackoffHeader) { | 280 TEST_F(URLRequestHttpJobWithMockSocketsTest, BackoffHeader) { |
| 187 MockWrite writes[] = {MockWrite( | 281 MockWrite writes[] = {MockWrite( |
| 188 "GET / HTTP/1.1\r\n" | 282 "GET / HTTP/1.1\r\n" |
| 189 "Host: www.example.com\r\n" | 283 "Host: www.example.com\r\n" |
| 190 "Connection: keep-alive\r\n" | 284 "Connection: keep-alive\r\n" |
| 191 "User-Agent:\r\n" | 285 "User-Agent:\r\n" |
| 192 "Accept-Encoding: gzip, deflate\r\n" | 286 "Accept-Encoding: gzip, deflate\r\n" |
| 193 "Accept-Language: en-us,fr\r\n\r\n")}; | 287 "Accept-Language: en-us,fr\r\n\r\n")}; |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 req_->SetLoadFlags(LOAD_DISABLE_CACHE); | 666 req_->SetLoadFlags(LOAD_DISABLE_CACHE); |
| 573 job->Start(); | 667 job->Start(); |
| 574 base::RunLoop().RunUntilIdle(); | 668 base::RunLoop().RunUntilIdle(); |
| 575 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); | 669 EXPECT_EQ(URLRequestStatus::IO_PENDING, req_->status().status()); |
| 576 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 670 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
| 577 } | 671 } |
| 578 | 672 |
| 579 } // namespace | 673 } // namespace |
| 580 | 674 |
| 581 } // namespace net | 675 } // namespace net |
| OLD | NEW |