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