Chromium Code Reviews| 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 28 matching lines...) Expand all Loading... | |
| 39 | 39 |
| 40 namespace net { | 40 namespace net { |
| 41 | 41 |
| 42 namespace { | 42 namespace { |
| 43 | 43 |
| 44 const size_t kOutputSize = 1024; // Just large enough for this test. | 44 const size_t kOutputSize = 1024; // Just large enough for this test. |
| 45 // The number of bytes that can fit in a buffer of kOutputSize. | 45 // The number of bytes that can fit in a buffer of kOutputSize. |
| 46 const size_t kMaxPayloadSize = | 46 const size_t kMaxPayloadSize = |
| 47 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize; | 47 kOutputSize - HttpStreamParser::kChunkHeaderFooterSize; |
| 48 | 48 |
| 49 const bool kAsynchronously = true; | |
|
mmenke
2016/05/24 17:13:47
I'd suggest using an enum, and making the enum a p
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
| |
| 50 | |
| 49 // Helper method to create a connected ClientSocketHandle using |data|. | 51 // Helper method to create a connected ClientSocketHandle using |data|. |
| 50 // Modifies |data|. | 52 // Modifies |data|. |
| 51 std::unique_ptr<ClientSocketHandle> CreateConnectedSocketHandle( | 53 std::unique_ptr<ClientSocketHandle> CreateConnectedSocketHandle( |
| 52 SequencedSocketData* data) { | 54 SequencedSocketData* data) { |
| 53 data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); | 55 data->set_connect_data(MockConnect(SYNCHRONOUS, OK)); |
| 54 | 56 |
| 55 std::unique_ptr<MockTCPClientSocket> socket( | 57 std::unique_ptr<MockTCPClientSocket> socket( |
| 56 new MockTCPClientSocket(net::AddressList(), nullptr, data)); | 58 new MockTCPClientSocket(net::AddressList(), nullptr, data)); |
| 57 | 59 |
| 58 TestCompletionCallback callback; | 60 TestCompletionCallback callback; |
| 59 EXPECT_EQ(OK, socket->Connect(callback.callback())); | 61 EXPECT_EQ(OK, socket->Connect(callback.callback())); |
| 60 | 62 |
| 61 std::unique_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle); | 63 std::unique_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle); |
| 62 socket_handle->SetSocket(std::move(socket)); | 64 socket_handle->SetSocket(std::move(socket)); |
| 63 return socket_handle; | 65 return socket_handle; |
| 64 } | 66 } |
| 65 | 67 |
| 68 class ElementsUploadDataStreamError : public ElementsUploadDataStream { | |
|
mmenke
2016/05/24 17:13:47
Also, this is an UploadDataStream with a read erro
mmenke
2016/05/24 17:13:47
This should inherit from UploadDataStream, instead
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
| |
| 69 public: | |
| 70 ElementsUploadDataStreamError( | |
| 71 std::vector<std::unique_ptr<UploadElementReader>> element_readers, | |
| 72 int64_t identifier, | |
| 73 bool async = false) | |
|
mmenke
2016/05/24 17:13:48
There's a preference not to use default values, in
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
| |
| 74 : ElementsUploadDataStream(std::move(element_readers), identifier) { | |
| 75 async_ = async; | |
| 76 } | |
| 77 | |
| 78 int ReadInternal(IOBuffer* buf, int buf_len) override { | |
| 79 if (async_) | |
| 80 return ERR_IO_PENDING; | |
|
mmenke
2016/05/24 17:13:47
For the async case, suggest instead:
base::Thread
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
| |
| 81 return ERR_FILE_READ_FAIL; | |
|
mmenke
2016/05/24 17:13:48
This can be any error, per my suggestion not to ad
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
| |
| 82 } | |
| 83 | |
| 84 void CompleteRead(int result) { | |
|
mmenke
2016/05/24 17:13:47
Don't think this should take an argument, since Re
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
| |
| 85 ElementsUploadDataStream::OnReadCompleted(result); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 bool IsInMemory() const override { return false; } | |
|
mmenke
2016/05/24 17:13:47
nit: This should be up with the other override me
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
| |
| 90 bool async_; | |
|
mmenke
2016/05/24 17:13:47
nit:
DISALLOW_COPY_AND_ASSIGN(ElementsUploadDataS
mmenke
2016/05/24 17:13:47
nit: const
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
| |
| 91 }; | |
| 92 | |
| 93 TEST(HttpStreamParser, DataReadErrorSynchronous) { | |
| 94 MockWrite writes[] = { | |
| 95 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"), | |
| 96 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"), | |
| 97 }; | |
| 98 | |
| 99 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); | |
| 100 std::unique_ptr<ClientSocketHandle> socket_handle = | |
| 101 CreateConnectedSocketHandle(&data); | |
| 102 | |
| 103 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | |
| 104 | |
| 105 // "hello world!" shouldn't be read nor sent. | |
| 106 // ElementsUploadDataStreamError::ReadInternal() emulates either pending IO | |
| 107 // or ERR_FILE_READ_FAIL | |
| 108 element_readers.push_back( | |
| 109 base::WrapUnique(new UploadBytesElementReader("hello world!", 12))); | |
| 110 ElementsUploadDataStreamError upload_data_stream(std::move(element_readers), | |
| 111 0); | |
| 112 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); | |
| 113 | |
| 114 HttpRequestInfo request; | |
| 115 request.method = "POST"; | |
| 116 request.url = GURL("http://localhost"); | |
| 117 request.upload_data_stream = &upload_data_stream; | |
| 118 | |
| 119 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | |
| 120 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), | |
| 121 BoundNetLog()); | |
| 122 | |
| 123 HttpRequestHeaders headers; | |
| 124 headers.SetHeader("Content-Length", "12"); | |
| 125 | |
| 126 HttpResponseInfo response; | |
| 127 TestCompletionCallback callback; | |
| 128 EXPECT_EQ(ERR_FILE_READ_FAIL, | |
| 129 parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, | |
| 130 callback.callback())); | |
|
mmenke
2016/05/24 17:13:47
This works, and is correct, but I suggest instead:
maksims (do not use this acc)
2016/05/25 07:13:38
Done.
| |
| 131 | |
| 132 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | |
| 133 } | |
| 134 | |
| 135 TEST(HttpStreamParser, DataReadErrorAsynchronous) { | |
| 136 MockWrite writes[] = { | |
| 137 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"), | |
| 138 MockWrite(ASYNC, 1, "Content-Length: 12\r\n\r\n"), | |
| 139 }; | |
| 140 | |
| 141 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); | |
| 142 std::unique_ptr<ClientSocketHandle> socket_handle = | |
| 143 CreateConnectedSocketHandle(&data); | |
| 144 | |
| 145 std::vector<std::unique_ptr<UploadElementReader>> element_readers; | |
| 146 | |
| 147 // "hello world!" shouldn't be read nor sent. | |
| 148 // ElementsUploadDataStreamError::ReadInternal() emulates either pending IO | |
| 149 // or ERR_FILE_READ_FAIL | |
| 150 element_readers.push_back( | |
| 151 base::WrapUnique(new UploadBytesElementReader("hello world!", 12))); | |
| 152 ElementsUploadDataStreamError upload_data_stream(std::move(element_readers), | |
| 153 0, kAsynchronously); | |
| 154 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); | |
| 155 | |
| 156 HttpRequestInfo request; | |
| 157 request.method = "POST"; | |
| 158 request.url = GURL("http://localhost"); | |
| 159 request.upload_data_stream = &upload_data_stream; | |
| 160 | |
| 161 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | |
| 162 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), | |
| 163 BoundNetLog()); | |
| 164 | |
| 165 HttpRequestHeaders headers; | |
| 166 headers.SetHeader("Content-Length", "12"); | |
| 167 | |
| 168 DLOG(ERROR) << "TUT"; | |
|
mmenke
2016/05/24 17:13:48
Remove debugging code.
| |
| 169 HttpResponseInfo response; | |
| 170 TestCompletionCallback callback; | |
| 171 EXPECT_EQ(ERR_IO_PENDING, parser.SendRequest("POST / HTTP/1.1\r\n", headers, | |
| 172 &response, callback.callback())); | |
| 173 | |
| 174 base::RunLoop().RunUntilIdle(); | |
| 175 upload_data_stream.CompleteRead(ERR_FILE_READ_FAIL); | |
| 176 EXPECT_EQ(ERR_FILE_READ_FAIL, callback.WaitForResult()); | |
| 177 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | |
| 178 } | |
| 179 | |
| 66 // The empty payload is how the last chunk is encoded. | 180 // The empty payload is how the last chunk is encoded. |
| 67 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) { | 181 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) { |
| 68 char output[kOutputSize]; | 182 char output[kOutputSize]; |
| 69 | 183 |
| 70 const base::StringPiece kPayload = ""; | 184 const base::StringPiece kPayload = ""; |
| 71 const base::StringPiece kExpected = "0\r\n\r\n"; | 185 const base::StringPiece kExpected = "0\r\n\r\n"; |
| 72 const int num_bytes_written = | 186 const int num_bytes_written = |
| 73 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); | 187 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); |
| 74 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); | 188 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); |
| 75 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); | 189 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); |
| (...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1284 ASSERT_EQ(kBodySize, parser.ReadResponseBody( | 1398 ASSERT_EQ(kBodySize, parser.ReadResponseBody( |
| 1285 body_buffer.get(), kBodySize, callback.callback())); | 1399 body_buffer.get(), kBodySize, callback.callback())); |
| 1286 | 1400 |
| 1287 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); | 1401 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| 1288 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); | 1402 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); |
| 1289 } | 1403 } |
| 1290 | 1404 |
| 1291 } // namespace | 1405 } // namespace |
| 1292 | 1406 |
| 1293 } // namespace net | 1407 } // namespace net |
| OLD | NEW |