| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/http/http_stream_parser.h" | 5 #include "net/http/http_stream_parser.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 28 #include "net/base/test_completion_callback.h" | 28 #include "net/base/test_completion_callback.h" |
| 29 #include "net/base/upload_bytes_element_reader.h" | 29 #include "net/base/upload_bytes_element_reader.h" |
| 30 #include "net/base/upload_file_element_reader.h" | 30 #include "net/base/upload_file_element_reader.h" |
| 31 #include "net/http/http_request_headers.h" | 31 #include "net/http/http_request_headers.h" |
| 32 #include "net/http/http_request_info.h" | 32 #include "net/http/http_request_info.h" |
| 33 #include "net/http/http_response_headers.h" | 33 #include "net/http/http_response_headers.h" |
| 34 #include "net/http/http_response_info.h" | 34 #include "net/http/http_response_info.h" |
| 35 #include "net/socket/client_socket_handle.h" | 35 #include "net/socket/client_socket_handle.h" |
| 36 #include "net/socket/socket_test_util.h" | 36 #include "net/socket/socket_test_util.h" |
| 37 #include "net/test/gtest_util.h" |
| 38 #include "testing/gmock/include/gmock/gmock.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 38 #include "url/gurl.h" | 40 #include "url/gurl.h" |
| 39 | 41 |
| 42 using net::test::IsError; |
| 43 using net::test::IsOk; |
| 44 |
| 40 namespace net { | 45 namespace net { |
| 41 | 46 |
| 42 namespace { | 47 namespace { |
| 43 | 48 |
| 44 const size_t kOutputSize = 1024; // Just large enough for this test. | 49 const size_t kOutputSize = 1024; // Just large enough for this test. |
| 45 // The number of bytes that can fit in a buffer of kOutputSize. | 50 // The number of bytes that can fit in a buffer of kOutputSize. |
| 46 const size_t kMaxPayloadSize = | 51 const size_t kMaxPayloadSize = |
| 47 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize; | 52 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize; |
| 48 | 53 |
| 49 // Helper method to create a connected ClientSocketHandle using |data|. | 54 // Helper method to create a connected ClientSocketHandle using |data|. |
| 50 // Modifies |data|. | 55 // Modifies |data|. |
| 51 std::unique_ptr<ClientSocketHandle> CreateConnectedSocketHandle( | 56 std::unique_ptr<ClientSocketHandle> CreateConnectedSocketHandle( |
| 52 SequencedSocketData* data) { | 57 SequencedSocketData* data) { |
| 53 data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 58 data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 54 | 59 |
| 55 std::unique_ptr<MockTCPClientSocket> socket( | 60 std::unique_ptr<MockTCPClientSocket> socket( |
| 56 new MockTCPClientSocket(net::AddressList(), nullptr, data)); | 61 new MockTCPClientSocket(net::AddressList(), nullptr, data)); |
| 57 | 62 |
| 58 TestCompletionCallback callback; | 63 TestCompletionCallback callback; |
| 59 EXPECT_EQ(OK, socket->Connect(callback.callback())); | 64 EXPECT_THAT(socket->Connect(callback.callback()), IsOk()); |
| 60 | 65 |
| 61 std::unique_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle); | 66 std::unique_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle); |
| 62 socket_handle->SetSocket(std::move(socket)); | 67 socket_handle->SetSocket(std::move(socket)); |
| 63 return socket_handle; | 68 return socket_handle; |
| 64 } | 69 } |
| 65 | 70 |
| 66 class ReadErrorUploadDataStream : public UploadDataStream { | 71 class ReadErrorUploadDataStream : public UploadDataStream { |
| 67 public: | 72 public: |
| 68 enum class FailureMode { SYNC, ASYNC }; | 73 enum class FailureMode { SYNC, ASYNC }; |
| 69 | 74 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 100 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"), | 105 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"), |
| 101 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"), | 106 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"), |
| 102 }; | 107 }; |
| 103 | 108 |
| 104 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); | 109 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| 105 std::unique_ptr<ClientSocketHandle> socket_handle = | 110 std::unique_ptr<ClientSocketHandle> socket_handle = |
| 106 CreateConnectedSocketHandle(&data); | 111 CreateConnectedSocketHandle(&data); |
| 107 | 112 |
| 108 ReadErrorUploadDataStream upload_data_stream( | 113 ReadErrorUploadDataStream upload_data_stream( |
| 109 ReadErrorUploadDataStream::FailureMode::SYNC); | 114 ReadErrorUploadDataStream::FailureMode::SYNC); |
| 110 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); | 115 ASSERT_THAT(upload_data_stream.Init(TestCompletionCallback().callback()), |
| 116 IsOk()); |
| 111 | 117 |
| 112 HttpRequestInfo request; | 118 HttpRequestInfo request; |
| 113 request.method = "POST"; | 119 request.method = "POST"; |
| 114 request.url = GURL("http://localhost"); | 120 request.url = GURL("http://localhost"); |
| 115 request.upload_data_stream = &upload_data_stream; | 121 request.upload_data_stream = &upload_data_stream; |
| 116 | 122 |
| 117 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 123 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 118 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), | 124 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), |
| 119 BoundNetLog()); | 125 BoundNetLog()); |
| 120 | 126 |
| 121 HttpRequestHeaders headers; | 127 HttpRequestHeaders headers; |
| 122 headers.SetHeader("Content-Length", "12"); | 128 headers.SetHeader("Content-Length", "12"); |
| 123 | 129 |
| 124 HttpResponseInfo response; | 130 HttpResponseInfo response; |
| 125 TestCompletionCallback callback; | 131 TestCompletionCallback callback; |
| 126 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, | 132 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, |
| 127 callback.callback()); | 133 callback.callback()); |
| 128 EXPECT_EQ(ERR_FAILED, callback.GetResult(result)); | 134 EXPECT_THAT(callback.GetResult(result), IsError(ERR_FAILED)); |
| 129 | 135 |
| 130 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 136 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 131 } | 137 } |
| 132 | 138 |
| 133 TEST(HttpStreamParser, DataReadErrorAsynchronous) { | 139 TEST(HttpStreamParser, DataReadErrorAsynchronous) { |
| 134 MockWrite writes[] = { | 140 MockWrite writes[] = { |
| 135 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"), | 141 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"), |
| 136 MockWrite(ASYNC, 1, "Content-Length: 12\r\n\r\n"), | 142 MockWrite(ASYNC, 1, "Content-Length: 12\r\n\r\n"), |
| 137 }; | 143 }; |
| 138 | 144 |
| 139 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); | 145 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| 140 std::unique_ptr<ClientSocketHandle> socket_handle = | 146 std::unique_ptr<ClientSocketHandle> socket_handle = |
| 141 CreateConnectedSocketHandle(&data); | 147 CreateConnectedSocketHandle(&data); |
| 142 | 148 |
| 143 ReadErrorUploadDataStream upload_data_stream( | 149 ReadErrorUploadDataStream upload_data_stream( |
| 144 ReadErrorUploadDataStream::FailureMode::ASYNC); | 150 ReadErrorUploadDataStream::FailureMode::ASYNC); |
| 145 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); | 151 ASSERT_THAT(upload_data_stream.Init(TestCompletionCallback().callback()), |
| 152 IsOk()); |
| 146 | 153 |
| 147 HttpRequestInfo request; | 154 HttpRequestInfo request; |
| 148 request.method = "POST"; | 155 request.method = "POST"; |
| 149 request.url = GURL("http://localhost"); | 156 request.url = GURL("http://localhost"); |
| 150 request.upload_data_stream = &upload_data_stream; | 157 request.upload_data_stream = &upload_data_stream; |
| 151 | 158 |
| 152 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 159 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 153 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), | 160 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), |
| 154 BoundNetLog()); | 161 BoundNetLog()); |
| 155 | 162 |
| 156 HttpRequestHeaders headers; | 163 HttpRequestHeaders headers; |
| 157 headers.SetHeader("Content-Length", "12"); | 164 headers.SetHeader("Content-Length", "12"); |
| 158 | 165 |
| 159 HttpResponseInfo response; | 166 HttpResponseInfo response; |
| 160 TestCompletionCallback callback; | 167 TestCompletionCallback callback; |
| 161 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, | 168 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, |
| 162 callback.callback()); | 169 callback.callback()); |
| 163 EXPECT_EQ(ERR_IO_PENDING, result); | 170 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 164 | 171 |
| 165 EXPECT_EQ(ERR_FAILED, callback.GetResult(result)); | 172 EXPECT_THAT(callback.GetResult(result), IsError(ERR_FAILED)); |
| 166 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 173 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 167 } | 174 } |
| 168 | 175 |
| 169 // The empty payload is how the last chunk is encoded. | 176 // The empty payload is how the last chunk is encoded. |
| 170 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) { | 177 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) { |
| 171 char output[kOutputSize]; | 178 char output[kOutputSize]; |
| 172 | 179 |
| 173 const base::StringPiece kPayload = ""; | 180 const base::StringPiece kPayload = ""; |
| 174 const base::StringPiece kExpected = "0\r\n\r\n"; | 181 const base::StringPiece kExpected = "0\r\n\r\n"; |
| 175 const int num_bytes_written = | 182 const int num_bytes_written = |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); | 221 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| 215 } | 222 } |
| 216 | 223 |
| 217 TEST(HttpStreamParser, EncodeChunk_TooLargePayload) { | 224 TEST(HttpStreamParser, EncodeChunk_TooLargePayload) { |
| 218 char output[kOutputSize]; | 225 char output[kOutputSize]; |
| 219 | 226 |
| 220 // The payload is one byte larger the output buffer size. | 227 // The payload is one byte larger the output buffer size. |
| 221 const std::string kPayload(kMaxPayloadSize + 1, '\xff'); | 228 const std::string kPayload(kMaxPayloadSize + 1, '\xff'); |
| 222 const int num_bytes_written = | 229 const int num_bytes_written = |
| 223 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); | 230 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| 224 ASSERT_EQ(ERR_INVALID_ARGUMENT, num_bytes_written); | 231 ASSERT_THAT(num_bytes_written, IsError(ERR_INVALID_ARGUMENT)); |
| 225 } | 232 } |
| 226 | 233 |
| 227 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { | 234 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { |
| 228 // Shouldn't be merged if upload data is non-existent. | 235 // Shouldn't be merged if upload data is non-existent. |
| 229 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 236 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 230 "some header", NULL)); | 237 "some header", NULL)); |
| 231 } | 238 } |
| 232 | 239 |
| 233 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { | 240 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { |
| 234 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 241 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 235 std::unique_ptr<UploadDataStream> body(base::WrapUnique( | 242 std::unique_ptr<UploadDataStream> body(base::WrapUnique( |
| 236 new ElementsUploadDataStream(std::move(element_readers), 0))); | 243 new ElementsUploadDataStream(std::move(element_readers), 0))); |
| 237 ASSERT_EQ(OK, body->Init(CompletionCallback())); | 244 ASSERT_THAT(body->Init(CompletionCallback()), IsOk()); |
| 238 // Shouldn't be merged if upload data is empty. | 245 // Shouldn't be merged if upload data is empty. |
| 239 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 246 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 240 "some header", body.get())); | 247 "some header", body.get())); |
| 241 } | 248 } |
| 242 | 249 |
| 243 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { | 250 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { |
| 244 const std::string payload = "123"; | 251 const std::string payload = "123"; |
| 245 std::unique_ptr<ChunkedUploadDataStream> body(new ChunkedUploadDataStream(0)); | 252 std::unique_ptr<ChunkedUploadDataStream> body(new ChunkedUploadDataStream(0)); |
| 246 body->AppendData(payload.data(), payload.size(), true); | 253 body->AppendData(payload.data(), payload.size(), true); |
| 247 ASSERT_EQ(OK, body->Init(TestCompletionCallback().callback())); | 254 ASSERT_THAT(body->Init(TestCompletionCallback().callback()), IsOk()); |
| 248 // Shouldn't be merged if upload data carries chunked data. | 255 // Shouldn't be merged if upload data carries chunked data. |
| 249 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 256 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 250 "some header", body.get())); | 257 "some header", body.get())); |
| 251 } | 258 } |
| 252 | 259 |
| 253 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { | 260 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { |
| 254 // Create an empty temporary file. | 261 // Create an empty temporary file. |
| 255 base::ScopedTempDir temp_dir; | 262 base::ScopedTempDir temp_dir; |
| 256 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 263 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 257 base::FilePath temp_file_path; | 264 base::FilePath temp_file_path; |
| 258 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &temp_file_path)); | 265 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &temp_file_path)); |
| 259 | 266 |
| 260 { | 267 { |
| 261 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 268 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 262 | 269 |
| 263 element_readers.push_back(base::WrapUnique( | 270 element_readers.push_back(base::WrapUnique( |
| 264 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(), | 271 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(), |
| 265 temp_file_path, 0, 0, base::Time()))); | 272 temp_file_path, 0, 0, base::Time()))); |
| 266 | 273 |
| 267 std::unique_ptr<UploadDataStream> body( | 274 std::unique_ptr<UploadDataStream> body( |
| 268 new ElementsUploadDataStream(std::move(element_readers), 0)); | 275 new ElementsUploadDataStream(std::move(element_readers), 0)); |
| 269 TestCompletionCallback callback; | 276 TestCompletionCallback callback; |
| 270 ASSERT_EQ(ERR_IO_PENDING, body->Init(callback.callback())); | 277 ASSERT_THAT(body->Init(callback.callback()), IsError(ERR_IO_PENDING)); |
| 271 ASSERT_EQ(OK, callback.WaitForResult()); | 278 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 272 // Shouldn't be merged if upload data carries a file, as it's not in-memory. | 279 // Shouldn't be merged if upload data carries a file, as it's not in-memory. |
| 273 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 280 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 274 "some header", body.get())); | 281 "some header", body.get())); |
| 275 } | 282 } |
| 276 | 283 |
| 277 // UploadFileElementReaders may post clean-up tasks on destruction. | 284 // UploadFileElementReaders may post clean-up tasks on destruction. |
| 278 base::RunLoop().RunUntilIdle(); | 285 base::RunLoop().RunUntilIdle(); |
| 279 } | 286 } |
| 280 | 287 |
| 281 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { | 288 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { |
| 282 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 289 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 283 const std::string payload = "123"; | 290 const std::string payload = "123"; |
| 284 element_readers.push_back(base::WrapUnique( | 291 element_readers.push_back(base::WrapUnique( |
| 285 new UploadBytesElementReader(payload.data(), payload.size()))); | 292 new UploadBytesElementReader(payload.data(), payload.size()))); |
| 286 | 293 |
| 287 std::unique_ptr<UploadDataStream> body( | 294 std::unique_ptr<UploadDataStream> body( |
| 288 new ElementsUploadDataStream(std::move(element_readers), 0)); | 295 new ElementsUploadDataStream(std::move(element_readers), 0)); |
| 289 ASSERT_EQ(OK, body->Init(CompletionCallback())); | 296 ASSERT_THAT(body->Init(CompletionCallback()), IsOk()); |
| 290 // Yes, should be merged if the in-memory body is small here. | 297 // Yes, should be merged if the in-memory body is small here. |
| 291 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 298 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 292 "some header", body.get())); | 299 "some header", body.get())); |
| 293 } | 300 } |
| 294 | 301 |
| 295 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { | 302 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { |
| 296 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 303 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 297 const std::string payload(10000, 'a'); // 'a' x 10000. | 304 const std::string payload(10000, 'a'); // 'a' x 10000. |
| 298 element_readers.push_back(base::WrapUnique( | 305 element_readers.push_back(base::WrapUnique( |
| 299 new UploadBytesElementReader(payload.data(), payload.size()))); | 306 new UploadBytesElementReader(payload.data(), payload.size()))); |
| 300 | 307 |
| 301 std::unique_ptr<UploadDataStream> body( | 308 std::unique_ptr<UploadDataStream> body( |
| 302 new ElementsUploadDataStream(std::move(element_readers), 0)); | 309 new ElementsUploadDataStream(std::move(element_readers), 0)); |
| 303 ASSERT_EQ(OK, body->Init(CompletionCallback())); | 310 ASSERT_THAT(body->Init(CompletionCallback()), IsOk()); |
| 304 // Shouldn't be merged if the in-memory body is large here. | 311 // Shouldn't be merged if the in-memory body is large here. |
| 305 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 312 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 306 "some header", body.get())); | 313 "some header", body.get())); |
| 307 } | 314 } |
| 308 | 315 |
| 309 TEST(HttpStreamParser, SentBytesNoHeaders) { | 316 TEST(HttpStreamParser, SentBytesNoHeaders) { |
| 310 MockWrite writes[] = { | 317 MockWrite writes[] = { |
| 311 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"), | 318 MockWrite(SYNCHRONOUS, 0, "GET / HTTP/1.1\r\n\r\n"), |
| 312 }; | 319 }; |
| 313 | 320 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 }; | 442 }; |
| 436 | 443 |
| 437 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); | 444 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| 438 std::unique_ptr<ClientSocketHandle> socket_handle = | 445 std::unique_ptr<ClientSocketHandle> socket_handle = |
| 439 CreateConnectedSocketHandle(&data); | 446 CreateConnectedSocketHandle(&data); |
| 440 | 447 |
| 441 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | 448 std::vector<std::unique_ptr<UploadElementReader>> element_readers; |
| 442 element_readers.push_back( | 449 element_readers.push_back( |
| 443 base::WrapUnique(new UploadBytesElementReader("hello world!", 12))); | 450 base::WrapUnique(new UploadBytesElementReader("hello world!", 12))); |
| 444 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); | 451 ElementsUploadDataStream upload_data_stream(std::move(element_readers), 0); |
| 445 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); | 452 ASSERT_THAT(upload_data_stream.Init(TestCompletionCallback().callback()), |
| 453 IsOk()); |
| 446 | 454 |
| 447 HttpRequestInfo request; | 455 HttpRequestInfo request; |
| 448 request.method = "POST"; | 456 request.method = "POST"; |
| 449 request.url = GURL("http://localhost"); | 457 request.url = GURL("http://localhost"); |
| 450 request.upload_data_stream = &upload_data_stream; | 458 request.upload_data_stream = &upload_data_stream; |
| 451 | 459 |
| 452 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 460 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 453 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), | 461 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), |
| 454 BoundNetLog()); | 462 BoundNetLog()); |
| 455 | 463 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 472 MockWrite(ASYNC, 1, "Transfer-Encoding: chunked\r\n\r\n"), | 480 MockWrite(ASYNC, 1, "Transfer-Encoding: chunked\r\n\r\n"), |
| 473 MockWrite(ASYNC, 2, "7\r\nChunk 1\r\n"), | 481 MockWrite(ASYNC, 2, "7\r\nChunk 1\r\n"), |
| 474 MockWrite(SYNCHRONOUS, ERR_FAILED, 3), | 482 MockWrite(SYNCHRONOUS, ERR_FAILED, 3), |
| 475 }; | 483 }; |
| 476 | 484 |
| 477 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); | 485 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| 478 std::unique_ptr<ClientSocketHandle> socket_handle = | 486 std::unique_ptr<ClientSocketHandle> socket_handle = |
| 479 CreateConnectedSocketHandle(&data); | 487 CreateConnectedSocketHandle(&data); |
| 480 | 488 |
| 481 ChunkedUploadDataStream upload_data_stream(0); | 489 ChunkedUploadDataStream upload_data_stream(0); |
| 482 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); | 490 ASSERT_THAT(upload_data_stream.Init(TestCompletionCallback().callback()), |
| 491 IsOk()); |
| 483 | 492 |
| 484 HttpRequestInfo request; | 493 HttpRequestInfo request; |
| 485 request.method = "POST"; | 494 request.method = "POST"; |
| 486 request.url = GURL("http://localhost"); | 495 request.url = GURL("http://localhost"); |
| 487 request.upload_data_stream = &upload_data_stream; | 496 request.upload_data_stream = &upload_data_stream; |
| 488 | 497 |
| 489 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 498 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 490 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), | 499 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), |
| 491 BoundNetLog()); | 500 BoundNetLog()); |
| 492 | 501 |
| 493 HttpRequestHeaders headers; | 502 HttpRequestHeaders headers; |
| 494 headers.SetHeader("Transfer-Encoding", "chunked"); | 503 headers.SetHeader("Transfer-Encoding", "chunked"); |
| 495 | 504 |
| 496 HttpResponseInfo response; | 505 HttpResponseInfo response; |
| 497 TestCompletionCallback callback; | 506 TestCompletionCallback callback; |
| 498 EXPECT_EQ(ERR_IO_PENDING, parser.SendRequest("POST / HTTP/1.1\r\n", headers, | 507 EXPECT_EQ(ERR_IO_PENDING, parser.SendRequest("POST / HTTP/1.1\r\n", headers, |
| 499 &response, callback.callback())); | 508 &response, callback.callback())); |
| 500 | 509 |
| 501 base::RunLoop().RunUntilIdle(); | 510 base::RunLoop().RunUntilIdle(); |
| 502 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false); | 511 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false); |
| 503 | 512 |
| 504 base::RunLoop().RunUntilIdle(); | 513 base::RunLoop().RunUntilIdle(); |
| 505 // This write should fail. | 514 // This write should fail. |
| 506 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false); | 515 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false); |
| 507 EXPECT_EQ(ERR_FAILED, callback.WaitForResult()); | 516 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_FAILED)); |
| 508 | 517 |
| 509 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 518 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 510 } | 519 } |
| 511 | 520 |
| 512 // Test to ensure the HttpStreamParser state machine does not get confused | 521 // Test to ensure the HttpStreamParser state machine does not get confused |
| 513 // when sending a request with a chunked body with only one chunk that becomes | 522 // when sending a request with a chunked body with only one chunk that becomes |
| 514 // available asynchronously. | 523 // available asynchronously. |
| 515 TEST(HttpStreamParser, AsyncSingleChunkAndAsyncSocket) { | 524 TEST(HttpStreamParser, AsyncSingleChunkAndAsyncSocket) { |
| 516 static const char kChunk[] = "Chunk"; | 525 static const char kChunk[] = "Chunk"; |
| 517 | 526 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 528 static const int kBodySize = 8; | 537 static const int kBodySize = 8; |
| 529 | 538 |
| 530 MockRead reads[] = { | 539 MockRead reads[] = { |
| 531 MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n"), | 540 MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n"), |
| 532 MockRead(ASYNC, 4, "Content-Length: 8\r\n\r\n"), | 541 MockRead(ASYNC, 4, "Content-Length: 8\r\n\r\n"), |
| 533 MockRead(ASYNC, 5, "one.html"), | 542 MockRead(ASYNC, 5, "one.html"), |
| 534 MockRead(SYNCHRONOUS, 0, 6), // EOF | 543 MockRead(SYNCHRONOUS, 0, 6), // EOF |
| 535 }; | 544 }; |
| 536 | 545 |
| 537 ChunkedUploadDataStream upload_stream(0); | 546 ChunkedUploadDataStream upload_stream(0); |
| 538 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback())); | 547 ASSERT_THAT(upload_stream.Init(TestCompletionCallback().callback()), IsOk()); |
| 539 | 548 |
| 540 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); | 549 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 541 std::unique_ptr<ClientSocketHandle> socket_handle = | 550 std::unique_ptr<ClientSocketHandle> socket_handle = |
| 542 CreateConnectedSocketHandle(&data); | 551 CreateConnectedSocketHandle(&data); |
| 543 | 552 |
| 544 HttpRequestInfo request_info; | 553 HttpRequestInfo request_info; |
| 545 request_info.method = "GET"; | 554 request_info.method = "GET"; |
| 546 request_info.url = GURL("http://localhost"); | 555 request_info.url = GURL("http://localhost"); |
| 547 request_info.upload_data_stream = &upload_stream; | 556 request_info.upload_data_stream = &upload_stream; |
| 548 | 557 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 560 ASSERT_EQ(ERR_IO_PENDING, | 569 ASSERT_EQ(ERR_IO_PENDING, |
| 561 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, | 570 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, |
| 562 &response_info, callback.callback())); | 571 &response_info, callback.callback())); |
| 563 | 572 |
| 564 // Complete the initial request write. Callback should not have been invoked. | 573 // Complete the initial request write. Callback should not have been invoked. |
| 565 base::RunLoop().RunUntilIdle(); | 574 base::RunLoop().RunUntilIdle(); |
| 566 ASSERT_FALSE(callback.have_result()); | 575 ASSERT_FALSE(callback.have_result()); |
| 567 | 576 |
| 568 // Now append the only chunk and wait for the callback. | 577 // Now append the only chunk and wait for the callback. |
| 569 upload_stream.AppendData(kChunk, arraysize(kChunk) - 1, true); | 578 upload_stream.AppendData(kChunk, arraysize(kChunk) - 1, true); |
| 570 ASSERT_EQ(OK, callback.WaitForResult()); | 579 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 571 | 580 |
| 572 // Attempt to read the response status and the response headers. | 581 // Attempt to read the response status and the response headers. |
| 573 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback())); | 582 ASSERT_THAT(parser.ReadResponseHeaders(callback.callback()), |
| 574 ASSERT_EQ(OK, callback.WaitForResult()); | 583 IsError(ERR_IO_PENDING)); |
| 584 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 575 | 585 |
| 576 // Finally, attempt to read the response body. | 586 // Finally, attempt to read the response body. |
| 577 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); | 587 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); |
| 578 ASSERT_EQ(ERR_IO_PENDING, | 588 ASSERT_EQ(ERR_IO_PENDING, |
| 579 parser.ReadResponseBody(body_buffer.get(), kBodySize, | 589 parser.ReadResponseBody(body_buffer.get(), kBodySize, |
| 580 callback.callback())); | 590 callback.callback())); |
| 581 ASSERT_EQ(kBodySize, callback.WaitForResult()); | 591 ASSERT_EQ(kBodySize, callback.WaitForResult()); |
| 582 | 592 |
| 583 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 593 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 584 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 594 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 603 static const int kBodySize = 8; | 613 static const int kBodySize = 8; |
| 604 | 614 |
| 605 MockRead reads[] = { | 615 MockRead reads[] = { |
| 606 MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n"), | 616 MockRead(ASYNC, 3, "HTTP/1.1 200 OK\r\n"), |
| 607 MockRead(ASYNC, 4, "Content-Length: 8\r\n\r\n"), | 617 MockRead(ASYNC, 4, "Content-Length: 8\r\n\r\n"), |
| 608 MockRead(ASYNC, 5, "one.html"), | 618 MockRead(ASYNC, 5, "one.html"), |
| 609 MockRead(SYNCHRONOUS, 0, 6), // EOF | 619 MockRead(SYNCHRONOUS, 0, 6), // EOF |
| 610 }; | 620 }; |
| 611 | 621 |
| 612 ChunkedUploadDataStream upload_stream(0); | 622 ChunkedUploadDataStream upload_stream(0); |
| 613 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback())); | 623 ASSERT_THAT(upload_stream.Init(TestCompletionCallback().callback()), IsOk()); |
| 614 // Append the only chunk. | 624 // Append the only chunk. |
| 615 upload_stream.AppendData(kChunk, arraysize(kChunk) - 1, true); | 625 upload_stream.AppendData(kChunk, arraysize(kChunk) - 1, true); |
| 616 | 626 |
| 617 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); | 627 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 618 std::unique_ptr<ClientSocketHandle> socket_handle = | 628 std::unique_ptr<ClientSocketHandle> socket_handle = |
| 619 CreateConnectedSocketHandle(&data); | 629 CreateConnectedSocketHandle(&data); |
| 620 | 630 |
| 621 HttpRequestInfo request_info; | 631 HttpRequestInfo request_info; |
| 622 request_info.method = "GET"; | 632 request_info.method = "GET"; |
| 623 request_info.url = GURL("http://localhost"); | 633 request_info.url = GURL("http://localhost"); |
| 624 request_info.upload_data_stream = &upload_stream; | 634 request_info.upload_data_stream = &upload_stream; |
| 625 | 635 |
| 626 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 636 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 627 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(), | 637 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(), |
| 628 BoundNetLog()); | 638 BoundNetLog()); |
| 629 | 639 |
| 630 HttpRequestHeaders request_headers; | 640 HttpRequestHeaders request_headers; |
| 631 request_headers.SetHeader("Transfer-Encoding", "chunked"); | 641 request_headers.SetHeader("Transfer-Encoding", "chunked"); |
| 632 | 642 |
| 633 HttpResponseInfo response_info; | 643 HttpResponseInfo response_info; |
| 634 TestCompletionCallback callback; | 644 TestCompletionCallback callback; |
| 635 // This will attempt to Write() the initial request and headers, which will | 645 // This will attempt to Write() the initial request and headers, which will |
| 636 // complete asynchronously. | 646 // complete asynchronously. |
| 637 ASSERT_EQ(ERR_IO_PENDING, | 647 ASSERT_EQ(ERR_IO_PENDING, |
| 638 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, | 648 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, |
| 639 &response_info, callback.callback())); | 649 &response_info, callback.callback())); |
| 640 ASSERT_EQ(OK, callback.WaitForResult()); | 650 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 641 | 651 |
| 642 // Attempt to read the response status and the response headers. | 652 // Attempt to read the response status and the response headers. |
| 643 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback())); | 653 ASSERT_THAT(parser.ReadResponseHeaders(callback.callback()), |
| 644 ASSERT_EQ(OK, callback.WaitForResult()); | 654 IsError(ERR_IO_PENDING)); |
| 655 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 645 | 656 |
| 646 // Finally, attempt to read the response body. | 657 // Finally, attempt to read the response body. |
| 647 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); | 658 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); |
| 648 ASSERT_EQ(ERR_IO_PENDING, | 659 ASSERT_EQ(ERR_IO_PENDING, |
| 649 parser.ReadResponseBody(body_buffer.get(), kBodySize, | 660 parser.ReadResponseBody(body_buffer.get(), kBodySize, |
| 650 callback.callback())); | 661 callback.callback())); |
| 651 ASSERT_EQ(kBodySize, callback.WaitForResult()); | 662 ASSERT_EQ(kBodySize, callback.WaitForResult()); |
| 652 | 663 |
| 653 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 664 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 654 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 665 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 682 | 693 |
| 683 MockRead reads[] = { | 694 MockRead reads[] = { |
| 684 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\n"), | 695 MockRead(ASYNC, 5, "HTTP/1.1 200 OK\r\n"), |
| 685 MockRead(ASYNC, 6, "Content-Length: 8\r\n\r\n"), | 696 MockRead(ASYNC, 6, "Content-Length: 8\r\n\r\n"), |
| 686 MockRead(ASYNC, 7, "one.html"), | 697 MockRead(ASYNC, 7, "one.html"), |
| 687 MockRead(SYNCHRONOUS, 0, 8), // EOF | 698 MockRead(SYNCHRONOUS, 0, 8), // EOF |
| 688 }; | 699 }; |
| 689 | 700 |
| 690 ChunkedUploadDataStream upload_stream(0); | 701 ChunkedUploadDataStream upload_stream(0); |
| 691 upload_stream.AppendData(kChunk1, arraysize(kChunk1) - 1, false); | 702 upload_stream.AppendData(kChunk1, arraysize(kChunk1) - 1, false); |
| 692 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback())); | 703 ASSERT_THAT(upload_stream.Init(TestCompletionCallback().callback()), IsOk()); |
| 693 | 704 |
| 694 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); | 705 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 695 std::unique_ptr<ClientSocketHandle> socket_handle = | 706 std::unique_ptr<ClientSocketHandle> socket_handle = |
| 696 CreateConnectedSocketHandle(&data); | 707 CreateConnectedSocketHandle(&data); |
| 697 | 708 |
| 698 HttpRequestInfo request_info; | 709 HttpRequestInfo request_info; |
| 699 request_info.method = "GET"; | 710 request_info.method = "GET"; |
| 700 request_info.url = GURL("http://localhost"); | 711 request_info.url = GURL("http://localhost"); |
| 701 request_info.upload_data_stream = &upload_stream; | 712 request_info.upload_data_stream = &upload_stream; |
| 702 | 713 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 723 // Now append another chunk. | 734 // Now append another chunk. |
| 724 upload_stream.AppendData(kChunk2, arraysize(kChunk2) - 1, false); | 735 upload_stream.AppendData(kChunk2, arraysize(kChunk2) - 1, false); |
| 725 ASSERT_FALSE(callback.have_result()); | 736 ASSERT_FALSE(callback.have_result()); |
| 726 | 737 |
| 727 // Add the final chunk, while the write for the second is still pending, | 738 // Add the final chunk, while the write for the second is still pending, |
| 728 // which should not confuse the state machine. | 739 // which should not confuse the state machine. |
| 729 upload_stream.AppendData(kChunk3, arraysize(kChunk3) - 1, true); | 740 upload_stream.AppendData(kChunk3, arraysize(kChunk3) - 1, true); |
| 730 ASSERT_FALSE(callback.have_result()); | 741 ASSERT_FALSE(callback.have_result()); |
| 731 | 742 |
| 732 // Wait for writes to complete. | 743 // Wait for writes to complete. |
| 733 ASSERT_EQ(OK, callback.WaitForResult()); | 744 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 734 | 745 |
| 735 // Attempt to read the response status and the response headers. | 746 // Attempt to read the response status and the response headers. |
| 736 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback())); | 747 ASSERT_THAT(parser.ReadResponseHeaders(callback.callback()), |
| 737 ASSERT_EQ(OK, callback.WaitForResult()); | 748 IsError(ERR_IO_PENDING)); |
| 749 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 738 | 750 |
| 739 // Finally, attempt to read the response body. | 751 // Finally, attempt to read the response body. |
| 740 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); | 752 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); |
| 741 ASSERT_EQ(ERR_IO_PENDING, | 753 ASSERT_EQ(ERR_IO_PENDING, |
| 742 parser.ReadResponseBody(body_buffer.get(), kBodySize, | 754 parser.ReadResponseBody(body_buffer.get(), kBodySize, |
| 743 callback.callback())); | 755 callback.callback())); |
| 744 ASSERT_EQ(kBodySize, callback.WaitForResult()); | 756 ASSERT_EQ(kBodySize, callback.WaitForResult()); |
| 745 | 757 |
| 746 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 758 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 747 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 759 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 763 const int kBodySize = 8; | 775 const int kBodySize = 8; |
| 764 | 776 |
| 765 MockRead reads[] = { | 777 MockRead reads[] = { |
| 766 MockRead(ASYNC, 2, "HTTP/1.1 200 OK\r\n"), | 778 MockRead(ASYNC, 2, "HTTP/1.1 200 OK\r\n"), |
| 767 MockRead(ASYNC, 3, "Content-Length: 8\r\n\r\n"), | 779 MockRead(ASYNC, 3, "Content-Length: 8\r\n\r\n"), |
| 768 MockRead(ASYNC, 4, "one.html"), | 780 MockRead(ASYNC, 4, "one.html"), |
| 769 MockRead(SYNCHRONOUS, 0, 5), // EOF | 781 MockRead(SYNCHRONOUS, 0, 5), // EOF |
| 770 }; | 782 }; |
| 771 | 783 |
| 772 ChunkedUploadDataStream upload_stream(0); | 784 ChunkedUploadDataStream upload_stream(0); |
| 773 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback())); | 785 ASSERT_THAT(upload_stream.Init(TestCompletionCallback().callback()), IsOk()); |
| 774 | 786 |
| 775 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); | 787 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 776 std::unique_ptr<ClientSocketHandle> socket_handle = | 788 std::unique_ptr<ClientSocketHandle> socket_handle = |
| 777 CreateConnectedSocketHandle(&data); | 789 CreateConnectedSocketHandle(&data); |
| 778 | 790 |
| 779 HttpRequestInfo request_info; | 791 HttpRequestInfo request_info; |
| 780 request_info.method = "GET"; | 792 request_info.method = "GET"; |
| 781 request_info.url = GURL("http://localhost"); | 793 request_info.url = GURL("http://localhost"); |
| 782 request_info.upload_data_stream = &upload_stream; | 794 request_info.upload_data_stream = &upload_stream; |
| 783 | 795 |
| 784 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 796 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 785 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(), | 797 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(), |
| 786 BoundNetLog()); | 798 BoundNetLog()); |
| 787 | 799 |
| 788 HttpRequestHeaders request_headers; | 800 HttpRequestHeaders request_headers; |
| 789 request_headers.SetHeader("Transfer-Encoding", "chunked"); | 801 request_headers.SetHeader("Transfer-Encoding", "chunked"); |
| 790 | 802 |
| 791 HttpResponseInfo response_info; | 803 HttpResponseInfo response_info; |
| 792 TestCompletionCallback callback; | 804 TestCompletionCallback callback; |
| 793 // This will attempt to Write() the initial request and headers, which will | 805 // This will attempt to Write() the initial request and headers, which will |
| 794 // complete asynchronously. | 806 // complete asynchronously. |
| 795 ASSERT_EQ(ERR_IO_PENDING, | 807 ASSERT_EQ(ERR_IO_PENDING, |
| 796 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, | 808 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, |
| 797 &response_info, callback.callback())); | 809 &response_info, callback.callback())); |
| 798 | 810 |
| 799 // Now append the terminal 0-byte "chunk". | 811 // Now append the terminal 0-byte "chunk". |
| 800 upload_stream.AppendData(nullptr, 0, true); | 812 upload_stream.AppendData(nullptr, 0, true); |
| 801 ASSERT_FALSE(callback.have_result()); | 813 ASSERT_FALSE(callback.have_result()); |
| 802 | 814 |
| 803 ASSERT_EQ(OK, callback.WaitForResult()); | 815 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 804 | 816 |
| 805 // Attempt to read the response status and the response headers. | 817 // Attempt to read the response status and the response headers. |
| 806 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback())); | 818 ASSERT_THAT(parser.ReadResponseHeaders(callback.callback()), |
| 807 ASSERT_EQ(OK, callback.WaitForResult()); | 819 IsError(ERR_IO_PENDING)); |
| 820 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 808 | 821 |
| 809 // Finally, attempt to read the response body. | 822 // Finally, attempt to read the response body. |
| 810 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); | 823 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); |
| 811 ASSERT_EQ(ERR_IO_PENDING, | 824 ASSERT_EQ(ERR_IO_PENDING, |
| 812 parser.ReadResponseBody(body_buffer.get(), kBodySize, | 825 parser.ReadResponseBody(body_buffer.get(), kBodySize, |
| 813 callback.callback())); | 826 callback.callback())); |
| 814 ASSERT_EQ(kBodySize, callback.WaitForResult()); | 827 ASSERT_EQ(kBodySize, callback.WaitForResult()); |
| 815 | 828 |
| 816 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 829 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 817 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 830 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 833 const int kBodySize = 8; | 846 const int kBodySize = 8; |
| 834 | 847 |
| 835 MockRead reads[] = { | 848 MockRead reads[] = { |
| 836 MockRead(ASYNC, 2, "HTTP/1.1 200 OK\r\n"), | 849 MockRead(ASYNC, 2, "HTTP/1.1 200 OK\r\n"), |
| 837 MockRead(ASYNC, 3, "Content-Length: 8\r\n\r\n"), | 850 MockRead(ASYNC, 3, "Content-Length: 8\r\n\r\n"), |
| 838 MockRead(ASYNC, 4, "one.html"), | 851 MockRead(ASYNC, 4, "one.html"), |
| 839 MockRead(SYNCHRONOUS, 0, 5), // EOF | 852 MockRead(SYNCHRONOUS, 0, 5), // EOF |
| 840 }; | 853 }; |
| 841 | 854 |
| 842 ChunkedUploadDataStream upload_stream(0); | 855 ChunkedUploadDataStream upload_stream(0); |
| 843 ASSERT_EQ(OK, upload_stream.Init(TestCompletionCallback().callback())); | 856 ASSERT_THAT(upload_stream.Init(TestCompletionCallback().callback()), IsOk()); |
| 844 // Append final empty chunk. | 857 // Append final empty chunk. |
| 845 upload_stream.AppendData(nullptr, 0, true); | 858 upload_stream.AppendData(nullptr, 0, true); |
| 846 | 859 |
| 847 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); | 860 SequencedSocketData data(reads, arraysize(reads), writes, arraysize(writes)); |
| 848 std::unique_ptr<ClientSocketHandle> socket_handle = | 861 std::unique_ptr<ClientSocketHandle> socket_handle = |
| 849 CreateConnectedSocketHandle(&data); | 862 CreateConnectedSocketHandle(&data); |
| 850 | 863 |
| 851 HttpRequestInfo request_info; | 864 HttpRequestInfo request_info; |
| 852 request_info.method = "GET"; | 865 request_info.method = "GET"; |
| 853 request_info.url = GURL("http://localhost"); | 866 request_info.url = GURL("http://localhost"); |
| 854 request_info.upload_data_stream = &upload_stream; | 867 request_info.upload_data_stream = &upload_stream; |
| 855 | 868 |
| 856 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 869 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 857 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(), | 870 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer.get(), |
| 858 BoundNetLog()); | 871 BoundNetLog()); |
| 859 | 872 |
| 860 HttpRequestHeaders request_headers; | 873 HttpRequestHeaders request_headers; |
| 861 request_headers.SetHeader("Transfer-Encoding", "chunked"); | 874 request_headers.SetHeader("Transfer-Encoding", "chunked"); |
| 862 | 875 |
| 863 HttpResponseInfo response_info; | 876 HttpResponseInfo response_info; |
| 864 TestCompletionCallback callback; | 877 TestCompletionCallback callback; |
| 865 // This will attempt to Write() the initial request and headers, which will | 878 // This will attempt to Write() the initial request and headers, which will |
| 866 // complete asynchronously. | 879 // complete asynchronously. |
| 867 ASSERT_EQ(ERR_IO_PENDING, | 880 ASSERT_EQ(ERR_IO_PENDING, |
| 868 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, | 881 parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, |
| 869 &response_info, callback.callback())); | 882 &response_info, callback.callback())); |
| 870 | 883 |
| 871 // Complete writing the request headers and body. | 884 // Complete writing the request headers and body. |
| 872 ASSERT_EQ(OK, callback.WaitForResult()); | 885 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 873 | 886 |
| 874 // Attempt to read the response status and the response headers. | 887 // Attempt to read the response status and the response headers. |
| 875 ASSERT_EQ(ERR_IO_PENDING, parser.ReadResponseHeaders(callback.callback())); | 888 ASSERT_THAT(parser.ReadResponseHeaders(callback.callback()), |
| 876 ASSERT_EQ(OK, callback.WaitForResult()); | 889 IsError(ERR_IO_PENDING)); |
| 890 ASSERT_THAT(callback.WaitForResult(), IsOk()); |
| 877 | 891 |
| 878 // Finally, attempt to read the response body. | 892 // Finally, attempt to read the response body. |
| 879 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); | 893 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); |
| 880 ASSERT_EQ(ERR_IO_PENDING, | 894 ASSERT_EQ(ERR_IO_PENDING, |
| 881 parser.ReadResponseBody(body_buffer.get(), kBodySize, | 895 parser.ReadResponseBody(body_buffer.get(), kBodySize, |
| 882 callback.callback())); | 896 callback.callback())); |
| 883 ASSERT_EQ(kBodySize, callback.WaitForResult()); | 897 ASSERT_EQ(kBodySize, callback.WaitForResult()); |
| 884 | 898 |
| 885 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 899 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 886 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 900 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 961 HttpRequestHeaders request_headers; | 975 HttpRequestHeaders request_headers; |
| 962 HttpResponseInfo response_info; | 976 HttpResponseInfo response_info; |
| 963 TestCompletionCallback callback; | 977 TestCompletionCallback callback; |
| 964 ASSERT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", request_headers, | 978 ASSERT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", request_headers, |
| 965 &response_info, callback.callback())); | 979 &response_info, callback.callback())); |
| 966 | 980 |
| 967 int rv = parser.ReadResponseHeaders(callback.callback()); | 981 int rv = parser.ReadResponseHeaders(callback.callback()); |
| 968 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), | 982 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
| 969 parser.sent_bytes()); | 983 parser.sent_bytes()); |
| 970 if (i == arraysize(reads) - 1) { | 984 if (i == arraysize(reads) - 1) { |
| 971 EXPECT_EQ(OK, rv); | 985 EXPECT_THAT(rv, IsOk()); |
| 972 EXPECT_TRUE(response_info.headers.get()); | 986 EXPECT_TRUE(response_info.headers.get()); |
| 973 EXPECT_EQ(CountReadBytes(reads[i], 2), parser.received_bytes()); | 987 EXPECT_EQ(CountReadBytes(reads[i], 2), parser.received_bytes()); |
| 974 } else { | 988 } else { |
| 975 if (protocol == HTTP) { | 989 if (protocol == HTTP) { |
| 976 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); | 990 EXPECT_THAT(rv, IsError(ERR_CONNECTION_CLOSED)); |
| 977 EXPECT_TRUE(response_info.headers.get()); | 991 EXPECT_TRUE(response_info.headers.get()); |
| 978 EXPECT_EQ(CountReadBytes(reads[i], 2), parser.received_bytes()); | 992 EXPECT_EQ(CountReadBytes(reads[i], 2), parser.received_bytes()); |
| 979 } else { | 993 } else { |
| 980 EXPECT_EQ(ERR_RESPONSE_HEADERS_TRUNCATED, rv); | 994 EXPECT_THAT(rv, IsError(ERR_RESPONSE_HEADERS_TRUNCATED)); |
| 981 EXPECT_FALSE(response_info.headers.get()); | 995 EXPECT_FALSE(response_info.headers.get()); |
| 982 EXPECT_EQ(0, parser.received_bytes()); | 996 EXPECT_EQ(0, parser.received_bytes()); |
| 983 } | 997 } |
| 984 } | 998 } |
| 985 } | 999 } |
| 986 } | 1000 } |
| 987 } | 1001 } |
| 988 | 1002 |
| 989 // Confirm that on 101 response, the headers are parsed but the data that | 1003 // Confirm that on 101 response, the headers are parsed but the data that |
| 990 // follows remains in the buffer. | 1004 // follows remains in the buffer. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1014 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 1028 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 1015 HttpStreamParser parser( | 1029 HttpStreamParser parser( |
| 1016 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog()); | 1030 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog()); |
| 1017 | 1031 |
| 1018 HttpRequestHeaders request_headers; | 1032 HttpRequestHeaders request_headers; |
| 1019 HttpResponseInfo response_info; | 1033 HttpResponseInfo response_info; |
| 1020 TestCompletionCallback callback; | 1034 TestCompletionCallback callback; |
| 1021 ASSERT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", request_headers, | 1035 ASSERT_EQ(OK, parser.SendRequest("GET / HTTP/1.1\r\n", request_headers, |
| 1022 &response_info, callback.callback())); | 1036 &response_info, callback.callback())); |
| 1023 | 1037 |
| 1024 EXPECT_EQ(OK, parser.ReadResponseHeaders(callback.callback())); | 1038 EXPECT_THAT(parser.ReadResponseHeaders(callback.callback()), IsOk()); |
| 1025 ASSERT_TRUE(response_info.headers.get()); | 1039 ASSERT_TRUE(response_info.headers.get()); |
| 1026 EXPECT_EQ(101, response_info.headers->response_code()); | 1040 EXPECT_EQ(101, response_info.headers->response_code()); |
| 1027 EXPECT_TRUE(response_info.headers->HasHeaderValue("Connection", "Upgrade")); | 1041 EXPECT_TRUE(response_info.headers->HasHeaderValue("Connection", "Upgrade")); |
| 1028 EXPECT_TRUE(response_info.headers->HasHeaderValue("Upgrade", "websocket")); | 1042 EXPECT_TRUE(response_info.headers->HasHeaderValue("Upgrade", "websocket")); |
| 1029 EXPECT_EQ(read_buffer->capacity(), read_buffer->offset()); | 1043 EXPECT_EQ(read_buffer->capacity(), read_buffer->offset()); |
| 1030 EXPECT_EQ("a fake websocket frame", | 1044 EXPECT_EQ("a fake websocket frame", |
| 1031 base::StringPiece(read_buffer->StartOfBuffer(), | 1045 base::StringPiece(read_buffer->StartOfBuffer(), |
| 1032 read_buffer->capacity())); | 1046 read_buffer->capacity())); |
| 1033 | 1047 |
| 1034 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 1048 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1075 parser_.reset(new HttpStreamParser( | 1089 parser_.reset(new HttpStreamParser( |
| 1076 socket_handle_.get(), &request_info_, read_buffer(), BoundNetLog())); | 1090 socket_handle_.get(), &request_info_, read_buffer(), BoundNetLog())); |
| 1077 | 1091 |
| 1078 TestCompletionCallback callback; | 1092 TestCompletionCallback callback; |
| 1079 ASSERT_EQ(OK, parser_->SendRequest("GET / HTTP/1.1\r\n", request_headers_, | 1093 ASSERT_EQ(OK, parser_->SendRequest("GET / HTTP/1.1\r\n", request_headers_, |
| 1080 &response_info_, callback.callback())); | 1094 &response_info_, callback.callback())); |
| 1081 } | 1095 } |
| 1082 | 1096 |
| 1083 void ReadHeaders() { | 1097 void ReadHeaders() { |
| 1084 TestCompletionCallback callback; | 1098 TestCompletionCallback callback; |
| 1085 EXPECT_EQ(OK, parser_->ReadResponseHeaders(callback.callback())); | 1099 EXPECT_THAT(parser_->ReadResponseHeaders(callback.callback()), IsOk()); |
| 1086 } | 1100 } |
| 1087 | 1101 |
| 1088 void ReadBody(int user_buf_len, int* read_lengths) { | 1102 void ReadBody(int user_buf_len, int* read_lengths) { |
| 1089 TestCompletionCallback callback; | 1103 TestCompletionCallback callback; |
| 1090 scoped_refptr<IOBuffer> buffer = new IOBuffer(user_buf_len); | 1104 scoped_refptr<IOBuffer> buffer = new IOBuffer(user_buf_len); |
| 1091 int rv; | 1105 int rv; |
| 1092 int i = 0; | 1106 int i = 0; |
| 1093 while (true) { | 1107 while (true) { |
| 1094 rv = parser_->ReadResponseBody( | 1108 rv = parser_->ReadResponseBody( |
| 1095 buffer.get(), user_buf_len, callback.callback()); | 1109 buffer.get(), user_buf_len, callback.callback()); |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1368 | 1382 |
| 1369 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 1383 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| 1370 HttpStreamParser parser(socket_handle.get(), request_info.get(), | 1384 HttpStreamParser parser(socket_handle.get(), request_info.get(), |
| 1371 read_buffer.get(), BoundNetLog()); | 1385 read_buffer.get(), BoundNetLog()); |
| 1372 | 1386 |
| 1373 std::unique_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders()); | 1387 std::unique_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders()); |
| 1374 std::unique_ptr<HttpResponseInfo> response_info(new HttpResponseInfo()); | 1388 std::unique_ptr<HttpResponseInfo> response_info(new HttpResponseInfo()); |
| 1375 TestCompletionCallback callback; | 1389 TestCompletionCallback callback; |
| 1376 ASSERT_EQ(OK, parser.SendRequest("GET /foo.html HTTP/1.1\r\n", | 1390 ASSERT_EQ(OK, parser.SendRequest("GET /foo.html HTTP/1.1\r\n", |
| 1377 *request_headers, response_info.get(), callback.callback())); | 1391 *request_headers, response_info.get(), callback.callback())); |
| 1378 ASSERT_EQ(OK, parser.ReadResponseHeaders(callback.callback())); | 1392 ASSERT_THAT(parser.ReadResponseHeaders(callback.callback()), IsOk()); |
| 1379 | 1393 |
| 1380 // If the object that owns the HttpStreamParser is deleted, it takes the | 1394 // If the object that owns the HttpStreamParser is deleted, it takes the |
| 1381 // objects passed to the HttpStreamParser with it. | 1395 // objects passed to the HttpStreamParser with it. |
| 1382 request_info.reset(); | 1396 request_info.reset(); |
| 1383 request_headers.reset(); | 1397 request_headers.reset(); |
| 1384 response_info.reset(); | 1398 response_info.reset(); |
| 1385 | 1399 |
| 1386 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); | 1400 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); |
| 1387 ASSERT_EQ(kBodySize, parser.ReadResponseBody( | 1401 ASSERT_EQ(kBodySize, parser.ReadResponseBody( |
| 1388 body_buffer.get(), kBodySize, callback.callback())); | 1402 body_buffer.get(), kBodySize, callback.callback())); |
| 1389 | 1403 |
| 1390 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 1404 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 1391 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 1405 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
| 1392 } | 1406 } |
| 1393 | 1407 |
| 1394 } // namespace | 1408 } // namespace |
| 1395 | 1409 |
| 1396 } // namespace net | 1410 } // namespace net |
| OLD | NEW |