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

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: 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 unified diff | Download patch
« no previous file with comments | « net/http/http_stream_parser.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 explicit ReadErrorUploadDataStream(FailureMode mode)
71 : UploadDataStream(true, 0), async_(mode), weak_factory_(this) {}
72
73 private:
74 void CompleteRead() { UploadDataStream::OnReadCompleted(ERR_FAILED); }
75
76 // UploadDataStream implementation:
77 int InitInternal() override { return OK; }
78
79 int ReadInternal(IOBuffer* buf, int buf_len) override {
80 if (async_ == FailureMode::ASYNC) {
81 base::ThreadTaskRunnerHandle::Get()->PostTask(
82 FROM_HERE, base::Bind(&ReadErrorUploadDataStream::CompleteRead,
83 weak_factory_.GetWeakPtr()));
84 return ERR_IO_PENDING;
85 }
86 return ERR_FAILED;
87 }
88
89 void ResetInternal() override {}
90
91 const FailureMode async_;
92
93 base::WeakPtrFactory<ReadErrorUploadDataStream> weak_factory_;
94
95 DISALLOW_COPY_AND_ASSIGN(ReadErrorUploadDataStream);
96 };
97
98 TEST(HttpStreamParser, DataReadErrorSynchronous) {
99 MockWrite writes[] = {
100 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"),
101 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"),
102 };
103
104 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
105 std::unique_ptr<ClientSocketHandle> socket_handle =
106 CreateConnectedSocketHandle(&data);
107
108 ReadErrorUploadDataStream upload_data_stream(
109 ReadErrorUploadDataStream::FailureMode::SYNC);
110 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback()));
111
112 HttpRequestInfo request;
113 request.method = "POST";
114 request.url = GURL("http://localhost");
115 request.upload_data_stream = &upload_data_stream;
116
117 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
118 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
119 BoundNetLog());
120
121 HttpRequestHeaders headers;
122 headers.SetHeader("Content-Length", "12");
123
124 HttpResponseInfo response;
125 TestCompletionCallback callback;
126 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
127 callback.callback());
128 EXPECT_EQ(ERR_FAILED, callback.GetResult(result));
129
130 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
131 }
132
133 TEST(HttpStreamParser, DataReadErrorAsynchronous) {
134 MockWrite writes[] = {
135 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"),
136 MockWrite(ASYNC, 1, "Content-Length: 12\r\n\r\n"),
137 };
138
139 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
140 std::unique_ptr<ClientSocketHandle> socket_handle =
141 CreateConnectedSocketHandle(&data);
142
143 ReadErrorUploadDataStream upload_data_stream(
144 ReadErrorUploadDataStream::FailureMode::ASYNC);
145 ASSERT_EQ(OK, upload_data_stream.Init(TestCompletionCallback().callback()));
146
147 HttpRequestInfo request;
148 request.method = "POST";
149 request.url = GURL("http://localhost");
150 request.upload_data_stream = &upload_data_stream;
151
152 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
153 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
154 BoundNetLog());
155
156 HttpRequestHeaders headers;
157 headers.SetHeader("Content-Length", "12");
158
159 HttpResponseInfo response;
160 TestCompletionCallback callback;
161 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
162 callback.callback());
163 EXPECT_EQ(ERR_IO_PENDING, result);
164
165 EXPECT_EQ(ERR_FAILED, callback.GetResult(result));
166 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
167 }
168
66 // The empty payload is how the last chunk is encoded. 169 // The empty payload is how the last chunk is encoded.
67 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) { 170 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) {
68 char output[kOutputSize]; 171 char output[kOutputSize];
69 172
70 const base::StringPiece kPayload = ""; 173 const base::StringPiece kPayload = "";
71 const base::StringPiece kExpected = "0\r\n\r\n"; 174 const base::StringPiece kExpected = "0\r\n\r\n";
72 const int num_bytes_written = 175 const int num_bytes_written =
73 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); 176 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
74 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); 177 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
75 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); 178 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( 1387 ASSERT_EQ(kBodySize, parser.ReadResponseBody(
1285 body_buffer.get(), kBodySize, callback.callback())); 1388 body_buffer.get(), kBodySize, callback.callback()));
1286 1389
1287 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); 1390 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
1288 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); 1391 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
1289 } 1392 }
1290 1393
1291 } // namespace 1394 } // namespace
1292 1395
1293 } // namespace net 1396 } // namespace net
OLDNEW
« 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