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

Unified Diff: net/http/http_stream_parser_unittest.cc

Issue 2007013003: Introduce error handling in HttpStreamParser on UploadDataStream::Read() failure. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added upload_data_stream.cc/h in order to be able to land the cl Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/http/http_stream_parser.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..00119b3c5147ef0c3f3d4b62e7c7a97c4984a6de 100644
--- a/net/http/http_stream_parser_unittest.cc
+++ b/net/http/http_stream_parser_unittest.cc
@@ -63,6 +63,109 @@ std::unique_ptr<ClientSocketHandle> CreateConnectedSocketHandle(
return socket_handle;
}
+class ReadErrorUploadDataStream : public UploadDataStream {
+ public:
+ enum class FailureMode { SYNC, ASYNC };
+
+ explicit ReadErrorUploadDataStream(FailureMode mode)
+ : UploadDataStream(true, 0), async_(mode), weak_factory_(this) {}
+
+ private:
+ void CompleteRead() { UploadDataStream::OnReadCompleted(ERR_FAILED); }
+
+ // UploadDataStream implementation:
+ int InitInternal() override { return OK; }
+
+ int ReadInternal(IOBuffer* buf, int buf_len) override {
+ if (async_ == FailureMode::ASYNC) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(&ReadErrorUploadDataStream::CompleteRead,
+ weak_factory_.GetWeakPtr()));
+ return ERR_IO_PENDING;
+ }
+ return ERR_FAILED;
+ }
+
+ void ResetInternal() override {}
+
+ const FailureMode 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(
+ 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(
+ 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];
« no previous file with comments | « net/http/http_stream_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698