Chromium Code Reviews| Index: net/http/http_stream_parser_unittest.cc |
| diff --git a/net/http/http_stream_parser_unittest.cc b/net/http/http_stream_parser_unittest.cc |
| index 89e75be300804654d7118c78b41db007dbf557be..6c713c1bae610c1e07a9f9af18df04ccac36e206 100644 |
| --- a/net/http/http_stream_parser_unittest.cc |
| +++ b/net/http/http_stream_parser_unittest.cc |
| @@ -63,6 +63,111 @@ std::unique_ptr<ClientSocketHandle> CreateConnectedSocketHandle( |
| return socket_handle; |
| } |
| +class ReadErrorUploadDataStream : public UploadDataStream { |
| + public: |
| + enum class FailureMode { SYNC, ASYNC }; |
| + |
| + ReadErrorUploadDataStream(int64_t identifier, FailureMode mode) |
|
mmenke
2016/05/25 15:13:31
optional: May want to just replace with with:
ex
maksims (do not use this acc)
2016/05/26 08:54:41
Yes, sure. That's a good point. Protecting self fr
|
| + : UploadDataStream(true, identifier), |
| + async_(static_cast<bool>(mode)), |
|
mmenke
2016/05/25 15:13:31
Should make async a FailureMode instead, and compa
maksims (do not use this acc)
2016/05/26 08:54:41
Done.
|
| + weak_factory_(this) {} |
| + |
| + private: |
| + void CompleteRead() { UploadDataStream::OnReadCompleted(ERR_FAILED); } |
| + |
| + int InitInternal() override { return OK; } |
| + |
| + int ReadInternal(IOBuffer* buf, int buf_len) override { |
|
mmenke
2016/05/25 15:13:31
nit: Add comment "// UploadDataStream implementat
maksims (do not use this acc)
2016/05/26 08:54:41
Done.
|
| + if (async_) { |
| + base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| + FROM_HERE, base::Bind(&ReadErrorUploadDataStream::CompleteRead, |
| + weak_factory_.GetWeakPtr()), |
| + base::TimeDelta::FromSeconds(1)); |
| + return ERR_IO_PENDING; |
| + } |
| + return ERR_FAILED; |
| + } |
| + |
| + void ResetInternal() override {} |
| + |
| + const bool async_; |
| + |
| + base::WeakPtrFactory<ReadErrorUploadDataStream> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ReadErrorUploadDataStream); |
| +}; |
| + |
| +TEST(HttpStreamParser, DataReadErrorSynchronous) { |
| + MockWrite writes[] = { |
| + MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"), |
| + MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"), |
| + }; |
| + |
| + SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| + std::unique_ptr<ClientSocketHandle> socket_handle = |
| + CreateConnectedSocketHandle(&data); |
| + |
| + ReadErrorUploadDataStream upload_data_stream( |
| + 0, ReadErrorUploadDataStream::FailureMode::SYNC); |
| + ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); |
| + |
| + HttpRequestInfo request; |
| + request.method = "POST"; |
| + request.url = GURL("http://localhost"); |
| + request.upload_data_stream = &upload_data_stream; |
| + |
| + scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| + HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), |
| + BoundNetLog()); |
| + |
| + HttpRequestHeaders headers; |
| + headers.SetHeader("Content-Length", "12"); |
| + |
| + HttpResponseInfo response; |
| + TestCompletionCallback callback; |
| + int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, |
| + callback.callback()); |
| + EXPECT_EQ(ERR_FAILED, callback.GetResult(result)); |
| + |
| + EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| +} |
| + |
| +TEST(HttpStreamParser, DataReadErrorAsynchronous) { |
| + MockWrite writes[] = { |
| + MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"), |
| + MockWrite(ASYNC, 1, "Content-Length: 12\r\n\r\n"), |
| + }; |
| + |
| + SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); |
| + std::unique_ptr<ClientSocketHandle> socket_handle = |
| + CreateConnectedSocketHandle(&data); |
| + |
| + ReadErrorUploadDataStream upload_data_stream( |
| + 0, ReadErrorUploadDataStream::FailureMode::ASYNC); |
| + ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback())); |
| + |
| + HttpRequestInfo request; |
| + request.method = "POST"; |
| + request.url = GURL("http://localhost"); |
| + request.upload_data_stream = &upload_data_stream; |
| + |
| + scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
| + HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), |
| + BoundNetLog()); |
| + |
| + HttpRequestHeaders headers; |
| + headers.SetHeader("Content-Length", "12"); |
| + |
| + HttpResponseInfo response; |
| + TestCompletionCallback callback; |
| + int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, |
| + callback.callback()); |
| + EXPECT_EQ(ERR_IO_PENDING, result); |
| + |
| + EXPECT_EQ(ERR_FAILED, callback.GetResult(result)); |
| + EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); |
| +} |
| + |
| // The empty payload is how the last chunk is encoded. |
| TEST(HttpStreamParser, EncodeChunk_EmptyPayload) { |
| char output[kOutputSize]; |