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 const std::string Read() { | |
| 94 net::TestCompletionCallback callback; | |
| 95 ReadInternal(callback.callback()); | |
| 96 int bytes_received = callback.WaitForResult(); | |
| 97 EXPECT_GT(bytes_received, 0); | |
|
Ryan Sleevi
2014/04/10 20:58:36
This won't do what you want.
When this condition
gunsch
2014/04/11 16:55:57
Interesting, guess I was still expecting the Java/
| |
| 98 std::string io_buffer_contents(read_buffer_->data()); | |
| 99 return io_buffer_contents.substr(0, bytes_received); | |
| 100 } | |
| 101 | |
| 89 private: | 102 private: |
| 90 void OnConnect(const base::Closure& quit_loop, int result) { | 103 void OnConnect(const base::Closure& quit_loop, int result) { |
| 91 connect_result_ = result; | 104 connect_result_ = result; |
| 92 quit_loop.Run(); | 105 quit_loop.Run(); |
| 93 } | 106 } |
| 94 | 107 |
| 95 void Write() { | 108 void Write() { |
| 96 int result = socket_->Write( | 109 int result = socket_->Write( |
| 97 write_buffer_.get(), | 110 write_buffer_.get(), |
| 98 write_buffer_->BytesRemaining(), | 111 write_buffer_->BytesRemaining(), |
| 99 base::Bind(&TestHttpClient::OnWrite, base::Unretained(this))); | 112 base::Bind(&TestHttpClient::OnWrite, base::Unretained(this))); |
| 100 if (result != ERR_IO_PENDING) | 113 if (result != ERR_IO_PENDING) |
| 101 OnWrite(result); | 114 OnWrite(result); |
| 102 } | 115 } |
| 103 | 116 |
| 104 void OnWrite(int result) { | 117 void OnWrite(int result) { |
| 105 ASSERT_GT(result, 0); | 118 ASSERT_GT(result, 0); |
| 106 write_buffer_->DidConsume(result); | 119 write_buffer_->DidConsume(result); |
| 107 if (write_buffer_->BytesRemaining()) | 120 if (write_buffer_->BytesRemaining()) |
| 108 Write(); | 121 Write(); |
| 109 } | 122 } |
| 110 | 123 |
| 124 void ReadInternal(const net::CompletionCallback& callback) { | |
| 125 read_buffer_ = new IOBufferWithSize(kMaxExpectedResponseLength); | |
| 126 int result = socket_->Read(read_buffer_, | |
| 127 kMaxExpectedResponseLength, | |
| 128 callback); | |
| 129 if (result != ERR_IO_PENDING) | |
| 130 callback.Run(result); | |
| 131 } | |
| 132 | |
| 133 scoped_refptr<IOBufferWithSize> read_buffer_; | |
| 111 scoped_refptr<DrainableIOBuffer> write_buffer_; | 134 scoped_refptr<DrainableIOBuffer> write_buffer_; |
| 112 scoped_ptr<TCPClientSocket> socket_; | 135 scoped_ptr<TCPClientSocket> socket_; |
| 113 int connect_result_; | 136 int connect_result_; |
| 114 }; | 137 }; |
| 115 | 138 |
| 116 } // namespace | 139 } // namespace |
| 117 | 140 |
| 118 class HttpServerTest : public testing::Test, | 141 class HttpServerTest : public testing::Test, |
| 119 public HttpServer::Delegate { | 142 public HttpServer::Delegate { |
| 120 public: | 143 public: |
| 121 HttpServerTest() : quit_after_request_count_(0) {} | 144 HttpServerTest() : quit_after_request_count_(0) {} |
| 122 | 145 |
| 123 virtual void SetUp() OVERRIDE { | 146 virtual void SetUp() OVERRIDE { |
| 124 TCPListenSocketFactory socket_factory("127.0.0.1", 0); | 147 TCPListenSocketFactory socket_factory("127.0.0.1", 0); |
| 125 server_ = new HttpServer(socket_factory, this); | 148 server_ = new HttpServer(socket_factory, this); |
| 126 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); | 149 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); |
| 127 } | 150 } |
| 128 | 151 |
| 129 virtual void OnHttpRequest(int connection_id, | 152 virtual void OnHttpRequest(int connection_id, |
| 130 const HttpServerRequestInfo& info) OVERRIDE { | 153 const HttpServerRequestInfo& info) OVERRIDE { |
| 131 requests_.push_back(info); | 154 requests_.push_back(std::make_pair(info, connection_id)); |
| 132 if (requests_.size() == quit_after_request_count_) | 155 if (requests_.size() == quit_after_request_count_) |
| 133 run_loop_quit_func_.Run(); | 156 run_loop_quit_func_.Run(); |
| 134 } | 157 } |
| 135 | 158 |
| 136 virtual void OnWebSocketRequest(int connection_id, | 159 virtual void OnWebSocketRequest(int connection_id, |
| 137 const HttpServerRequestInfo& info) OVERRIDE { | 160 const HttpServerRequestInfo& info) OVERRIDE { |
| 138 NOTREACHED(); | 161 NOTREACHED(); |
| 139 } | 162 } |
| 140 | 163 |
| 141 virtual void OnWebSocketMessage(int connection_id, | 164 virtual void OnWebSocketMessage(int connection_id, |
| 142 const std::string& data) OVERRIDE { | 165 const std::string& data) OVERRIDE { |
| 143 NOTREACHED(); | 166 NOTREACHED(); |
| 144 } | 167 } |
| 145 | 168 |
| 146 virtual void OnClose(int connection_id) OVERRIDE {} | 169 virtual void OnClose(int connection_id) OVERRIDE {} |
| 147 | 170 |
| 148 bool RunUntilRequestsReceived(size_t count) { | 171 bool RunUntilRequestsReceived(size_t count) { |
| 149 quit_after_request_count_ = count; | 172 quit_after_request_count_ = count; |
| 150 if (requests_.size() == count) | 173 if (requests_.size() == count) |
| 151 return true; | 174 return true; |
| 152 | 175 |
| 153 base::RunLoop run_loop; | 176 base::RunLoop run_loop; |
| 154 run_loop_quit_func_ = run_loop.QuitClosure(); | 177 run_loop_quit_func_ = run_loop.QuitClosure(); |
| 155 bool success = RunLoopWithTimeout(&run_loop); | 178 bool success = RunLoopWithTimeout(&run_loop); |
| 156 run_loop_quit_func_.Reset(); | 179 run_loop_quit_func_.Reset(); |
| 157 return success; | 180 return success; |
| 158 } | 181 } |
| 159 | 182 |
| 183 HttpServerRequestInfo GetRequest(size_t request_index) { | |
| 184 EXPECT_LE(request_index, requests_.size()); | |
|
Ryan Sleevi
2014/04/10 20:58:36
This doesn't really do what you want, for similar
gunsch
2014/04/11 16:55:57
In that case, I'd rather remove these checks----it
| |
| 185 return requests_[request_index].first; | |
| 186 } | |
| 187 | |
| 188 int GetConnectionId(size_t request_index) { | |
| 189 EXPECT_LE(request_index, requests_.size()); | |
|
Ryan Sleevi
2014/04/10 20:58:36
Ditto; this doesn't do what you expect.
gunsch
2014/04/11 16:55:57
Done.
| |
| 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 = client.Read(); | |
| 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 = client.Read(); | |
| 321 ASSERT_EQ("Raw Data More DataThird Piece of Data", response); | |
| 322 } | |
| 323 | |
| 264 namespace { | 324 namespace { |
| 265 | 325 |
| 266 class MockStreamListenSocket : public StreamListenSocket { | 326 class MockStreamListenSocket : public StreamListenSocket { |
| 267 public: | 327 public: |
| 268 MockStreamListenSocket(StreamListenSocket::Delegate* delegate) | 328 MockStreamListenSocket(StreamListenSocket::Delegate* delegate) |
| 269 : StreamListenSocket(kInvalidSocket, delegate) {} | 329 : StreamListenSocket(kInvalidSocket, delegate) {} |
| 270 | 330 |
| 271 virtual void Accept() OVERRIDE { NOTREACHED(); } | 331 virtual void Accept() OVERRIDE { NOTREACHED(); } |
| 272 | 332 |
| 273 private: | 333 private: |
| 274 virtual ~MockStreamListenSocket() {} | 334 virtual ~MockStreamListenSocket() {} |
| 275 }; | 335 }; |
| 276 | 336 |
| 277 } // namespace | 337 } // namespace |
| 278 | 338 |
| 279 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { | 339 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { |
| 280 StreamListenSocket* socket = | 340 StreamListenSocket* socket = |
| 281 new MockStreamListenSocket(server_.get()); | 341 new MockStreamListenSocket(server_.get()); |
| 282 server_->DidAccept(NULL, make_scoped_ptr(socket)); | 342 server_->DidAccept(NULL, make_scoped_ptr(socket)); |
| 283 std::string body("body"); | 343 std::string body("body"); |
| 284 std::string request = base::StringPrintf( | 344 std::string request_text = base::StringPrintf( |
| 285 "GET /test HTTP/1.1\r\n" | 345 "GET /test HTTP/1.1\r\n" |
| 286 "SomeHeader: 1\r\n" | 346 "SomeHeader: 1\r\n" |
| 287 "Content-Length: %" PRIuS "\r\n\r\n%s", | 347 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 288 body.length(), | 348 body.length(), |
| 289 body.c_str()); | 349 body.c_str()); |
| 290 server_->DidRead(socket, request.c_str(), request.length() - 2); | 350 server_->DidRead(socket, request_text.c_str(), request_text.length() - 2); |
| 291 ASSERT_EQ(0u, requests_.size()); | 351 ASSERT_EQ(0u, requests_.size()); |
| 292 server_->DidRead(socket, request.c_str() + request.length() - 2, 2); | 352 server_->DidRead(socket, request_text.c_str() + request_text.length() - 2, 2); |
| 293 ASSERT_EQ(1u, requests_.size()); | 353 ASSERT_EQ(1u, requests_.size()); |
| 294 ASSERT_EQ(body, requests_[0].data); | 354 ASSERT_EQ(body, GetRequest(0).data); |
| 295 } | 355 } |
| 296 | 356 |
| 297 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) { | 357 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) { |
| 298 // The idea behind this test is that requests with or without bodies should | 358 // The idea behind this test is that requests with or without bodies should |
| 299 // not break parsing of the next request. | 359 // not break parsing of the next request. |
| 300 TestHttpClient client; | 360 TestHttpClient client; |
| 301 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 361 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 302 std::string body = "body"; | 362 std::string body = "body"; |
| 303 client.Send(base::StringPrintf( | 363 client.Send(base::StringPrintf( |
| 304 "GET /test HTTP/1.1\r\n" | 364 "GET /test HTTP/1.1\r\n" |
| 305 "Content-Length: %" PRIuS "\r\n\r\n%s", | 365 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 306 body.length(), | 366 body.length(), |
| 307 body.c_str())); | 367 body.c_str())); |
| 308 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 368 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 309 ASSERT_EQ(body, requests_[0].data); | 369 ASSERT_EQ(body, GetRequest(0).data); |
| 370 | |
| 371 int client_connection_id = GetConnectionId(0); | |
| 372 server_->Send200(client_connection_id, "Content for /test", "text/plain"); | |
| 373 std::string response1 = client.Read(); | |
| 374 ASSERT_TRUE(StartsWithASCII(response1, "HTTP/1.1 200 OK", true)); | |
| 375 ASSERT_TRUE(EndsWith(response1, "Content for /test", true)); | |
| 310 | 376 |
| 311 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); | 377 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); |
| 312 ASSERT_TRUE(RunUntilRequestsReceived(2)); | 378 ASSERT_TRUE(RunUntilRequestsReceived(2)); |
| 313 ASSERT_EQ("/test2", requests_[1].path); | 379 ASSERT_EQ("/test2", GetRequest(1).path); |
| 380 | |
| 381 ASSERT_EQ(client_connection_id, GetConnectionId(1)); | |
| 382 server_->Send404(client_connection_id); | |
| 383 std::string response2 = client.Read(); | |
| 384 ASSERT_TRUE(StartsWithASCII(response2, "HTTP/1.1 404 Not Found", true)); | |
| 314 | 385 |
| 315 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); | 386 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); |
| 316 ASSERT_TRUE(RunUntilRequestsReceived(3)); | 387 ASSERT_TRUE(RunUntilRequestsReceived(3)); |
| 317 ASSERT_EQ("/test3", requests_[2].path); | 388 ASSERT_EQ("/test3", GetRequest(2).path); |
| 389 | |
| 390 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | |
| 391 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | |
| 392 std::string response3 = client.Read(); | |
| 393 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | |
| 394 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | |
| 318 } | 395 } |
| 319 | 396 |
| 320 } // namespace net | 397 } // namespace net |
| OLD | NEW |