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 "net/server/http_server.h" | 5 #include "net/server/http_server.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 #include "net/test/gtest_util.h" | 43 #include "net/test/gtest_util.h" |
44 #include "net/url_request/url_fetcher.h" | 44 #include "net/url_request/url_fetcher.h" |
45 #include "net/url_request/url_fetcher_delegate.h" | 45 #include "net/url_request/url_fetcher_delegate.h" |
46 #include "net/url_request/url_request_context.h" | 46 #include "net/url_request/url_request_context.h" |
47 #include "net/url_request/url_request_context_getter.h" | 47 #include "net/url_request/url_request_context_getter.h" |
48 #include "net/url_request/url_request_test_util.h" | 48 #include "net/url_request/url_request_test_util.h" |
49 #include "testing/gmock/include/gmock/gmock.h" | 49 #include "testing/gmock/include/gmock/gmock.h" |
50 #include "testing/gtest/include/gtest/gtest.h" | 50 #include "testing/gtest/include/gtest/gtest.h" |
51 | 51 |
52 using net::test::IsOk; | 52 using net::test::IsOk; |
53 using testing::_; | |
53 | 54 |
54 namespace net { | 55 namespace net { |
55 | 56 |
56 namespace { | 57 namespace { |
57 | 58 |
58 const int kMaxExpectedResponseLength = 2048; | 59 const int kMaxExpectedResponseLength = 2048; |
59 | 60 |
60 void SetTimedOutAndQuitLoop(const base::WeakPtr<bool> timed_out, | 61 void SetTimedOutAndQuitLoop(const base::WeakPtr<bool> timed_out, |
61 const base::Closure& quit_loop_func) { | 62 const base::Closure& quit_loop_func) { |
62 if (timed_out) { | 63 if (timed_out) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
176 HttpUtil::AssembleRawHeaders(response.data(), end_of_headers))); | 177 HttpUtil::AssembleRawHeaders(response.data(), end_of_headers))); |
177 return body_size >= headers->GetContentLength(); | 178 return body_size >= headers->GetContentLength(); |
178 } | 179 } |
179 | 180 |
180 scoped_refptr<IOBufferWithSize> read_buffer_; | 181 scoped_refptr<IOBufferWithSize> read_buffer_; |
181 scoped_refptr<DrainableIOBuffer> write_buffer_; | 182 scoped_refptr<DrainableIOBuffer> write_buffer_; |
182 std::unique_ptr<TCPClientSocket> socket_; | 183 std::unique_ptr<TCPClientSocket> socket_; |
183 int connect_result_; | 184 int connect_result_; |
184 }; | 185 }; |
185 | 186 |
187 class MockHttpServerDelegate : public HttpServer::Delegate { | |
188 public: | |
189 MOCK_METHOD1(OnConnect, void(int connection_id)); | |
190 MOCK_METHOD2(OnHttpRequest, | |
191 void(int connection_id, const HttpServerRequestInfo& info)); | |
192 MOCK_METHOD2(OnWebSocketRequest, | |
193 void(int connection_id, const HttpServerRequestInfo& info)); | |
194 MOCK_METHOD2(OnWebSocketMessage, | |
195 void(int connection_id, const std::string& data)); | |
196 MOCK_METHOD1(OnClose, void(int connection_id)); | |
197 }; | |
mmenke
2016/09/12 20:35:04
We try not to use Gmock in net - it makes tests si
slan
2016/09/14 15:50:57
OK, I removed the use of GMock and added checks fo
| |
198 | |
186 } // namespace | 199 } // namespace |
187 | 200 |
188 class HttpServerTest : public testing::Test, | 201 class HttpServerTest : public testing::Test, |
189 public HttpServer::Delegate { | 202 public HttpServer::Delegate { |
190 public: | 203 public: |
191 HttpServerTest() : quit_after_request_count_(0) {} | 204 HttpServerTest() : quit_after_request_count_(0) {} |
192 | 205 |
193 void SetUp() override { | 206 void SetUp() override { |
194 std::unique_ptr<ServerSocket> server_socket( | 207 std::unique_ptr<ServerSocket> server_socket( |
195 new TCPServerSocket(NULL, NetLog::Source())); | 208 new TCPServerSocket(NULL, NetLog::Source())); |
196 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); | 209 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); |
197 server_.reset(new HttpServer(std::move(server_socket), this)); | 210 server_.reset(new HttpServer(std::move(server_socket), this)); |
198 ASSERT_THAT(server_->GetLocalAddress(&server_address_), IsOk()); | 211 ASSERT_THAT(server_->GetLocalAddress(&server_address_), IsOk()); |
199 } | 212 } |
200 | 213 |
201 void OnConnect(int connection_id) override {} | 214 void OnConnect(int connection_id) override { |
215 mock_delegate_.OnConnect(connection_id); | |
216 } | |
202 | 217 |
203 void OnHttpRequest(int connection_id, | 218 void OnHttpRequest(int connection_id, |
204 const HttpServerRequestInfo& info) override { | 219 const HttpServerRequestInfo& info) override { |
205 requests_.push_back(std::make_pair(info, connection_id)); | 220 requests_.push_back(std::make_pair(info, connection_id)); |
206 if (requests_.size() == quit_after_request_count_) | 221 if (requests_.size() == quit_after_request_count_) |
207 run_loop_quit_func_.Run(); | 222 run_loop_quit_func_.Run(); |
223 mock_delegate_.OnHttpRequest(connection_id, info); | |
208 } | 224 } |
209 | 225 |
210 void OnWebSocketRequest(int connection_id, | 226 void OnWebSocketRequest(int connection_id, |
211 const HttpServerRequestInfo& info) override { | 227 const HttpServerRequestInfo& info) override { |
212 NOTREACHED(); | 228 NOTREACHED(); |
213 } | 229 } |
214 | 230 |
215 void OnWebSocketMessage(int connection_id, const std::string& data) override { | 231 void OnWebSocketMessage(int connection_id, const std::string& data) override { |
216 NOTREACHED(); | 232 NOTREACHED(); |
217 } | 233 } |
218 | 234 |
219 void OnClose(int connection_id) override {} | 235 void OnClose(int connection_id) override { |
236 mock_delegate_.OnClose(connection_id); | |
237 } | |
220 | 238 |
221 bool RunUntilRequestsReceived(size_t count) { | 239 bool RunUntilRequestsReceived(size_t count) { |
222 quit_after_request_count_ = count; | 240 quit_after_request_count_ = count; |
223 if (requests_.size() == count) | 241 if (requests_.size() == count) |
224 return true; | 242 return true; |
225 | 243 |
226 base::RunLoop run_loop; | 244 base::RunLoop run_loop; |
227 run_loop_quit_func_ = run_loop.QuitClosure(); | 245 run_loop_quit_func_ = run_loop.QuitClosure(); |
228 bool success = RunLoopWithTimeout(&run_loop); | 246 bool success = RunLoopWithTimeout(&run_loop); |
229 run_loop_quit_func_.Reset(); | 247 run_loop_quit_func_.Reset(); |
(...skipping 11 matching lines...) Expand all Loading... | |
241 void HandleAcceptResult(std::unique_ptr<StreamSocket> socket) { | 259 void HandleAcceptResult(std::unique_ptr<StreamSocket> socket) { |
242 server_->accepted_socket_.reset(socket.release()); | 260 server_->accepted_socket_.reset(socket.release()); |
243 server_->HandleAcceptResult(OK); | 261 server_->HandleAcceptResult(OK); |
244 } | 262 } |
245 | 263 |
246 protected: | 264 protected: |
247 std::unique_ptr<HttpServer> server_; | 265 std::unique_ptr<HttpServer> server_; |
248 IPEndPoint server_address_; | 266 IPEndPoint server_address_; |
249 base::Closure run_loop_quit_func_; | 267 base::Closure run_loop_quit_func_; |
250 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; | 268 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; |
269 testing::NiceMock<MockHttpServerDelegate> mock_delegate_; | |
251 | 270 |
252 private: | 271 private: |
253 size_t quit_after_request_count_; | 272 size_t quit_after_request_count_; |
254 }; | 273 }; |
255 | 274 |
256 namespace { | 275 namespace { |
257 | 276 |
258 class WebSocketTest : public HttpServerTest { | 277 class WebSocketTest : public HttpServerTest { |
259 void OnHttpRequest(int connection_id, | 278 void OnHttpRequest(int connection_id, |
260 const HttpServerRequestInfo& info) override { | 279 const HttpServerRequestInfo& info) override { |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
465 server_->SendRaw(GetConnectionId(0), "Raw Data "); | 484 server_->SendRaw(GetConnectionId(0), "Raw Data "); |
466 server_->SendRaw(GetConnectionId(0), "More Data"); | 485 server_->SendRaw(GetConnectionId(0), "More Data"); |
467 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); | 486 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); |
468 | 487 |
469 const std::string expected_response("Raw Data More DataThird Piece of Data"); | 488 const std::string expected_response("Raw Data More DataThird Piece of Data"); |
470 std::string response; | 489 std::string response; |
471 ASSERT_TRUE(client.Read(&response, expected_response.length())); | 490 ASSERT_TRUE(client.Read(&response, expected_response.length())); |
472 ASSERT_EQ(expected_response, response); | 491 ASSERT_EQ(expected_response, response); |
473 } | 492 } |
474 | 493 |
494 TEST_F(HttpServerTest, SendWrongProtocolRequest) { | |
495 EXPECT_CALL(mock_delegate_, OnConnect(_)).Times(1); | |
496 EXPECT_CALL(mock_delegate_, OnClose(_)).Times(1); | |
497 | |
498 // Tests that non-HTTP/1.1 requests are simply ignored. | |
499 TestHttpClient client; | |
500 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | |
501 | |
502 client.Send("GET /test HTTP/1.0\r\n\r\n"); | |
503 ASSERT_FALSE(RunUntilRequestsReceived(1)); | |
mmenke
2016/09/12 20:35:04
Maybe also a test with "GET /test foo\r\n\r\n" and
slan
2016/09/14 15:50:57
Done.
| |
504 } | |
505 | |
475 class MockStreamSocket : public StreamSocket { | 506 class MockStreamSocket : public StreamSocket { |
476 public: | 507 public: |
477 MockStreamSocket() | 508 MockStreamSocket() |
478 : connected_(true), | 509 : connected_(true), |
479 read_buf_(NULL), | 510 read_buf_(NULL), |
480 read_buf_len_(0) {} | 511 read_buf_len_(0) {} |
481 | 512 |
482 // StreamSocket | 513 // StreamSocket |
483 int Connect(const CompletionCallback& callback) override { | 514 int Connect(const CompletionCallback& callback) override { |
484 return ERR_NOT_IMPLEMENTED; | 515 return ERR_NOT_IMPLEMENTED; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
653 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 684 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
654 client.Send("GET / HTTP/1.1\r\n\r\n"); | 685 client.Send("GET / HTTP/1.1\r\n\r\n"); |
655 ASSERT_FALSE(RunUntilRequestsReceived(1)); | 686 ASSERT_FALSE(RunUntilRequestsReceived(1)); |
656 ASSERT_EQ(1ul, connection_ids_.size()); | 687 ASSERT_EQ(1ul, connection_ids_.size()); |
657 ASSERT_EQ(0ul, requests_.size()); | 688 ASSERT_EQ(0ul, requests_.size()); |
658 } | 689 } |
659 | 690 |
660 } // namespace | 691 } // namespace |
661 | 692 |
662 } // namespace net | 693 } // namespace net |
OLD | NEW |