| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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> |
| 6 |
| 5 #include "base/logging.h" | 7 #include "base/logging.h" |
| 6 #include "base/macros.h" | 8 #include "base/macros.h" |
| 7 #include "base/memory/linked_ptr.h" | 9 #include "base/memory/linked_ptr.h" |
| 8 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 11 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 13 #include "mojo/application/public/cpp/application_connection.h" | 15 #include "mojo/application/public/cpp/application_connection.h" |
| 14 #include "mojo/application/public/cpp/application_impl.h" | 16 #include "mojo/application/public/cpp/application_impl.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 41 NetAddressPtr addr(NetAddress::New()); | 43 NetAddressPtr addr(NetAddress::New()); |
| 42 addr->family = NET_ADDRESS_FAMILY_IPV4; | 44 addr->family = NET_ADDRESS_FAMILY_IPV4; |
| 43 addr->ipv4 = NetAddressIPv4::New(); | 45 addr->ipv4 = NetAddressIPv4::New(); |
| 44 addr->ipv4->port = 0; | 46 addr->ipv4->port = 0; |
| 45 addr->ipv4->addr.resize(4); | 47 addr->ipv4->addr.resize(4); |
| 46 addr->ipv4->addr[0] = 127; | 48 addr->ipv4->addr[0] = 127; |
| 47 addr->ipv4->addr[1] = 0; | 49 addr->ipv4->addr[1] = 0; |
| 48 addr->ipv4->addr[2] = 0; | 50 addr->ipv4->addr[2] = 0; |
| 49 addr->ipv4->addr[3] = 1; | 51 addr->ipv4->addr[3] = 1; |
| 50 | 52 |
| 51 return addr.Pass(); | 53 return addr; |
| 52 } | 54 } |
| 53 | 55 |
| 54 using TestHeaders = std::vector<std::pair<std::string, std::string>>; | 56 using TestHeaders = std::vector<std::pair<std::string, std::string>>; |
| 55 | 57 |
| 56 struct TestRequest { | 58 struct TestRequest { |
| 57 std::string method; | 59 std::string method; |
| 58 std::string url; | 60 std::string url; |
| 59 TestHeaders headers; | 61 TestHeaders headers; |
| 60 scoped_ptr<std::string> body; | 62 scoped_ptr<std::string> body; |
| 61 }; | 63 }; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 79 | 81 |
| 80 HttpResponsePtr MakeResponseStruct(const TestResponse& data) { | 82 HttpResponsePtr MakeResponseStruct(const TestResponse& data) { |
| 81 HttpResponsePtr response(HttpResponse::New()); | 83 HttpResponsePtr response(HttpResponse::New()); |
| 82 response->status_code = data.status_code; | 84 response->status_code = data.status_code; |
| 83 response->headers.resize(data.headers.size()); | 85 response->headers.resize(data.headers.size()); |
| 84 size_t index = 0; | 86 size_t index = 0; |
| 85 for (const auto& item : data.headers) { | 87 for (const auto& item : data.headers) { |
| 86 HttpHeaderPtr header(HttpHeader::New()); | 88 HttpHeaderPtr header(HttpHeader::New()); |
| 87 header->name = item.first; | 89 header->name = item.first; |
| 88 header->value = item.second; | 90 header->value = item.second; |
| 89 response->headers[index++] = header.Pass(); | 91 response->headers[index++] = std::move(header); |
| 90 } | 92 } |
| 91 | 93 |
| 92 if (data.body) { | 94 if (data.body) { |
| 93 uint32_t num_bytes = static_cast<uint32_t>(data.body->size()); | 95 uint32_t num_bytes = static_cast<uint32_t>(data.body->size()); |
| 94 MojoCreateDataPipeOptions options; | 96 MojoCreateDataPipeOptions options; |
| 95 options.struct_size = sizeof(MojoCreateDataPipeOptions); | 97 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 96 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | 98 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 97 options.element_num_bytes = 1; | 99 options.element_num_bytes = 1; |
| 98 options.capacity_num_bytes = num_bytes; | 100 options.capacity_num_bytes = num_bytes; |
| 99 DataPipe data_pipe(options); | 101 DataPipe data_pipe(options); |
| 100 response->body = data_pipe.consumer_handle.Pass(); | 102 response->body = std::move(data_pipe.consumer_handle); |
| 101 MojoResult result = | 103 MojoResult result = |
| 102 WriteDataRaw(data_pipe.producer_handle.get(), data.body->data(), | 104 WriteDataRaw(data_pipe.producer_handle.get(), data.body->data(), |
| 103 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | 105 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); |
| 104 EXPECT_EQ(MOJO_RESULT_OK, result); | 106 EXPECT_EQ(MOJO_RESULT_OK, result); |
| 105 } | 107 } |
| 106 | 108 |
| 107 return response.Pass(); | 109 return response; |
| 108 } | 110 } |
| 109 | 111 |
| 110 void CheckHeaders(const TestHeaders& expected, | 112 void CheckHeaders(const TestHeaders& expected, |
| 111 const Array<HttpHeaderPtr>& headers) { | 113 const Array<HttpHeaderPtr>& headers) { |
| 112 // The server impl fiddles with Content-Length and Content-Type. So we don't | 114 // The server impl fiddles with Content-Length and Content-Type. So we don't |
| 113 // do a strict check here. | 115 // do a strict check here. |
| 114 std::map<std::string, std::string> header_map; | 116 std::map<std::string, std::string> header_map; |
| 115 for (size_t i = 0; i < headers.size(); ++i) { | 117 for (size_t i = 0; i < headers.size(); ++i) { |
| 116 std::string lower_name = | 118 std::string lower_name = |
| 117 base::ToLowerASCII(headers[i]->name.To<std::string>()); | 119 base::ToLowerASCII(headers[i]->name.To<std::string>()); |
| 118 header_map[lower_name] = headers[i]->value; | 120 header_map[lower_name] = headers[i]->value; |
| 119 } | 121 } |
| 120 | 122 |
| 121 for (const auto& item : expected) { | 123 for (const auto& item : expected) { |
| 122 std::string lower_name = base::ToLowerASCII(item.first); | 124 std::string lower_name = base::ToLowerASCII(item.first); |
| 123 EXPECT_NE(header_map.end(), header_map.find(lower_name)); | 125 EXPECT_NE(header_map.end(), header_map.find(lower_name)); |
| 124 EXPECT_EQ(item.second, header_map[lower_name]); | 126 EXPECT_EQ(item.second, header_map[lower_name]); |
| 125 } | 127 } |
| 126 } | 128 } |
| 127 | 129 |
| 128 void CheckRequest(const TestRequest& expected, HttpRequestPtr request) { | 130 void CheckRequest(const TestRequest& expected, HttpRequestPtr request) { |
| 129 EXPECT_EQ(expected.method, request->method); | 131 EXPECT_EQ(expected.method, request->method); |
| 130 EXPECT_EQ(expected.url, request->url); | 132 EXPECT_EQ(expected.url, request->url); |
| 131 CheckHeaders(expected.headers, request->headers); | 133 CheckHeaders(expected.headers, request->headers); |
| 132 if (expected.body) { | 134 if (expected.body) { |
| 133 EXPECT_TRUE(request->body.is_valid()); | 135 EXPECT_TRUE(request->body.is_valid()); |
| 134 std::string body; | 136 std::string body; |
| 135 common::BlockingCopyToString(request->body.Pass(), &body); | 137 common::BlockingCopyToString(std::move(request->body), &body); |
| 136 EXPECT_EQ(*expected.body, body); | 138 EXPECT_EQ(*expected.body, body); |
| 137 } else { | 139 } else { |
| 138 EXPECT_FALSE(request->body.is_valid()); | 140 EXPECT_FALSE(request->body.is_valid()); |
| 139 } | 141 } |
| 140 } | 142 } |
| 141 | 143 |
| 142 void CheckResponse(const TestResponse& expected, const std::string& response) { | 144 void CheckResponse(const TestResponse& expected, const std::string& response) { |
| 143 int header_end = | 145 int header_end = |
| 144 net::HttpUtil::LocateEndOfHeaders(response.c_str(), response.size()); | 146 net::HttpUtil::LocateEndOfHeaders(response.c_str(), response.size()); |
| 145 std::string assembled_headers = | 147 std::string assembled_headers = |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 class WebSocketClientImpl : public WebSocketClient { | 275 class WebSocketClientImpl : public WebSocketClient { |
| 274 public: | 276 public: |
| 275 explicit WebSocketClientImpl() | 277 explicit WebSocketClientImpl() |
| 276 : binding_(this, &client_ptr_), | 278 : binding_(this, &client_ptr_), |
| 277 wait_for_message_count_(0), | 279 wait_for_message_count_(0), |
| 278 run_loop_(nullptr) {} | 280 run_loop_(nullptr) {} |
| 279 ~WebSocketClientImpl() override {} | 281 ~WebSocketClientImpl() override {} |
| 280 | 282 |
| 281 // Establishes a connection from the client side. | 283 // Establishes a connection from the client side. |
| 282 void Connect(WebSocketPtr web_socket, const std::string& url) { | 284 void Connect(WebSocketPtr web_socket, const std::string& url) { |
| 283 web_socket_ = web_socket.Pass(); | 285 web_socket_ = std::move(web_socket); |
| 284 | 286 |
| 285 DataPipe data_pipe; | 287 DataPipe data_pipe; |
| 286 send_stream_ = data_pipe.producer_handle.Pass(); | 288 send_stream_ = std::move(data_pipe.producer_handle); |
| 287 write_send_stream_.reset(new WebSocketWriteQueue(send_stream_.get())); | 289 write_send_stream_.reset(new WebSocketWriteQueue(send_stream_.get())); |
| 288 | 290 |
| 289 web_socket_->Connect(url, Array<String>(0), "http://example.com", | 291 web_socket_->Connect(url, Array<String>(0), "http://example.com", |
| 290 data_pipe.consumer_handle.Pass(), client_ptr_.Pass()); | 292 std::move(data_pipe.consumer_handle), |
| 293 std::move(client_ptr_)); |
| 291 } | 294 } |
| 292 | 295 |
| 293 // Establishes a connection from the server side. | 296 // Establishes a connection from the server side. |
| 294 void AcceptConnectRequest( | 297 void AcceptConnectRequest( |
| 295 const HttpConnectionDelegate::OnReceivedWebSocketRequestCallback& | 298 const HttpConnectionDelegate::OnReceivedWebSocketRequestCallback& |
| 296 callback) { | 299 callback) { |
| 297 InterfaceRequest<WebSocket> web_socket_request = GetProxy(&web_socket_); | 300 InterfaceRequest<WebSocket> web_socket_request = GetProxy(&web_socket_); |
| 298 | 301 |
| 299 DataPipe data_pipe; | 302 DataPipe data_pipe; |
| 300 send_stream_ = data_pipe.producer_handle.Pass(); | 303 send_stream_ = std::move(data_pipe.producer_handle); |
| 301 write_send_stream_.reset(new WebSocketWriteQueue(send_stream_.get())); | 304 write_send_stream_.reset(new WebSocketWriteQueue(send_stream_.get())); |
| 302 | 305 |
| 303 callback.Run(web_socket_request.Pass(), data_pipe.consumer_handle.Pass(), | 306 callback.Run(std::move(web_socket_request), |
| 304 client_ptr_.Pass()); | 307 std::move(data_pipe.consumer_handle), std::move(client_ptr_)); |
| 305 } | 308 } |
| 306 | 309 |
| 307 void WaitForConnectCompletion() { | 310 void WaitForConnectCompletion() { |
| 308 DCHECK(!run_loop_); | 311 DCHECK(!run_loop_); |
| 309 | 312 |
| 310 if (receive_stream_.is_valid()) | 313 if (receive_stream_.is_valid()) |
| 311 return; | 314 return; |
| 312 | 315 |
| 313 base::RunLoop run_loop; | 316 base::RunLoop run_loop; |
| 314 run_loop_ = &run_loop; | 317 run_loop_ = &run_loop; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 338 run_loop_ = nullptr; | 341 run_loop_ = nullptr; |
| 339 } | 342 } |
| 340 | 343 |
| 341 std::vector<std::string>& received_messages() { return received_messages_; } | 344 std::vector<std::string>& received_messages() { return received_messages_; } |
| 342 | 345 |
| 343 private: | 346 private: |
| 344 // WebSocketClient implementation. | 347 // WebSocketClient implementation. |
| 345 void DidConnect(const String& selected_subprotocol, | 348 void DidConnect(const String& selected_subprotocol, |
| 346 const String& extensions, | 349 const String& extensions, |
| 347 ScopedDataPipeConsumerHandle receive_stream) override { | 350 ScopedDataPipeConsumerHandle receive_stream) override { |
| 348 receive_stream_ = receive_stream.Pass(); | 351 receive_stream_ = std::move(receive_stream); |
| 349 read_receive_stream_.reset(new WebSocketReadQueue(receive_stream_.get())); | 352 read_receive_stream_.reset(new WebSocketReadQueue(receive_stream_.get())); |
| 350 | 353 |
| 351 web_socket_->FlowControl(2048); | 354 web_socket_->FlowControl(2048); |
| 352 if (run_loop_) | 355 if (run_loop_) |
| 353 run_loop_->Quit(); | 356 run_loop_->Quit(); |
| 354 } | 357 } |
| 355 | 358 |
| 356 void DidReceiveData(bool fin, | 359 void DidReceiveData(bool fin, |
| 357 WebSocket::MessageType type, | 360 WebSocket::MessageType type, |
| 358 uint32_t num_bytes) override { | 361 uint32_t num_bytes) override { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 | 410 |
| 408 class HttpConnectionDelegateImpl : public HttpConnectionDelegate { | 411 class HttpConnectionDelegateImpl : public HttpConnectionDelegate { |
| 409 public: | 412 public: |
| 410 struct PendingRequest { | 413 struct PendingRequest { |
| 411 HttpRequestPtr request; | 414 HttpRequestPtr request; |
| 412 OnReceivedRequestCallback callback; | 415 OnReceivedRequestCallback callback; |
| 413 }; | 416 }; |
| 414 | 417 |
| 415 HttpConnectionDelegateImpl(HttpConnectionPtr connection, | 418 HttpConnectionDelegateImpl(HttpConnectionPtr connection, |
| 416 InterfaceRequest<HttpConnectionDelegate> request) | 419 InterfaceRequest<HttpConnectionDelegate> request) |
| 417 : connection_(connection.Pass()), | 420 : connection_(std::move(connection)), |
| 418 binding_(this, request.Pass()), | 421 binding_(this, std::move(request)), |
| 419 wait_for_request_count_(0), | 422 wait_for_request_count_(0), |
| 420 run_loop_(nullptr) {} | 423 run_loop_(nullptr) {} |
| 421 ~HttpConnectionDelegateImpl() override {} | 424 ~HttpConnectionDelegateImpl() override {} |
| 422 | 425 |
| 423 // HttpConnectionDelegate implementation: | 426 // HttpConnectionDelegate implementation: |
| 424 void OnReceivedRequest(HttpRequestPtr request, | 427 void OnReceivedRequest(HttpRequestPtr request, |
| 425 const OnReceivedRequestCallback& callback) override { | 428 const OnReceivedRequestCallback& callback) override { |
| 426 linked_ptr<PendingRequest> pending_request(new PendingRequest); | 429 linked_ptr<PendingRequest> pending_request(new PendingRequest); |
| 427 pending_request->request = request.Pass(); | 430 pending_request->request = std::move(request); |
| 428 pending_request->callback = callback; | 431 pending_request->callback = callback; |
| 429 pending_requests_.push_back(pending_request); | 432 pending_requests_.push_back(pending_request); |
| 430 if (run_loop_ && pending_requests_.size() >= wait_for_request_count_) { | 433 if (run_loop_ && pending_requests_.size() >= wait_for_request_count_) { |
| 431 wait_for_request_count_ = 0; | 434 wait_for_request_count_ = 0; |
| 432 run_loop_->Quit(); | 435 run_loop_->Quit(); |
| 433 } | 436 } |
| 434 } | 437 } |
| 435 | 438 |
| 436 void OnReceivedWebSocketRequest( | 439 void OnReceivedWebSocketRequest( |
| 437 HttpRequestPtr request, | 440 HttpRequestPtr request, |
| 438 const OnReceivedWebSocketRequestCallback& callback) override { | 441 const OnReceivedWebSocketRequestCallback& callback) override { |
| 439 web_socket_.reset(new WebSocketClientImpl()); | 442 web_socket_.reset(new WebSocketClientImpl()); |
| 440 | 443 |
| 441 web_socket_->AcceptConnectRequest(callback); | 444 web_socket_->AcceptConnectRequest(callback); |
| 442 | 445 |
| 443 if (run_loop_) | 446 if (run_loop_) |
| 444 run_loop_->Quit(); | 447 run_loop_->Quit(); |
| 445 } | 448 } |
| 446 | 449 |
| 447 void SendResponse(HttpResponsePtr response) { | 450 void SendResponse(HttpResponsePtr response) { |
| 448 ASSERT_FALSE(pending_requests_.empty()); | 451 ASSERT_FALSE(pending_requests_.empty()); |
| 449 linked_ptr<PendingRequest> request = pending_requests_[0]; | 452 linked_ptr<PendingRequest> request = pending_requests_[0]; |
| 450 pending_requests_.erase(pending_requests_.begin()); | 453 pending_requests_.erase(pending_requests_.begin()); |
| 451 request->callback.Run(response.Pass()); | 454 request->callback.Run(std::move(response)); |
| 452 } | 455 } |
| 453 | 456 |
| 454 void WaitForRequest(size_t count) { | 457 void WaitForRequest(size_t count) { |
| 455 DCHECK(!run_loop_); | 458 DCHECK(!run_loop_); |
| 456 | 459 |
| 457 if (pending_requests_.size() >= count) | 460 if (pending_requests_.size() >= count) |
| 458 return; | 461 return; |
| 459 | 462 |
| 460 wait_for_request_count_ = count; | 463 wait_for_request_count_ = count; |
| 461 base::RunLoop run_loop; | 464 base::RunLoop run_loop; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 public: | 502 public: |
| 500 explicit HttpServerDelegateImpl(HttpServerDelegatePtr* delegate_ptr) | 503 explicit HttpServerDelegateImpl(HttpServerDelegatePtr* delegate_ptr) |
| 501 : binding_(this, delegate_ptr), | 504 : binding_(this, delegate_ptr), |
| 502 wait_for_connection_count_(0), | 505 wait_for_connection_count_(0), |
| 503 run_loop_(nullptr) {} | 506 run_loop_(nullptr) {} |
| 504 ~HttpServerDelegateImpl() override {} | 507 ~HttpServerDelegateImpl() override {} |
| 505 | 508 |
| 506 // HttpServerDelegate implementation. | 509 // HttpServerDelegate implementation. |
| 507 void OnConnected(HttpConnectionPtr connection, | 510 void OnConnected(HttpConnectionPtr connection, |
| 508 InterfaceRequest<HttpConnectionDelegate> delegate) override { | 511 InterfaceRequest<HttpConnectionDelegate> delegate) override { |
| 509 connections_.push_back(make_linked_ptr( | 512 connections_.push_back(make_linked_ptr(new HttpConnectionDelegateImpl( |
| 510 new HttpConnectionDelegateImpl(connection.Pass(), delegate.Pass()))); | 513 std::move(connection), std::move(delegate)))); |
| 511 if (run_loop_ && connections_.size() >= wait_for_connection_count_) { | 514 if (run_loop_ && connections_.size() >= wait_for_connection_count_) { |
| 512 wait_for_connection_count_ = 0; | 515 wait_for_connection_count_ = 0; |
| 513 run_loop_->Quit(); | 516 run_loop_->Quit(); |
| 514 } | 517 } |
| 515 } | 518 } |
| 516 | 519 |
| 517 void WaitForConnection(size_t count) { | 520 void WaitForConnection(size_t count) { |
| 518 DCHECK(!run_loop_); | 521 DCHECK(!run_loop_); |
| 519 | 522 |
| 520 if (connections_.size() >= count) | 523 if (connections_.size() >= count) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 | 557 |
| 555 scoped_ptr<ApplicationConnection> connection = | 558 scoped_ptr<ApplicationConnection> connection = |
| 556 application_impl()->ConnectToApplication("mojo:network_service"); | 559 application_impl()->ConnectToApplication("mojo:network_service"); |
| 557 connection->ConnectToService(&network_service_); | 560 connection->ConnectToService(&network_service_); |
| 558 connection->ConnectToService(&web_socket_factory_); | 561 connection->ConnectToService(&web_socket_factory_); |
| 559 } | 562 } |
| 560 | 563 |
| 561 void CreateHttpServer(HttpServerDelegatePtr delegate, | 564 void CreateHttpServer(HttpServerDelegatePtr delegate, |
| 562 NetAddressPtr* out_bound_to) { | 565 NetAddressPtr* out_bound_to) { |
| 563 network_service_->CreateHttpServer( | 566 network_service_->CreateHttpServer( |
| 564 GetLocalHostWithAnyPort(), delegate.Pass(), | 567 GetLocalHostWithAnyPort(), std::move(delegate), |
| 565 [out_bound_to](NetworkErrorPtr result, NetAddressPtr bound_to) { | 568 [out_bound_to](NetworkErrorPtr result, NetAddressPtr bound_to) { |
| 566 ASSERT_EQ(net::OK, result->code); | 569 ASSERT_EQ(net::OK, result->code); |
| 567 EXPECT_NE(0u, bound_to->ipv4->port); | 570 EXPECT_NE(0u, bound_to->ipv4->port); |
| 568 *out_bound_to = bound_to.Pass(); | 571 *out_bound_to = std::move(bound_to); |
| 569 }); | 572 }); |
| 570 network_service_.WaitForIncomingResponse(); | 573 network_service_.WaitForIncomingResponse(); |
| 571 } | 574 } |
| 572 | 575 |
| 573 NetworkServicePtr network_service_; | 576 NetworkServicePtr network_service_; |
| 574 WebSocketFactoryPtr web_socket_factory_; | 577 WebSocketFactoryPtr web_socket_factory_; |
| 575 | 578 |
| 576 private: | 579 private: |
| 577 base::MessageLoop message_loop_; | 580 base::MessageLoop message_loop_; |
| 578 | 581 |
| 579 DISALLOW_COPY_AND_ASSIGN(HttpServerAppTest); | 582 DISALLOW_COPY_AND_ASSIGN(HttpServerAppTest); |
| 580 }; | 583 }; |
| 581 | 584 |
| 582 } // namespace | 585 } // namespace |
| 583 | 586 |
| 584 TEST_F(HttpServerAppTest, BasicHttpRequestResponse) { | 587 TEST_F(HttpServerAppTest, BasicHttpRequestResponse) { |
| 585 NetAddressPtr bound_to; | 588 NetAddressPtr bound_to; |
| 586 HttpServerDelegatePtr server_delegate_ptr; | 589 HttpServerDelegatePtr server_delegate_ptr; |
| 587 HttpServerDelegateImpl server_delegate_impl(&server_delegate_ptr); | 590 HttpServerDelegateImpl server_delegate_impl(&server_delegate_ptr); |
| 588 CreateHttpServer(server_delegate_ptr.Pass(), &bound_to); | 591 CreateHttpServer(std::move(server_delegate_ptr), &bound_to); |
| 589 | 592 |
| 590 TestHttpClient client; | 593 TestHttpClient client; |
| 591 client.Connect(bound_to.To<net::IPEndPoint>()); | 594 client.Connect(bound_to.To<net::IPEndPoint>()); |
| 592 | 595 |
| 593 server_delegate_impl.WaitForConnection(1); | 596 server_delegate_impl.WaitForConnection(1); |
| 594 HttpConnectionDelegateImpl& connection = | 597 HttpConnectionDelegateImpl& connection = |
| 595 *server_delegate_impl.connections()[0]; | 598 *server_delegate_impl.connections()[0]; |
| 596 | 599 |
| 597 TestRequest request_data = {"HEAD", "/test", {{"Hello", "World"}}, nullptr}; | 600 TestRequest request_data = {"HEAD", "/test", {{"Hello", "World"}}, nullptr}; |
| 598 client.Send(MakeRequestMessage(request_data)); | 601 client.Send(MakeRequestMessage(request_data)); |
| 599 | 602 |
| 600 connection.WaitForRequest(1); | 603 connection.WaitForRequest(1); |
| 601 | 604 |
| 602 CheckRequest(request_data, connection.pending_requests()[0]->request.Pass()); | 605 CheckRequest(request_data, |
| 606 std::move(connection.pending_requests()[0]->request)); |
| 603 | 607 |
| 604 TestResponse response_data = {200, {{"Content-Length", "4"}}, nullptr}; | 608 TestResponse response_data = {200, {{"Content-Length", "4"}}, nullptr}; |
| 605 connection.SendResponse(MakeResponseStruct(response_data)); | 609 connection.SendResponse(MakeResponseStruct(response_data)); |
| 606 // This causes the underlying TCP connection to be closed. The client can | 610 // This causes the underlying TCP connection to be closed. The client can |
| 607 // determine the end of the response based on that. | 611 // determine the end of the response based on that. |
| 608 server_delegate_impl.connections().clear(); | 612 server_delegate_impl.connections().clear(); |
| 609 | 613 |
| 610 std::string response_message; | 614 std::string response_message; |
| 611 client.ReadResponse(&response_message); | 615 client.ReadResponse(&response_message); |
| 612 | 616 |
| 613 CheckResponse(response_data, response_message); | 617 CheckResponse(response_data, response_message); |
| 614 } | 618 } |
| 615 | 619 |
| 616 TEST_F(HttpServerAppTest, HttpRequestResponseWithBody) { | 620 TEST_F(HttpServerAppTest, HttpRequestResponseWithBody) { |
| 617 NetAddressPtr bound_to; | 621 NetAddressPtr bound_to; |
| 618 HttpServerDelegatePtr server_delegate_ptr; | 622 HttpServerDelegatePtr server_delegate_ptr; |
| 619 HttpServerDelegateImpl server_delegate_impl(&server_delegate_ptr); | 623 HttpServerDelegateImpl server_delegate_impl(&server_delegate_ptr); |
| 620 CreateHttpServer(server_delegate_ptr.Pass(), &bound_to); | 624 CreateHttpServer(std::move(server_delegate_ptr), &bound_to); |
| 621 | 625 |
| 622 TestHttpClient client; | 626 TestHttpClient client; |
| 623 client.Connect(bound_to.To<net::IPEndPoint>()); | 627 client.Connect(bound_to.To<net::IPEndPoint>()); |
| 624 | 628 |
| 625 server_delegate_impl.WaitForConnection(1); | 629 server_delegate_impl.WaitForConnection(1); |
| 626 HttpConnectionDelegateImpl& connection = | 630 HttpConnectionDelegateImpl& connection = |
| 627 *server_delegate_impl.connections()[0]; | 631 *server_delegate_impl.connections()[0]; |
| 628 | 632 |
| 629 TestRequest request_data = { | 633 TestRequest request_data = { |
| 630 "Post", | 634 "Post", |
| 631 "/test", | 635 "/test", |
| 632 {{"Hello", "World"}, | 636 {{"Hello", "World"}, |
| 633 {"Content-Length", "23"}, | 637 {"Content-Length", "23"}, |
| 634 {"Content-Type", "text/plain"}}, | 638 {"Content-Type", "text/plain"}}, |
| 635 make_scoped_ptr(new std::string("This is a test request!"))}; | 639 make_scoped_ptr(new std::string("This is a test request!"))}; |
| 636 client.Send(MakeRequestMessage(request_data)); | 640 client.Send(MakeRequestMessage(request_data)); |
| 637 | 641 |
| 638 connection.WaitForRequest(1); | 642 connection.WaitForRequest(1); |
| 639 | 643 |
| 640 CheckRequest(request_data, connection.pending_requests()[0]->request.Pass()); | 644 CheckRequest(request_data, |
| 645 std::move(connection.pending_requests()[0]->request)); |
| 641 | 646 |
| 642 TestResponse response_data = { | 647 TestResponse response_data = { |
| 643 200, | 648 200, |
| 644 {{"Content-Length", "26"}}, | 649 {{"Content-Length", "26"}}, |
| 645 make_scoped_ptr(new std::string("This is a test response..."))}; | 650 make_scoped_ptr(new std::string("This is a test response..."))}; |
| 646 connection.SendResponse(MakeResponseStruct(response_data)); | 651 connection.SendResponse(MakeResponseStruct(response_data)); |
| 647 | 652 |
| 648 std::string response_message; | 653 std::string response_message; |
| 649 client.ReadResponse(&response_message); | 654 client.ReadResponse(&response_message); |
| 650 | 655 |
| 651 CheckResponse(response_data, response_message); | 656 CheckResponse(response_data, response_message); |
| 652 } | 657 } |
| 653 | 658 |
| 654 TEST_F(HttpServerAppTest, WebSocket) { | 659 TEST_F(HttpServerAppTest, WebSocket) { |
| 655 NetAddressPtr bound_to; | 660 NetAddressPtr bound_to; |
| 656 HttpServerDelegatePtr server_delegate_ptr; | 661 HttpServerDelegatePtr server_delegate_ptr; |
| 657 HttpServerDelegateImpl server_delegate_impl(&server_delegate_ptr); | 662 HttpServerDelegateImpl server_delegate_impl(&server_delegate_ptr); |
| 658 CreateHttpServer(server_delegate_ptr.Pass(), &bound_to); | 663 CreateHttpServer(std::move(server_delegate_ptr), &bound_to); |
| 659 | 664 |
| 660 WebSocketPtr web_socket_ptr; | 665 WebSocketPtr web_socket_ptr; |
| 661 web_socket_factory_->CreateWebSocket(GetProxy(&web_socket_ptr)); | 666 web_socket_factory_->CreateWebSocket(GetProxy(&web_socket_ptr)); |
| 662 WebSocketClientImpl socket_0; | 667 WebSocketClientImpl socket_0; |
| 663 socket_0.Connect( | 668 socket_0.Connect( |
| 664 web_socket_ptr.Pass(), | 669 std::move(web_socket_ptr), |
| 665 base::StringPrintf("ws://127.0.0.1:%d/hello", bound_to->ipv4->port)); | 670 base::StringPrintf("ws://127.0.0.1:%d/hello", bound_to->ipv4->port)); |
| 666 | 671 |
| 667 server_delegate_impl.WaitForConnection(1); | 672 server_delegate_impl.WaitForConnection(1); |
| 668 HttpConnectionDelegateImpl& connection = | 673 HttpConnectionDelegateImpl& connection = |
| 669 *server_delegate_impl.connections()[0]; | 674 *server_delegate_impl.connections()[0]; |
| 670 | 675 |
| 671 connection.WaitForWebSocketRequest(); | 676 connection.WaitForWebSocketRequest(); |
| 672 WebSocketClientImpl& socket_1 = *connection.web_socket(); | 677 WebSocketClientImpl& socket_1 = *connection.web_socket(); |
| 673 | 678 |
| 674 socket_1.WaitForConnectCompletion(); | 679 socket_1.WaitForConnectCompletion(); |
| 675 socket_0.WaitForConnectCompletion(); | 680 socket_0.WaitForConnectCompletion(); |
| 676 | 681 |
| 677 socket_0.Send("Hello"); | 682 socket_0.Send("Hello"); |
| 678 socket_0.Send("world!"); | 683 socket_0.Send("world!"); |
| 679 | 684 |
| 680 socket_1.WaitForMessage(2); | 685 socket_1.WaitForMessage(2); |
| 681 EXPECT_EQ("Hello", socket_1.received_messages()[0]); | 686 EXPECT_EQ("Hello", socket_1.received_messages()[0]); |
| 682 EXPECT_EQ("world!", socket_1.received_messages()[1]); | 687 EXPECT_EQ("world!", socket_1.received_messages()[1]); |
| 683 | 688 |
| 684 socket_1.Send("How do"); | 689 socket_1.Send("How do"); |
| 685 socket_1.Send("you do?"); | 690 socket_1.Send("you do?"); |
| 686 | 691 |
| 687 socket_0.WaitForMessage(2); | 692 socket_0.WaitForMessage(2); |
| 688 EXPECT_EQ("How do", socket_0.received_messages()[0]); | 693 EXPECT_EQ("How do", socket_0.received_messages()[0]); |
| 689 EXPECT_EQ("you do?", socket_0.received_messages()[1]); | 694 EXPECT_EQ("you do?", socket_0.received_messages()[1]); |
| 690 } | 695 } |
| 691 | 696 |
| 692 } // namespace mojo | 697 } // namespace mojo |
| OLD | NEW |