| OLD | NEW |
| 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 "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 | 90 |
| 91 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { | 91 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) { |
| 92 // Shouldn't be merged if upload data is non-existent. | 92 // Shouldn't be merged if upload data is non-existent. |
| 93 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 93 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 94 "some header", NULL)); | 94 "some header", NULL)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { | 97 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) { |
| 98 scoped_refptr<UploadData> upload_data = new UploadData; | 98 scoped_refptr<UploadData> upload_data = new UploadData; |
| 99 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 99 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 100 ASSERT_EQ(OK, body->Init()); | 100 ASSERT_EQ(OK, body->InitSync()); |
| 101 // Shouldn't be merged if upload data is empty. | 101 // Shouldn't be merged if upload data is empty. |
| 102 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 102 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 103 "some header", body.get())); | 103 "some header", body.get())); |
| 104 } | 104 } |
| 105 | 105 |
| 106 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { | 106 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) { |
| 107 scoped_refptr<UploadData> upload_data = new UploadData; | 107 scoped_refptr<UploadData> upload_data = new UploadData; |
| 108 upload_data->set_is_chunked(true); | 108 upload_data->set_is_chunked(true); |
| 109 const std::string payload = "123"; | 109 const std::string payload = "123"; |
| 110 upload_data->AppendChunk(payload.data(), payload.size(), true); | 110 upload_data->AppendChunk(payload.data(), payload.size(), true); |
| 111 | 111 |
| 112 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 112 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 113 ASSERT_EQ(OK, body->Init()); | 113 ASSERT_EQ(OK, body->InitSync()); |
| 114 // Shouldn't be merged if upload data carries chunked data. | 114 // Shouldn't be merged if upload data carries chunked data. |
| 115 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 115 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 116 "some header", body.get())); | 116 "some header", body.get())); |
| 117 } | 117 } |
| 118 | 118 |
| 119 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { | 119 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) { |
| 120 scoped_refptr<UploadData> upload_data = new UploadData; | 120 scoped_refptr<UploadData> upload_data = new UploadData; |
| 121 | 121 |
| 122 // Create an empty temporary file. | 122 // Create an empty temporary file. |
| 123 ScopedTempDir temp_dir; | 123 ScopedTempDir temp_dir; |
| 124 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 124 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 125 FilePath temp_file_path; | 125 FilePath temp_file_path; |
| 126 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(), | 126 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(), |
| 127 &temp_file_path)); | 127 &temp_file_path)); |
| 128 | 128 |
| 129 upload_data->AppendFileRange(temp_file_path, 0, 0, base::Time()); | 129 upload_data->AppendFileRange(temp_file_path, 0, 0, base::Time()); |
| 130 | 130 |
| 131 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 131 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 132 ASSERT_EQ(OK, body->Init()); | 132 ASSERT_EQ(OK, body->InitSync()); |
| 133 // Shouldn't be merged if upload data carries a file, as it's not in-memory. | 133 // Shouldn't be merged if upload data carries a file, as it's not in-memory. |
| 134 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 134 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 135 "some header", body.get())); | 135 "some header", body.get())); |
| 136 } | 136 } |
| 137 | 137 |
| 138 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { | 138 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) { |
| 139 scoped_refptr<UploadData> upload_data = new UploadData; | 139 scoped_refptr<UploadData> upload_data = new UploadData; |
| 140 const std::string payload = "123"; | 140 const std::string payload = "123"; |
| 141 upload_data->AppendBytes(payload.data(), payload.size()); | 141 upload_data->AppendBytes(payload.data(), payload.size()); |
| 142 | 142 |
| 143 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 143 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 144 ASSERT_EQ(OK, body->Init()); | 144 ASSERT_EQ(OK, body->InitSync()); |
| 145 // Yes, should be merged if the in-memory body is small here. | 145 // Yes, should be merged if the in-memory body is small here. |
| 146 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 146 ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 147 "some header", body.get())); | 147 "some header", body.get())); |
| 148 } | 148 } |
| 149 | 149 |
| 150 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { | 150 TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) { |
| 151 scoped_refptr<UploadData> upload_data = new UploadData; | 151 scoped_refptr<UploadData> upload_data = new UploadData; |
| 152 const std::string payload(10000, 'a'); // 'a' x 10000. | 152 const std::string payload(10000, 'a'); // 'a' x 10000. |
| 153 upload_data->AppendBytes(payload.data(), payload.size()); | 153 upload_data->AppendBytes(payload.data(), payload.size()); |
| 154 | 154 |
| 155 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); | 155 scoped_ptr<UploadDataStream> body(new UploadDataStream(upload_data)); |
| 156 ASSERT_EQ(OK, body->Init()); | 156 ASSERT_EQ(OK, body->InitSync()); |
| 157 // Shouldn't be merged if the in-memory body is large here. | 157 // Shouldn't be merged if the in-memory body is large here. |
| 158 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( | 158 ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody( |
| 159 "some header", body.get())); | 159 "some header", body.get())); |
| 160 } | 160 } |
| 161 | 161 |
| 162 // Test to ensure the HttpStreamParser state machine does not get confused | 162 // Test to ensure the HttpStreamParser state machine does not get confused |
| 163 // when sending a request with a chunked body, where chunks become available | 163 // when sending a request with a chunked body, where chunks become available |
| 164 // asynchronously, over a socket where writes may also complete | 164 // asynchronously, over a socket where writes may also complete |
| 165 // asynchronously. | 165 // asynchronously. |
| 166 // This is a regression test for http://crbug.com/132243 | 166 // This is a regression test for http://crbug.com/132243 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer, | 219 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer, |
| 220 BoundNetLog()); | 220 BoundNetLog()); |
| 221 | 221 |
| 222 scoped_refptr<UploadData> upload_data(new UploadData); | 222 scoped_refptr<UploadData> upload_data(new UploadData); |
| 223 upload_data->set_is_chunked(true); | 223 upload_data->set_is_chunked(true); |
| 224 | 224 |
| 225 upload_data->AppendChunk(kChunk1, arraysize(kChunk1) - 1, false); | 225 upload_data->AppendChunk(kChunk1, arraysize(kChunk1) - 1, false); |
| 226 | 226 |
| 227 scoped_ptr<UploadDataStream> upload_stream( | 227 scoped_ptr<UploadDataStream> upload_stream( |
| 228 new UploadDataStream(upload_data)); | 228 new UploadDataStream(upload_data)); |
| 229 ASSERT_EQ(OK, upload_stream->Init()); | 229 ASSERT_EQ(OK, upload_stream->InitSync()); |
| 230 | 230 |
| 231 HttpRequestHeaders request_headers; | 231 HttpRequestHeaders request_headers; |
| 232 request_headers.SetHeader("Host", "localhost"); | 232 request_headers.SetHeader("Host", "localhost"); |
| 233 request_headers.SetHeader("Transfer-Encoding", "chunked"); | 233 request_headers.SetHeader("Transfer-Encoding", "chunked"); |
| 234 request_headers.SetHeader("Connection", "keep-alive"); | 234 request_headers.SetHeader("Connection", "keep-alive"); |
| 235 | 235 |
| 236 HttpResponseInfo response_info; | 236 HttpResponseInfo response_info; |
| 237 // This will attempt to Write() the initial request and headers, which will | 237 // This will attempt to Write() the initial request and headers, which will |
| 238 // complete asynchronously. | 238 // complete asynchronously. |
| 239 rv = parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, | 239 rv = parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 rv = parser.ReadResponseBody(body_buffer, kBodySize, callback.callback()); | 296 rv = parser.ReadResponseBody(body_buffer, kBodySize, callback.callback()); |
| 297 ASSERT_EQ(ERR_IO_PENDING, rv); | 297 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 298 data.RunFor(1); | 298 data.RunFor(1); |
| 299 | 299 |
| 300 ASSERT_TRUE(callback.have_result()); | 300 ASSERT_TRUE(callback.have_result()); |
| 301 rv = callback.WaitForResult(); | 301 rv = callback.WaitForResult(); |
| 302 ASSERT_EQ(kBodySize, rv); | 302 ASSERT_EQ(kBodySize, rv); |
| 303 } | 303 } |
| 304 | 304 |
| 305 } // namespace net | 305 } // namespace net |
| OLD | NEW |