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

Side by Side Diff: net/http/http_stream_parser_unittest.cc

Issue 2330983002: Removes get upload progress plumbing. URLRequest queries the UploadDataStream directly. (Closed)
Patch Set: Feedback incorporated. Created 4 years, 3 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') | net/http/http_transaction.h » ('j') | 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"), 105 MockWrite(SYNCHRONOUS, 0, "POST / HTTP/1.1\r\n"),
106 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"), 106 MockWrite(SYNCHRONOUS, 1, "Content-Length: 12\r\n\r\n"),
107 }; 107 };
108 108
109 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); 109 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
110 std::unique_ptr<ClientSocketHandle> socket_handle = 110 std::unique_ptr<ClientSocketHandle> socket_handle =
111 CreateConnectedSocketHandle(&data); 111 CreateConnectedSocketHandle(&data);
112 112
113 ReadErrorUploadDataStream upload_data_stream( 113 ReadErrorUploadDataStream upload_data_stream(
114 ReadErrorUploadDataStream::FailureMode::SYNC); 114 ReadErrorUploadDataStream::FailureMode::SYNC);
115
116 // test upload progress before init.
117 UploadProgress progress = upload_data_stream.GetUploadProgress();
118 EXPECT_EQ(0u, progress.size());
119 EXPECT_EQ(0u, progress.position());
120
115 ASSERT_THAT(upload_data_stream.Init(TestCompletionCallback().callback(), 121 ASSERT_THAT(upload_data_stream.Init(TestCompletionCallback().callback(),
116 BoundNetLog()), 122 BoundNetLog()),
117 IsOk()); 123 IsOk());
118 124
125 // test upload progress after init.
mmenke 2016/09/15 15:15:53 nit: capitalize Test (x2)
126 progress = upload_data_stream.GetUploadProgress();
127 EXPECT_EQ(0u, progress.size());
128 EXPECT_EQ(0u, progress.position());
129
119 HttpRequestInfo request; 130 HttpRequestInfo request;
120 request.method = "POST"; 131 request.method = "POST";
121 request.url = GURL("http://localhost"); 132 request.url = GURL("http://localhost");
122 request.upload_data_stream = &upload_data_stream; 133 request.upload_data_stream = &upload_data_stream;
123 134
124 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); 135 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
125 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(), 136 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
126 BoundNetLog()); 137 BoundNetLog());
127 138
128 HttpRequestHeaders headers; 139 HttpRequestHeaders headers;
129 headers.SetHeader("Content-Length", "12"); 140 headers.SetHeader("Content-Length", "12");
130 141
131 HttpResponseInfo response; 142 HttpResponseInfo response;
132 TestCompletionCallback callback; 143 TestCompletionCallback callback;
133 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, 144 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
134 callback.callback()); 145 callback.callback());
135 EXPECT_THAT(callback.GetResult(result), IsError(ERR_FAILED)); 146 EXPECT_THAT(callback.GetResult(result), IsError(ERR_FAILED));
136 147
148 progress = upload_data_stream.GetUploadProgress();
149 EXPECT_EQ(0u, progress.size());
150 EXPECT_EQ(0u, progress.position());
151
137 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); 152 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
138 } 153 }
139 154
140 TEST(HttpStreamParser, DataReadErrorAsynchronous) { 155 TEST(HttpStreamParser, DataReadErrorAsynchronous) {
141 MockWrite writes[] = { 156 MockWrite writes[] = {
142 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"), 157 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"),
143 MockWrite(ASYNC, 1, "Content-Length: 12\r\n\r\n"), 158 MockWrite(ASYNC, 1, "Content-Length: 12\r\n\r\n"),
144 }; 159 };
145 160
146 SequencedSocketData data(nullptr, 0, writes, arraysize(writes)); 161 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
(...skipping 17 matching lines...) Expand all
164 179
165 HttpRequestHeaders headers; 180 HttpRequestHeaders headers;
166 headers.SetHeader("Content-Length", "12"); 181 headers.SetHeader("Content-Length", "12");
167 182
168 HttpResponseInfo response; 183 HttpResponseInfo response;
169 TestCompletionCallback callback; 184 TestCompletionCallback callback;
170 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, 185 int result = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
171 callback.callback()); 186 callback.callback());
172 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); 187 EXPECT_THAT(result, IsError(ERR_IO_PENDING));
173 188
189 UploadProgress progress = upload_data_stream.GetUploadProgress();
190 EXPECT_EQ(0u, progress.size());
191 EXPECT_EQ(0u, progress.position());
192
174 EXPECT_THAT(callback.GetResult(result), IsError(ERR_FAILED)); 193 EXPECT_THAT(callback.GetResult(result), IsError(ERR_FAILED));
194
175 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); 195 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
176 } 196 }
177 197
198 class InitAsyncUploadDataStream : public ChunkedUploadDataStream {
199 public:
200 // explicit InitAsyncUploadDataStream()
201 // : UploadDataStream(true, 0), weak_factory_(this) {}
202 explicit InitAsyncUploadDataStream(int64_t identifier)
203 : ChunkedUploadDataStream(identifier), weak_factory_(this) {}
204
205 private:
206 void CompleteInit() { UploadDataStream::OnInitCompleted(OK); }
207
208 // UploadDataStream implementation:
209 int InitInternal(const BoundNetLog& net_log) override {
210 base::ThreadTaskRunnerHandle::Get()->PostTask(
211 FROM_HERE, base::Bind(&InitAsyncUploadDataStream::CompleteInit,
212 weak_factory_.GetWeakPtr()));
213 return ERR_IO_PENDING;
214 }
215
216 base::WeakPtrFactory<InitAsyncUploadDataStream> weak_factory_;
217
218 DISALLOW_COPY_AND_ASSIGN(InitAsyncUploadDataStream);
219 };
220
221 TEST(HttpStreamParser, InitAsynchronousUploadDataStream) {
222 InitAsyncUploadDataStream upload_data_stream(0);
223
224 TestCompletionCallback callback;
225 int result = upload_data_stream.Init(callback.callback(), BoundNetLog());
226 ASSERT_THAT(result, IsError(ERR_IO_PENDING));
227
228 // Should be empty progress while initialization is in progress.
229 UploadProgress progress = upload_data_stream.GetUploadProgress();
230 EXPECT_EQ(0u, progress.size());
231 EXPECT_EQ(0u, progress.position());
232 EXPECT_THAT(callback.GetResult(result), IsOk());
233
234 // Initialization complete.
235 progress = upload_data_stream.GetUploadProgress();
236 EXPECT_EQ(0u, progress.size());
237 EXPECT_EQ(0u, progress.position());
238
239 HttpRequestInfo request;
240 request.method = "POST";
241 request.url = GURL("http://localhost");
242 request.upload_data_stream = &upload_data_stream;
243
244 static const char kChunk[] = "Chunk 1";
245 MockWrite writes[] = {
246 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"),
247 MockWrite(ASYNC, 1, "Transfer-Encoding: chunked\r\n\r\n"),
248 MockWrite(ASYNC, 2, "7\r\nChunk 1\r\n"),
249 };
250
251 SequencedSocketData data(nullptr, 0, writes, arraysize(writes));
252 std::unique_ptr<ClientSocketHandle> socket_handle =
253 CreateConnectedSocketHandle(&data);
254
255 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer);
256 HttpStreamParser parser(socket_handle.get(), &request, read_buffer.get(),
257 BoundNetLog());
258
259 HttpRequestHeaders headers;
260 headers.SetHeader("Transfer-Encoding", "chunked");
261
262 HttpResponseInfo response;
263 TestCompletionCallback callback1;
264 int result1 = parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
265 callback1.callback());
266 EXPECT_EQ(ERR_IO_PENDING, result1);
267 base::RunLoop().RunUntilIdle();
268 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, true);
269
270 // Check progress after read completes.
271 progress = upload_data_stream.GetUploadProgress();
272 EXPECT_EQ(0u, progress.size());
273 EXPECT_EQ(7u, progress.position());
274
275 // Check progress after reset.
276 upload_data_stream.Reset();
277 progress = upload_data_stream.GetUploadProgress();
278 EXPECT_EQ(0u, progress.size());
279 EXPECT_EQ(0u, progress.position());
280 }
281
178 // The empty payload is how the last chunk is encoded. 282 // The empty payload is how the last chunk is encoded.
179 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) { 283 TEST(HttpStreamParser, EncodeChunk_EmptyPayload) {
180 char output[kOutputSize]; 284 char output[kOutputSize];
181 285
182 const base::StringPiece kPayload = ""; 286 const base::StringPiece kPayload = "";
183 const base::StringPiece kExpected = "0\r\n\r\n"; 287 const base::StringPiece kExpected = "0\r\n\r\n";
184 const int num_bytes_written = 288 const int num_bytes_written =
185 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output)); 289 HttpStreamParser::EncodeChunk(kPayload, output, sizeof(output));
186 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written)); 290 ASSERT_EQ(kExpected.size(), static_cast<size_t>(num_bytes_written));
187 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written)); 291 EXPECT_EQ(kExpected, base::StringPiece(output, num_bytes_written));
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 574
471 HttpRequestHeaders headers; 575 HttpRequestHeaders headers;
472 headers.SetHeader("Content-Length", "12"); 576 headers.SetHeader("Content-Length", "12");
473 577
474 HttpResponseInfo response; 578 HttpResponseInfo response;
475 TestCompletionCallback callback; 579 TestCompletionCallback callback;
476 EXPECT_EQ(OK, parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response, 580 EXPECT_EQ(OK, parser.SendRequest("POST / HTTP/1.1\r\n", headers, &response,
477 callback.callback())); 581 callback.callback()));
478 582
479 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); 583 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
584
585 UploadProgress progress = upload_data_stream.GetUploadProgress();
586 EXPECT_EQ(12u, progress.size());
587 EXPECT_EQ(12u, progress.position());
480 } 588 }
481 589
482 TEST(HttpStreamParser, SentBytesChunkedPostError) { 590 TEST(HttpStreamParser, SentBytesChunkedPostError) {
483 static const char kChunk[] = "Chunk 1"; 591 static const char kChunk[] = "Chunk 1";
484 592
485 MockWrite writes[] = { 593 MockWrite writes[] = {
486 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"), 594 MockWrite(ASYNC, 0, "POST / HTTP/1.1\r\n"),
487 MockWrite(ASYNC, 1, "Transfer-Encoding: chunked\r\n\r\n"), 595 MockWrite(ASYNC, 1, "Transfer-Encoding: chunked\r\n\r\n"),
488 MockWrite(ASYNC, 2, "7\r\nChunk 1\r\n"), 596 MockWrite(ASYNC, 2, "7\r\nChunk 1\r\n"),
489 MockWrite(SYNCHRONOUS, ERR_FAILED, 3), 597 MockWrite(SYNCHRONOUS, ERR_FAILED, 3),
(...skipping 27 matching lines...) Expand all
517 625
518 base::RunLoop().RunUntilIdle(); 626 base::RunLoop().RunUntilIdle();
519 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false); 627 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false);
520 628
521 base::RunLoop().RunUntilIdle(); 629 base::RunLoop().RunUntilIdle();
522 // This write should fail. 630 // This write should fail.
523 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false); 631 upload_data_stream.AppendData(kChunk, arraysize(kChunk) - 1, false);
524 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_FAILED)); 632 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_FAILED));
525 633
526 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); 634 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
635
636 UploadProgress progress = upload_data_stream.GetUploadProgress();
637 EXPECT_EQ(0u, progress.size());
638 EXPECT_EQ(14u, progress.position());
527 } 639 }
528 640
529 // Test to ensure the HttpStreamParser state machine does not get confused 641 // Test to ensure the HttpStreamParser state machine does not get confused
530 // when sending a request with a chunked body with only one chunk that becomes 642 // when sending a request with a chunked body with only one chunk that becomes
531 // available asynchronously. 643 // available asynchronously.
532 TEST(HttpStreamParser, AsyncSingleChunkAndAsyncSocket) { 644 TEST(HttpStreamParser, AsyncSingleChunkAndAsyncSocket) {
533 static const char kChunk[] = "Chunk"; 645 static const char kChunk[] = "Chunk";
534 646
535 MockWrite writes[] = { 647 MockWrite writes[] = {
536 MockWrite(ASYNC, 0, 648 MockWrite(ASYNC, 0,
(...skipping 970 matching lines...) Expand 10 before | Expand all | Expand 10 after
1507 ASSERT_EQ(kBodySize, parser.ReadResponseBody( 1619 ASSERT_EQ(kBodySize, parser.ReadResponseBody(
1508 body_buffer.get(), kBodySize, callback.callback())); 1620 body_buffer.get(), kBodySize, callback.callback()));
1509 1621
1510 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes()); 1622 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), parser.sent_bytes());
1511 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes()); 1623 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), parser.received_bytes());
1512 } 1624 }
1513 1625
1514 } // namespace 1626 } // namespace
1515 1627
1516 } // namespace net 1628 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_stream_parser.cc ('k') | net/http/http_transaction.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698