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