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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "mojo/data_pipe_utils/data_pipe_utils.h" | 8 #include "mojo/data_pipe_utils/data_pipe_utils.h" |
9 #include "mojo/public/cpp/application/application_impl.h" | 9 #include "mojo/public/cpp/application/application_impl.h" |
10 #include "mojo/public/cpp/application/application_test_base.h" | 10 #include "mojo/public/cpp/application/application_test_base.h" |
11 #include "mojo/public/cpp/application/connect.h" | 11 #include "mojo/public/cpp/application/connect.h" |
| 12 #include "mojo/public/cpp/bindings/synchronous_interface_ptr.h" |
12 #include "mojo/public/cpp/system/macros.h" | 13 #include "mojo/public/cpp/system/macros.h" |
13 #include "mojo/services/http_server/cpp/http_server_util.h" | 14 #include "mojo/services/http_server/cpp/http_server_util.h" |
14 #include "mojo/services/http_server/interfaces/http_server.mojom.h" | 15 #include "mojo/services/http_server/interfaces/http_server.mojom-sync.h" |
15 #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h" | 16 #include "mojo/services/http_server/interfaces/http_server_factory.mojom.h" |
16 #include "mojo/services/network/interfaces/net_address.mojom.h" | 17 #include "mojo/services/network/interfaces/net_address.mojom.h" |
17 #include "mojo/services/network/interfaces/network_service.mojom.h" | 18 #include "mojo/services/network/interfaces/network_service.mojom.h" |
18 #include "mojo/services/network/interfaces/url_loader.mojom.h" | 19 #include "mojo/services/network/interfaces/url_loader.mojom.h" |
19 | 20 |
20 namespace http_server { | 21 namespace http_server { |
21 | 22 |
22 namespace { | 23 namespace { |
| 24 |
23 void WriteMessageToDataPipe( | 25 void WriteMessageToDataPipe( |
24 const std::string message, | 26 const std::string message, |
25 mojo::ScopedDataPipeConsumerHandle* data_pipe_consumer) { | 27 mojo::ScopedDataPipeConsumerHandle* data_pipe_consumer) { |
26 mojo::ScopedDataPipeProducerHandle producer_handle_; | 28 mojo::ScopedDataPipeProducerHandle producer_handle_; |
27 MojoCreateDataPipeOptions options = {sizeof(MojoCreateDataPipeOptions), | 29 MojoCreateDataPipeOptions options = {sizeof(MojoCreateDataPipeOptions), |
28 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, | 30 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, |
29 1, | 31 1, |
30 static_cast<uint32_t>(message.size())}; | 32 static_cast<uint32_t>(message.size())}; |
31 | 33 |
32 MojoResult result = | 34 MojoResult result = |
33 CreateDataPipe(&options, &producer_handle_, data_pipe_consumer); | 35 CreateDataPipe(&options, &producer_handle_, data_pipe_consumer); |
34 ASSERT_EQ(MOJO_RESULT_OK, result); | 36 ASSERT_EQ(MOJO_RESULT_OK, result); |
35 | 37 |
36 uint32_t bytes = message.size(); | 38 uint32_t bytes = message.size(); |
37 result = WriteDataRaw(producer_handle_.get(), message.data(), &bytes, | 39 result = WriteDataRaw(producer_handle_.get(), message.data(), &bytes, |
38 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | 40 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); |
39 ASSERT_EQ(result, MOJO_RESULT_OK); | 41 ASSERT_EQ(result, MOJO_RESULT_OK); |
40 ASSERT_EQ(message.size(), bytes); | 42 ASSERT_EQ(message.size(), bytes); |
41 } | 43 } |
42 | 44 |
43 const char* kExampleMessage = "Hello, world!"; | 45 const char kExampleMessage[] = "Hello, world!"; |
| 46 |
44 } // namespace | 47 } // namespace |
45 | 48 |
46 // Test handler that responds to all requests with the status OK and | 49 // Test handler that responds to all requests with the status OK and |
47 // kExampleMessage. | 50 // kExampleMessage. |
48 class GetHandler : public http_server::HttpHandler { | 51 class GetHandler : public http_server::HttpHandler { |
49 public: | 52 public: |
50 GetHandler(mojo::InterfaceRequest<HttpHandler> request) | 53 GetHandler(mojo::InterfaceRequest<HttpHandler> request) |
51 : binding_(this, request.Pass()) {} | 54 : binding_(this, request.Pass()) {} |
52 ~GetHandler() override {} | 55 ~GetHandler() override {} |
53 | 56 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 // ApplicationTestBase: | 100 // ApplicationTestBase: |
98 void SetUp() override { | 101 void SetUp() override { |
99 ApplicationTestBase::SetUp(); | 102 ApplicationTestBase::SetUp(); |
100 | 103 |
101 mojo::ConnectToService(application_impl()->shell(), "mojo:http_server", | 104 mojo::ConnectToService(application_impl()->shell(), "mojo:http_server", |
102 GetProxy(&http_server_factory_)); | 105 GetProxy(&http_server_factory_)); |
103 mojo::ConnectToService(application_impl()->shell(), "mojo:network_service", | 106 mojo::ConnectToService(application_impl()->shell(), "mojo:network_service", |
104 GetProxy(&network_service_)); | 107 GetProxy(&network_service_)); |
105 } | 108 } |
106 | 109 |
107 http_server::HttpServerPtr CreateHttpServer(); | 110 mojo::SynchronousInterfacePtr<http_server::HttpServer> CreateHttpServer(); |
108 | 111 |
109 http_server::HttpServerFactoryPtr http_server_factory_; | 112 http_server::HttpServerFactoryPtr http_server_factory_; |
110 mojo::NetworkServicePtr network_service_; | 113 mojo::NetworkServicePtr network_service_; |
111 | 114 |
112 private: | 115 private: |
113 MOJO_DISALLOW_COPY_AND_ASSIGN(HttpServerApplicationTest); | 116 MOJO_DISALLOW_COPY_AND_ASSIGN(HttpServerApplicationTest); |
114 }; | 117 }; |
115 | 118 |
116 http_server::HttpServerPtr HttpServerApplicationTest::CreateHttpServer() { | 119 mojo::SynchronousInterfacePtr<http_server::HttpServer> |
117 http_server::HttpServerPtr http_server; | 120 HttpServerApplicationTest::CreateHttpServer() { |
| 121 mojo::SynchronousInterfacePtr<http_server::HttpServer> http_server; |
118 mojo::NetAddressPtr local_address(mojo::NetAddress::New()); | 122 mojo::NetAddressPtr local_address(mojo::NetAddress::New()); |
119 local_address->family = mojo::NetAddressFamily::IPV4; | 123 local_address->family = mojo::NetAddressFamily::IPV4; |
120 local_address->ipv4 = mojo::NetAddressIPv4::New(); | 124 local_address->ipv4 = mojo::NetAddressIPv4::New(); |
121 local_address->ipv4->addr.resize(4); | 125 local_address->ipv4->addr.resize(4); |
122 local_address->ipv4->addr[0] = 127; | 126 local_address->ipv4->addr[0] = 127; |
123 local_address->ipv4->addr[1] = 0; | 127 local_address->ipv4->addr[1] = 0; |
124 local_address->ipv4->addr[2] = 0; | 128 local_address->ipv4->addr[2] = 0; |
125 local_address->ipv4->addr[3] = 1; | 129 local_address->ipv4->addr[3] = 1; |
126 local_address->ipv4->port = 0; | 130 local_address->ipv4->port = 0; |
127 http_server_factory_->CreateHttpServer(GetProxy(&http_server).Pass(), | 131 http_server_factory_->CreateHttpServer(GetSynchronousProxy(&http_server), |
128 local_address.Pass()); | 132 local_address.Pass()); |
129 return http_server; | 133 return http_server; |
130 } | 134 } |
131 | 135 |
132 void CheckServerResponse(mojo::URLResponsePtr response) { | 136 void CheckServerResponse(mojo::URLResponsePtr response) { |
133 EXPECT_EQ(200u, response->status_code); | 137 EXPECT_EQ(200u, response->status_code); |
134 std::string response_body; | 138 std::string response_body; |
135 mojo::common::BlockingCopyToString(response->body.Pass(), &response_body); | 139 mojo::common::BlockingCopyToString(response->body.Pass(), &response_body); |
136 EXPECT_EQ(kExampleMessage, response_body); | 140 EXPECT_EQ(kExampleMessage, response_body); |
137 base::MessageLoop::current()->Quit(); | 141 base::MessageLoop::current()->Quit(); |
138 } | 142 } |
139 | 143 |
140 // Verifies that the server responds to http GET requests using example | 144 // Verifies that the server responds to http GET requests using example |
141 // GetHandler. | 145 // GetHandler. |
142 TEST_F(HttpServerApplicationTest, ServerResponse) { | 146 TEST_F(HttpServerApplicationTest, ServerResponse) { |
143 http_server::HttpServerPtr http_server(CreateHttpServer()); | 147 auto http_server = CreateHttpServer(); |
144 uint16_t assigned_port; | 148 uint16_t assigned_port = 0; |
145 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); | 149 EXPECT_TRUE(http_server->GetPort(&assigned_port)); |
146 http_server.WaitForIncomingResponse(); | 150 EXPECT_NE(assigned_port, 0u); |
147 | 151 |
148 HttpHandlerPtr http_handler_ptr; | 152 HttpHandlerPtr http_handler_ptr; |
149 GetHandler handler(GetProxy(&http_handler_ptr).Pass()); | 153 GetHandler handler(GetProxy(&http_handler_ptr).Pass()); |
150 | 154 |
151 // Set the test handler and wait for confirmation. | 155 // Set the test handler and wait for confirmation. |
152 http_server->SetHandler("/test", http_handler_ptr.Pass(), | 156 bool result = false; |
153 [](bool result) { EXPECT_TRUE(result); }); | 157 EXPECT_TRUE( |
154 http_server.WaitForIncomingResponse(); | 158 http_server->SetHandler("/test", http_handler_ptr.Pass(), &result)); |
| 159 EXPECT_TRUE(result); |
155 | 160 |
156 mojo::URLLoaderPtr url_loader; | 161 mojo::URLLoaderPtr url_loader; |
157 network_service_->CreateURLLoader(GetProxy(&url_loader)); | 162 network_service_->CreateURLLoader(GetProxy(&url_loader)); |
158 | 163 |
159 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); | 164 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); |
160 url_request->url = | 165 url_request->url = |
161 base::StringPrintf("http://127.0.0.1:%u/test", assigned_port); | 166 base::StringPrintf("http://127.0.0.1:%u/test", assigned_port); |
162 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); | 167 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); |
163 base::RunLoop run_loop; | 168 base::RunLoop run_loop; |
164 run_loop.Run(); | 169 run_loop.Run(); |
165 } | 170 } |
166 | 171 |
167 // Verifies that the server correctly passes the POST request payload using | 172 // Verifies that the server correctly passes the POST request payload using |
168 // example PostHandler. | 173 // example PostHandler. |
169 TEST_F(HttpServerApplicationTest, PostData) { | 174 TEST_F(HttpServerApplicationTest, PostData) { |
170 http_server::HttpServerPtr http_server(CreateHttpServer()); | 175 auto http_server = CreateHttpServer(); |
171 uint16_t assigned_port; | 176 uint16_t assigned_port = 0; |
172 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); | 177 EXPECT_TRUE(http_server->GetPort(&assigned_port)); |
173 http_server.WaitForIncomingResponse(); | 178 EXPECT_NE(assigned_port, 0u); |
174 | 179 |
175 HttpHandlerPtr http_handler_ptr; | 180 HttpHandlerPtr http_handler_ptr; |
176 PostHandler handler(GetProxy(&http_handler_ptr).Pass()); | 181 PostHandler handler(GetProxy(&http_handler_ptr).Pass()); |
177 | 182 |
178 // Set the test handler and wait for confirmation. | 183 // Set the test handler and wait for confirmation. |
179 http_server->SetHandler("/post", http_handler_ptr.Pass(), | 184 bool result = false; |
180 [](bool result) { EXPECT_TRUE(result); }); | 185 EXPECT_TRUE( |
181 http_server.WaitForIncomingResponse(); | 186 http_server->SetHandler("/post", http_handler_ptr.Pass(), &result)); |
| 187 EXPECT_TRUE(result); |
182 | 188 |
183 mojo::URLLoaderPtr url_loader; | 189 mojo::URLLoaderPtr url_loader; |
184 network_service_->CreateURLLoader(GetProxy(&url_loader)); | 190 network_service_->CreateURLLoader(GetProxy(&url_loader)); |
185 | 191 |
186 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); | 192 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); |
187 url_request->url = | 193 url_request->url = |
188 base::StringPrintf("http://127.0.0.1:%u/post", assigned_port); | 194 base::StringPrintf("http://127.0.0.1:%u/post", assigned_port); |
189 url_request->method = "POST"; | 195 url_request->method = "POST"; |
190 url_request->body.resize(1); | 196 url_request->body.resize(1); |
191 WriteMessageToDataPipe(kExampleMessage, &url_request->body[0]); | 197 WriteMessageToDataPipe(kExampleMessage, &url_request->body[0]); |
192 | 198 |
193 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); | 199 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); |
194 base::RunLoop run_loop; | 200 base::RunLoop run_loop; |
195 run_loop.Run(); | 201 run_loop.Run(); |
196 } | 202 } |
197 | 203 |
198 } // namespace http_server | 204 } // namespace http_server |
OLD | NEW |