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

Side by Side 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, 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 unified diff | Download patch
OLDNEW
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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 new MockTCPClientSocket(net::AddressList(), nullptr, data)); 56 new MockTCPClientSocket(net::AddressList(), nullptr, data));
57 57
58 TestCompletionCallback callback; 58 TestCompletionCallback callback;
59 EXPECT_EQ(OK, socket->Connect(callback.callback())); 59 EXPECT_EQ(OK, socket->Connect(callback.callback()));
60 60
61 std::unique_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle); 61 std::unique_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle);
62 socket_handle->SetSocket(std::move(socket)); 62 socket_handle->SetSocket(std::move(socket));
63 return socket_handle; 63 return socket_handle;
64 } 64 }
65 65
66 class ReadErrorUploadDataStream : public UploadDataStream {
67 public:
68 enum class FailureMode { SYNC, ASYNC };
69
70 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
71 : UploadDataStream(true, identifier),
72 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.
73 weak_factory_(this) {}
74
75 private:
76 void CompleteRead() { UploadDataStream::OnReadCompleted(ERR_FAILED); }
77
78 int InitInternal() override { return OK; }
79
80 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.
81 if (async_) {
82 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
83 FROM_HERE, base::Bind(&ReadErrorUploadDataStream::CompleteRead,
84 weak_factory_.GetWeakPtr()),
85 base::TimeDelta::FromSeconds(1));
86 return ERR_IO_PENDING;
87 }
88 return ERR_FAILED;
89 }
90
91 void ResetInternal() override {}
92
93 const bool async_;
94
95 base::WeakPtrFactory<ReadErrorUploadDataStream> weak_factory_;
96
97 DISALLOW_COPY_AND_ASSIGN(ReadErrorUploadDataStream);
98 };
99
100 TEST(HttpStreamParser, DataReadErrorSynchronous) {
101 MockWrite writes[] = {
102 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"),
103 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"),
104 };
105
106 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
107 std::unique_ptr<ClientSocketHandle> socket_handle =
108 CreateConnectedSocketHandle(&data);
109
110 ReadErrorUploadDataStream upload_data_stream(
111 0, ReadErrorUploadDataStream::FailureMode::SYNC);
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 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
129 callback.callback());
130 EXPECT_EQ(ERR_FAILED, callback.GetResult(result));
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 ReadErrorUploadDataStream upload_data_stream(
146 0, ReadErrorUploadDataStream::FailureMode::ASYNC);
147 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback()));
148
149 HttpRequestInfo request;
150 request.method = "POST";
151 request.url = GURL("http://localhost");
152 request.upload_data_stream = &upload_data_stream;
153
154 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
155 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
156 BoundNetLog());
157
158 HttpRequestHeaders headers;
159 headers.SetHeader("Content-Length", "12");
160
161 HttpResponseInfo response;
162 TestCompletionCallback callback;
163 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
164 callback.callback());
165 EXPECT_EQ(ERR_IO_PENDING, result);
166
167 EXPECT_EQ(ERR_FAILED, callback.GetResult(result));
168 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
169 }
170
66 // The empty payload is how the last chunk is encoded. 171 // The empty payload is how the last chunk is encoded.
67 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) { 172 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) {
68 char output[kOutputSize]; 173 char output[kOutputSize];
69 174
70 const base::StringPiece kPayload = ""; 175 const base::StringPiece kPayload = "";
71 const base::StringPiece kExpected = "0\r\n\r\n"; 176 const base::StringPiece kExpected = "0\r\n\r\n";
72 const int num_bytes_written = 177 const int num_bytes_written =
73 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); 178 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
74 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); 179 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
75 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); 180 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
(...skipping 1208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1284 ASSERT_EQ(kBodySize, parser.ReadResponseBody( 1389 ASSERT_EQ(kBodySize, parser.ReadResponseBody(
1285 body_buffer.get(), kBodySize, callback.callback())); 1390 body_buffer.get(), kBodySize, callback.callback()));
1286 1391
1287 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); 1392 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
1288 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); 1393 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
1289 } 1394 }
1290 1395
1291 } // namespace 1396 } // namespace
1292 1397
1293 } // namespace net 1398 } // namespace net
OLDNEW
« 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