Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <utility> | |
| 5 #include <vector> | 6 #include <vector> |
| 6 | 7 |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 10 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 31 #include "net/url_request/url_fetcher_delegate.h" | 32 #include "net/url_request/url_fetcher_delegate.h" |
| 32 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
| 33 #include "net/url_request/url_request_context_getter.h" | 34 #include "net/url_request/url_request_context_getter.h" |
| 34 #include "net/url_request/url_request_test_util.h" | 35 #include "net/url_request/url_request_test_util.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 37 |
| 37 namespace net { | 38 namespace net { |
| 38 | 39 |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 42 const int kMaxExpectedResponseLength = 2048; | |
| 43 | |
| 41 void SetTimedOutAndQuitLoop(const base::WeakPtr<bool> timed_out, | 44 void SetTimedOutAndQuitLoop(const base::WeakPtr<bool> timed_out, |
| 42 const base::Closure& quit_loop_func) { | 45 const base::Closure& quit_loop_func) { |
| 43 if (timed_out) { | 46 if (timed_out) { |
| 44 *timed_out = true; | 47 *timed_out = true; |
| 45 quit_loop_func.Run(); | 48 quit_loop_func.Run(); |
| 46 } | 49 } |
| 47 } | 50 } |
| 48 | 51 |
| 49 bool RunLoopWithTimeout(base::RunLoop* run_loop) { | 52 bool RunLoopWithTimeout(base::RunLoop* run_loop) { |
| 50 bool timed_out = false; | 53 bool timed_out = false; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 79 return ERR_TIMED_OUT; | 82 return ERR_TIMED_OUT; |
| 80 return connect_result_; | 83 return connect_result_; |
| 81 } | 84 } |
| 82 | 85 |
| 83 void Send(const std::string& data) { | 86 void Send(const std::string& data) { |
| 84 write_buffer_ = | 87 write_buffer_ = |
| 85 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); | 88 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); |
| 86 Write(); | 89 Write(); |
| 87 } | 90 } |
| 88 | 91 |
| 92 bool Read(std::string* data) { | |
| 93 base::RunLoop run_loop; | |
| 94 Read(run_loop.QuitClosure()); | |
| 95 bool success = RunLoopWithTimeout(&run_loop); | |
| 96 std::string io_buffer_contents(read_buffer_->data()); | |
| 97 *data = io_buffer_contents.substr(0, bytes_received_); | |
| 98 return success; | |
| 99 } | |
| 100 | |
| 89 private: | 101 private: |
| 90 void OnConnect(const base::Closure& quit_loop, int result) { | 102 void OnConnect(const base::Closure& quit_loop, int result) { |
| 91 connect_result_ = result; | 103 connect_result_ = result; |
| 92 quit_loop.Run(); | 104 quit_loop.Run(); |
| 93 } | 105 } |
| 94 | 106 |
| 95 void Write() { | 107 void Write() { |
| 96 int result = socket_->Write( | 108 int result = socket_->Write( |
| 97 write_buffer_.get(), | 109 write_buffer_.get(), |
| 98 write_buffer_->BytesRemaining(), | 110 write_buffer_->BytesRemaining(), |
| 99 base::Bind(&TestHttpClient::OnWrite, base::Unretained(this))); | 111 base::Bind(&TestHttpClient::OnWrite, base::Unretained(this))); |
| 100 if (result != ERR_IO_PENDING) | 112 if (result != ERR_IO_PENDING) |
| 101 OnWrite(result); | 113 OnWrite(result); |
| 102 } | 114 } |
| 103 | 115 |
| 104 void OnWrite(int result) { | 116 void OnWrite(int result) { |
| 105 ASSERT_GT(result, 0); | 117 ASSERT_GT(result, 0); |
| 106 write_buffer_->DidConsume(result); | 118 write_buffer_->DidConsume(result); |
| 107 if (write_buffer_->BytesRemaining()) | 119 if (write_buffer_->BytesRemaining()) |
| 108 Write(); | 120 Write(); |
| 109 } | 121 } |
| 110 | 122 |
| 123 void Read(const base::Closure& quit_loop) { | |
|
Ryan Sleevi
2014/03/27 20:59:46
This overloads with the Read method on 92. Rename
gunsch
2014/03/27 23:41:17
Done.
| |
| 124 read_buffer_ = new IOBufferWithSize(kMaxExpectedResponseLength); | |
| 125 int result = socket_->Read(read_buffer_, | |
| 126 kMaxExpectedResponseLength, | |
| 127 base::Bind(&TestHttpClient::OnRead, | |
| 128 base::Unretained(this), | |
| 129 quit_loop)); | |
| 130 if (result != ERR_IO_PENDING) | |
| 131 OnRead(quit_loop, result); | |
| 132 } | |
| 133 | |
| 134 void OnRead(const base::Closure& quit_loop, int result) { | |
| 135 bytes_received_ = result; | |
| 136 quit_loop.Run(); | |
| 137 } | |
| 138 | |
| 139 base::Closure run_loop_quit_func_; | |
| 140 scoped_refptr<IOBufferWithSize> read_buffer_; | |
| 111 scoped_refptr<DrainableIOBuffer> write_buffer_; | 141 scoped_refptr<DrainableIOBuffer> write_buffer_; |
| 112 scoped_ptr<TCPClientSocket> socket_; | 142 scoped_ptr<TCPClientSocket> socket_; |
| 143 int bytes_received_; | |
| 113 int connect_result_; | 144 int connect_result_; |
| 114 }; | 145 }; |
| 115 | 146 |
| 116 } // namespace | 147 } // namespace |
| 117 | 148 |
| 118 class HttpServerTest : public testing::Test, | 149 class HttpServerTest : public testing::Test, |
| 119 public HttpServer::Delegate { | 150 public HttpServer::Delegate { |
| 120 public: | 151 public: |
| 121 HttpServerTest() : quit_after_request_count_(0) {} | 152 HttpServerTest() : quit_after_request_count_(0) {} |
| 122 | 153 |
| 123 virtual void SetUp() OVERRIDE { | 154 virtual void SetUp() OVERRIDE { |
| 124 TCPListenSocketFactory socket_factory("127.0.0.1", 0); | 155 TCPListenSocketFactory socket_factory("127.0.0.1", 0); |
| 125 server_ = new HttpServer(socket_factory, this); | 156 server_ = new HttpServer(socket_factory, this); |
| 126 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); | 157 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); |
| 127 } | 158 } |
| 128 | 159 |
| 129 virtual void OnHttpRequest(int connection_id, | 160 virtual void OnHttpRequest(int connection_id, |
| 130 const HttpServerRequestInfo& info) OVERRIDE { | 161 const HttpServerRequestInfo& info) OVERRIDE { |
| 131 requests_.push_back(info); | 162 requests_.push_back(std::make_pair(info, connection_id)); |
| 132 if (requests_.size() == quit_after_request_count_) | 163 if (requests_.size() == quit_after_request_count_) |
| 133 run_loop_quit_func_.Run(); | 164 run_loop_quit_func_.Run(); |
| 134 } | 165 } |
| 135 | 166 |
| 136 virtual void OnWebSocketRequest(int connection_id, | 167 virtual void OnWebSocketRequest(int connection_id, |
| 137 const HttpServerRequestInfo& info) OVERRIDE { | 168 const HttpServerRequestInfo& info) OVERRIDE { |
| 138 NOTREACHED(); | 169 NOTREACHED(); |
| 139 } | 170 } |
| 140 | 171 |
| 141 virtual void OnWebSocketMessage(int connection_id, | 172 virtual void OnWebSocketMessage(int connection_id, |
| 142 const std::string& data) OVERRIDE { | 173 const std::string& data) OVERRIDE { |
| 143 NOTREACHED(); | 174 NOTREACHED(); |
| 144 } | 175 } |
| 145 | 176 |
| 146 virtual void OnClose(int connection_id) OVERRIDE {} | 177 virtual void OnClose(int connection_id) OVERRIDE {} |
| 147 | 178 |
| 148 bool RunUntilRequestsReceived(size_t count) { | 179 bool RunUntilRequestsReceived(size_t count) { |
| 149 quit_after_request_count_ = count; | 180 quit_after_request_count_ = count; |
| 150 if (requests_.size() == count) | 181 if (requests_.size() == count) |
| 151 return true; | 182 return true; |
| 152 | 183 |
| 153 base::RunLoop run_loop; | 184 base::RunLoop run_loop; |
| 154 run_loop_quit_func_ = run_loop.QuitClosure(); | 185 run_loop_quit_func_ = run_loop.QuitClosure(); |
| 155 bool success = RunLoopWithTimeout(&run_loop); | 186 bool success = RunLoopWithTimeout(&run_loop); |
| 156 run_loop_quit_func_.Reset(); | 187 run_loop_quit_func_.Reset(); |
| 157 return success; | 188 return success; |
| 158 } | 189 } |
| 159 | 190 |
| 191 HttpServerRequestInfo request(int request_index) { | |
|
Ryan Sleevi
2014/03/27 20:59:46
naming: GetRequest()
defensive: Should you DCHECK_
gunsch
2014/03/27 23:41:17
Done.
| |
| 192 return requests_[request_index].first; | |
| 193 } | |
| 194 | |
| 195 int connection_id(int request_index) { | |
|
Ryan Sleevi
2014/03/27 20:59:46
naming: GetConnectionId()
defensive: Should you DC
gunsch
2014/03/27 23:41:17
Done.
| |
| 196 return requests_[request_index].second; | |
| 197 } | |
| 198 | |
| 160 protected: | 199 protected: |
| 161 scoped_refptr<HttpServer> server_; | 200 scoped_refptr<HttpServer> server_; |
| 162 IPEndPoint server_address_; | 201 IPEndPoint server_address_; |
| 163 base::Closure run_loop_quit_func_; | 202 base::Closure run_loop_quit_func_; |
| 164 std::vector<HttpServerRequestInfo> requests_; | 203 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; |
| 165 | 204 |
| 166 private: | 205 private: |
| 167 size_t quit_after_request_count_; | 206 size_t quit_after_request_count_; |
| 168 }; | 207 }; |
| 169 | 208 |
| 170 TEST_F(HttpServerTest, Request) { | 209 TEST_F(HttpServerTest, Request) { |
| 171 TestHttpClient client; | 210 TestHttpClient client; |
| 172 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 211 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 173 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 212 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 174 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 213 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 175 ASSERT_EQ("GET", requests_[0].method); | 214 ASSERT_EQ("GET", request(0).method); |
| 176 ASSERT_EQ("/test", requests_[0].path); | 215 ASSERT_EQ("/test", request(0).path); |
| 177 ASSERT_EQ("", requests_[0].data); | 216 ASSERT_EQ("", request(0).data); |
| 178 ASSERT_EQ(0u, requests_[0].headers.size()); | 217 ASSERT_EQ(0u, request(0).headers.size()); |
| 179 ASSERT_TRUE(StartsWithASCII(requests_[0].peer.ToString(), "127.0.0.1", true)); | 218 ASSERT_TRUE(StartsWithASCII(request(0).peer.ToString(), "127.0.0.1", true)); |
| 180 } | 219 } |
| 181 | 220 |
| 182 TEST_F(HttpServerTest, RequestWithHeaders) { | 221 TEST_F(HttpServerTest, RequestWithHeaders) { |
| 183 TestHttpClient client; | 222 TestHttpClient client; |
| 184 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 223 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 185 const char* kHeaders[][3] = { | 224 const char* kHeaders[][3] = { |
| 186 {"Header", ": ", "1"}, | 225 {"Header", ": ", "1"}, |
| 187 {"HeaderWithNoWhitespace", ":", "1"}, | 226 {"HeaderWithNoWhitespace", ":", "1"}, |
| 188 {"HeaderWithWhitespace", " : \t ", "1 1 1 \t "}, | 227 {"HeaderWithWhitespace", " : \t ", "1 1 1 \t "}, |
| 189 {"HeaderWithColon", ": ", "1:1"}, | 228 {"HeaderWithColon", ": ", "1:1"}, |
| 190 {"EmptyHeader", ":", ""}, | 229 {"EmptyHeader", ":", ""}, |
| 191 {"EmptyHeaderWithWhitespace", ": \t ", ""}, | 230 {"EmptyHeaderWithWhitespace", ": \t ", ""}, |
| 192 {"HeaderWithNonASCII", ": ", "\xf7"}, | 231 {"HeaderWithNonASCII", ": ", "\xf7"}, |
| 193 }; | 232 }; |
| 194 std::string headers; | 233 std::string headers; |
| 195 for (size_t i = 0; i < arraysize(kHeaders); ++i) { | 234 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 196 headers += | 235 headers += |
| 197 std::string(kHeaders[i][0]) + kHeaders[i][1] + kHeaders[i][2] + "\r\n"; | 236 std::string(kHeaders[i][0]) + kHeaders[i][1] + kHeaders[i][2] + "\r\n"; |
| 198 } | 237 } |
| 199 | 238 |
| 200 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); | 239 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); |
| 201 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 240 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 202 ASSERT_EQ("", requests_[0].data); | 241 ASSERT_EQ("", request(0).data); |
| 203 | 242 |
| 204 for (size_t i = 0; i < arraysize(kHeaders); ++i) { | 243 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 205 std::string field = StringToLowerASCII(std::string(kHeaders[i][0])); | 244 std::string field = StringToLowerASCII(std::string(kHeaders[i][0])); |
| 206 std::string value = kHeaders[i][2]; | 245 std::string value = kHeaders[i][2]; |
| 207 ASSERT_EQ(1u, requests_[0].headers.count(field)) << field; | 246 ASSERT_EQ(1u, request(0).headers.count(field)) << field; |
| 208 ASSERT_EQ(value, requests_[0].headers[field]) << kHeaders[i][0]; | 247 ASSERT_EQ(value, request(0).headers[field]) << kHeaders[i][0]; |
| 209 } | 248 } |
| 210 } | 249 } |
| 211 | 250 |
| 212 TEST_F(HttpServerTest, RequestWithBody) { | 251 TEST_F(HttpServerTest, RequestWithBody) { |
| 213 TestHttpClient client; | 252 TestHttpClient client; |
| 214 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 253 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 215 std::string body = "a" + std::string(1 << 10, 'b') + "c"; | 254 std::string body = "a" + std::string(1 << 10, 'b') + "c"; |
| 216 client.Send(base::StringPrintf( | 255 client.Send(base::StringPrintf( |
| 217 "GET /test HTTP/1.1\r\n" | 256 "GET /test HTTP/1.1\r\n" |
| 218 "SomeHeader: 1\r\n" | 257 "SomeHeader: 1\r\n" |
| 219 "Content-Length: %" PRIuS "\r\n\r\n%s", | 258 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 220 body.length(), | 259 body.length(), |
| 221 body.c_str())); | 260 body.c_str())); |
| 222 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 261 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 223 ASSERT_EQ(2u, requests_[0].headers.size()); | 262 ASSERT_EQ(2u, request(0).headers.size()); |
| 224 ASSERT_EQ(body.length(), requests_[0].data.length()); | 263 ASSERT_EQ(body.length(), request(0).data.length()); |
| 225 ASSERT_EQ('a', body[0]); | 264 ASSERT_EQ('a', body[0]); |
| 226 ASSERT_EQ('c', *body.rbegin()); | 265 ASSERT_EQ('c', *body.rbegin()); |
| 227 } | 266 } |
| 228 | 267 |
| 229 TEST_F(HttpServerTest, RequestWithTooLargeBody) { | 268 TEST_F(HttpServerTest, RequestWithTooLargeBody) { |
| 230 class TestURLFetcherDelegate : public URLFetcherDelegate { | 269 class TestURLFetcherDelegate : public URLFetcherDelegate { |
| 231 public: | 270 public: |
| 232 TestURLFetcherDelegate(const base::Closure& quit_loop_func) | 271 TestURLFetcherDelegate(const base::Closure& quit_loop_func) |
| 233 : quit_loop_func_(quit_loop_func) {} | 272 : quit_loop_func_(quit_loop_func) {} |
| 234 virtual ~TestURLFetcherDelegate() {} | 273 virtual ~TestURLFetcherDelegate() {} |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 254 &delegate)); | 293 &delegate)); |
| 255 fetcher->SetRequestContext(request_context_getter.get()); | 294 fetcher->SetRequestContext(request_context_getter.get()); |
| 256 fetcher->AddExtraRequestHeader( | 295 fetcher->AddExtraRequestHeader( |
| 257 base::StringPrintf("content-length:%d", 1 << 30)); | 296 base::StringPrintf("content-length:%d", 1 << 30)); |
| 258 fetcher->Start(); | 297 fetcher->Start(); |
| 259 | 298 |
| 260 ASSERT_TRUE(RunLoopWithTimeout(&run_loop)); | 299 ASSERT_TRUE(RunLoopWithTimeout(&run_loop)); |
| 261 ASSERT_EQ(0u, requests_.size()); | 300 ASSERT_EQ(0u, requests_.size()); |
| 262 } | 301 } |
| 263 | 302 |
| 303 TEST_F(HttpServerTest, Send200) { | |
| 304 TestHttpClient client; | |
| 305 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | |
| 306 client.Send("GET /test HTTP/1.1\r\n\r\n"); | |
| 307 ASSERT_TRUE(RunUntilRequestsReceived(1)); | |
| 308 server_->Send200(connection_id(0), "Response!", "text/plain"); | |
| 309 | |
| 310 std::string response; | |
| 311 ASSERT_TRUE(client.Read(&response)); | |
| 312 ASSERT_TRUE(StartsWithASCII(response, "HTTP/1.1 200 OK", true)); | |
| 313 ASSERT_TRUE(response.find("Response!") != std::string::npos); | |
|
Ryan Sleevi
2014/03/27 20:59:46
ASSERT_NE(std::string::npos, response.find("Respon
gunsch
2014/03/27 23:41:17
Done.
| |
| 314 } | |
| 315 | |
| 316 TEST_F(HttpServerTest, SendRaw) { | |
| 317 TestHttpClient client; | |
| 318 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | |
| 319 client.Send("GET /test HTTP/1.1\r\n\r\n"); | |
| 320 ASSERT_TRUE(RunUntilRequestsReceived(1)); | |
| 321 server_->SendRaw(connection_id(0), "Raw Data "); | |
| 322 server_->SendRaw(connection_id(0), "More Data"); | |
| 323 server_->SendRaw(connection_id(0), "Third Piece of Data"); | |
| 324 | |
| 325 std::string response; | |
| 326 ASSERT_TRUE(client.Read(&response)); | |
| 327 ASSERT_EQ("Raw Data More DataThird Piece of Data", response); | |
| 328 } | |
| 329 | |
| 264 namespace { | 330 namespace { |
| 265 | 331 |
| 266 class MockStreamListenSocket : public StreamListenSocket { | 332 class MockStreamListenSocket : public StreamListenSocket { |
| 267 public: | 333 public: |
| 268 MockStreamListenSocket(StreamListenSocket::Delegate* delegate) | 334 MockStreamListenSocket(StreamListenSocket::Delegate* delegate) |
| 269 : StreamListenSocket(kInvalidSocket, delegate) {} | 335 : StreamListenSocket(kInvalidSocket, delegate) {} |
| 270 | 336 |
| 271 virtual void Accept() OVERRIDE { NOTREACHED(); } | 337 virtual void Accept() OVERRIDE { NOTREACHED(); } |
| 272 | 338 |
| 273 private: | 339 private: |
| 274 virtual ~MockStreamListenSocket() {} | 340 virtual ~MockStreamListenSocket() {} |
| 275 }; | 341 }; |
| 276 | 342 |
| 277 } // namespace | 343 } // namespace |
| 278 | 344 |
| 279 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { | 345 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { |
| 280 StreamListenSocket* socket = | 346 StreamListenSocket* socket = |
| 281 new MockStreamListenSocket(server_.get()); | 347 new MockStreamListenSocket(server_.get()); |
| 282 server_->DidAccept(NULL, make_scoped_ptr(socket)); | 348 server_->DidAccept(NULL, make_scoped_ptr(socket)); |
| 283 std::string body("body"); | 349 std::string body("body"); |
| 284 std::string request = base::StringPrintf( | 350 std::string request_text = base::StringPrintf( |
| 285 "GET /test HTTP/1.1\r\n" | 351 "GET /test HTTP/1.1\r\n" |
| 286 "SomeHeader: 1\r\n" | 352 "SomeHeader: 1\r\n" |
| 287 "Content-Length: %" PRIuS "\r\n\r\n%s", | 353 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 288 body.length(), | 354 body.length(), |
| 289 body.c_str()); | 355 body.c_str()); |
| 290 server_->DidRead(socket, request.c_str(), request.length() - 2); | 356 server_->DidRead(socket, request_text.c_str(), request_text.length() - 2); |
| 291 ASSERT_EQ(0u, requests_.size()); | 357 ASSERT_EQ(0u, requests_.size()); |
| 292 server_->DidRead(socket, request.c_str() + request.length() - 2, 2); | 358 server_->DidRead(socket, request_text.c_str() + request_text.length() - 2, 2); |
| 293 ASSERT_EQ(1u, requests_.size()); | 359 ASSERT_EQ(1u, requests_.size()); |
| 294 ASSERT_EQ(body, requests_[0].data); | 360 ASSERT_EQ(body, request(0).data); |
| 295 } | 361 } |
| 296 | 362 |
| 297 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) { | 363 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) { |
| 298 // The idea behind this test is that requests with or without bodies should | 364 // The idea behind this test is that requests with or without bodies should |
| 299 // not break parsing of the next request. | 365 // not break parsing of the next request. |
| 300 TestHttpClient client; | 366 TestHttpClient client; |
| 301 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 367 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 302 std::string body = "body"; | 368 std::string body = "body"; |
| 303 client.Send(base::StringPrintf( | 369 client.Send(base::StringPrintf( |
| 304 "GET /test HTTP/1.1\r\n" | 370 "GET /test HTTP/1.1\r\n" |
| 305 "Content-Length: %" PRIuS "\r\n\r\n%s", | 371 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 306 body.length(), | 372 body.length(), |
| 307 body.c_str())); | 373 body.c_str())); |
| 308 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 374 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 309 ASSERT_EQ(body, requests_[0].data); | 375 ASSERT_EQ(body, request(0).data); |
| 376 | |
| 377 int client_connection_id = connection_id(0); | |
| 378 server_->Send200(client_connection_id, "Content for /test", "text/plain"); | |
| 379 std::string response1; | |
| 380 ASSERT_TRUE(client.Read(&response1)); | |
| 381 ASSERT_TRUE(StartsWithASCII(response1, "HTTP/1.1 200 OK", true)); | |
| 382 ASSERT_TRUE(EndsWith(response1, "Content for /test", true)); | |
| 310 | 383 |
| 311 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); | 384 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); |
| 312 ASSERT_TRUE(RunUntilRequestsReceived(2)); | 385 ASSERT_TRUE(RunUntilRequestsReceived(2)); |
| 313 ASSERT_EQ("/test2", requests_[1].path); | 386 ASSERT_EQ("/test2", request(1).path); |
| 387 | |
| 388 ASSERT_EQ(client_connection_id, connection_id(1)); | |
| 389 server_->Send404(client_connection_id); | |
| 390 std::string response2; | |
| 391 ASSERT_TRUE(client.Read(&response2)); | |
| 392 ASSERT_TRUE(StartsWithASCII(response2, "HTTP/1.1 404 Not Found", true)); | |
| 314 | 393 |
| 315 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); | 394 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); |
| 316 ASSERT_TRUE(RunUntilRequestsReceived(3)); | 395 ASSERT_TRUE(RunUntilRequestsReceived(3)); |
| 317 ASSERT_EQ("/test3", requests_[2].path); | 396 ASSERT_EQ("/test3", request(2).path); |
| 397 | |
| 398 ASSERT_EQ(client_connection_id, connection_id(2)); | |
| 399 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | |
| 400 std::string response3; | |
| 401 ASSERT_TRUE(client.Read(&response3)); | |
| 402 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | |
| 403 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | |
| 318 } | 404 } |
| 319 | 405 |
| 320 } // namespace net | 406 } // namespace net |
| OLD | NEW |