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

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: Created 4 years, 7 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
« net/http/http_stream_parser.cc ('K') | « 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..c6f050523f3ddcd9dc6b57836e4b04a66a5243d7 100644
--- a/net/http/http_stream_parser_unittest.cc
+++ b/net/http/http_stream_parser_unittest.cc
@@ -46,6 +46,8 @@ const size_t kOutputSize = 1024; // Just large enough for this test.
const size_t kMaxPayloadSize =
kOutputSize - HttpStreamParser::kChunkHeaderFooterSize;
+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.
+
// Helper method to create a connected ClientSocketHandle using |data|.
// Modifies |data|.
std::unique_ptr<ClientSocketHandle> CreateConnectedSocketHandle(
@@ -63,6 +65,118 @@ std::unique_ptr<ClientSocketHandle> CreateConnectedSocketHandle(
return socket_handle;
}
+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.
+ public:
+ ElementsUploadDataStreamError(
+ std::vector<std::unique_ptr<UploadElementReader>> element_readers,
+ int64_t identifier,
+ 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.
+ : ElementsUploadDataStream(std::move(element_readers), identifier) {
+ async_ = async;
+ }
+
+ int ReadInternal(IOBuffer* buf, int buf_len) override {
+ if (async_)
+ 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.
+ 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.
+ }
+
+ 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.
+ ElementsUploadDataStream::OnReadCompleted(result);
+ }
+
+ private:
+ 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.
+ 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.
+};
+
+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);
+
+ std::vector<std::unique_ptr<UploadElementReader>> element_readers;
+
+ // "hello world!" shouldn't be read nor sent.
+ // ElementsUploadDataStreamError::ReadInternal() emulates either pending IO
+ // or ERR_FILE_READ_FAIL
+ element_readers.push_back(
+ base::WrapUnique(new UploadBytesElementReader("hello world!", 12)));
+ ElementsUploadDataStreamError upload_data_stream(std::move(element_readers),
+ 0);
+ 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;
+ EXPECT_EQ(ERR_FILE_READ_FAIL,
+ parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
+ 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.
+
+ 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);
+
+ std::vector<std::unique_ptr<UploadElementReader>> element_readers;
+
+ // "hello world!" shouldn't be read nor sent.
+ // ElementsUploadDataStreamError::ReadInternal() emulates either pending IO
+ // or ERR_FILE_READ_FAIL
+ element_readers.push_back(
+ base::WrapUnique(new UploadBytesElementReader("hello world!", 12)));
+ ElementsUploadDataStreamError upload_data_stream(std::move(element_readers),
+ 0, kAsynchronously);
+ 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");
+
+ DLOG(ERROR) << "TUT";
mmenke 2016/05/24 17:13:48 Remove debugging code.
+ HttpResponseInfo response;
+ TestCompletionCallback callback;
+ EXPECT_EQ(ERR_IO_PENDING, parser.SendRequest("POST / HTTP/1.1\r\n", headers,
+ &response, callback.callback()));
+
+ base::RunLoop().RunUntilIdle();
+ upload_data_stream.CompleteRead(ERR_FILE_READ_FAIL);
+ EXPECT_EQ(ERR_FILE_READ_FAIL, callback.WaitForResult());
+ 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];
« net/http/http_stream_parser.cc ('K') | « net/http/http_stream_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698