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/common/data_pipe_utils.h" | 8 #include "mojo/common/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/system/macros.h" | 11 #include "mojo/public/cpp/system/macros.h" |
| 12 #include "mojo/services/network/public/interfaces/net_address.mojom.h" |
12 #include "mojo/services/network/public/interfaces/network_service.mojom.h" | 13 #include "mojo/services/network/public/interfaces/network_service.mojom.h" |
13 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" | 14 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" |
14 #include "services/http_server/public/http_server.mojom.h" | 15 #include "services/http_server/public/http_server.mojom.h" |
15 #include "services/http_server/public/http_server_factory.mojom.h" | 16 #include "services/http_server/public/http_server_factory.mojom.h" |
16 #include "services/http_server/public/http_server_util.h" | 17 #include "services/http_server/public/http_server_util.h" |
17 | 18 |
18 namespace http_server { | 19 namespace http_server { |
19 | 20 |
20 namespace { | 21 namespace { |
21 void WriteMessageToDataPipe( | 22 void WriteMessageToDataPipe( |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 std::string response_body; | 123 std::string response_body; |
123 mojo::common::BlockingCopyToString(response->body.Pass(), &response_body); | 124 mojo::common::BlockingCopyToString(response->body.Pass(), &response_body); |
124 EXPECT_EQ(kExampleMessage, response_body); | 125 EXPECT_EQ(kExampleMessage, response_body); |
125 base::MessageLoop::current()->Quit(); | 126 base::MessageLoop::current()->Quit(); |
126 } | 127 } |
127 | 128 |
128 // Verifies that the server responds to http GET requests using example | 129 // Verifies that the server responds to http GET requests using example |
129 // GetHandler. | 130 // GetHandler. |
130 TEST_F(HttpServerApplicationTest, ServerResponse) { | 131 TEST_F(HttpServerApplicationTest, ServerResponse) { |
131 http_server::HttpServerPtr http_server; | 132 http_server::HttpServerPtr http_server; |
132 http_server_factory_->CreateHttpServer(GetProxy(&http_server).Pass(), 0u); | 133 http_server_factory_->CreateHttpServer(GetProxy(&http_server).Pass(), |
| 134 nullptr); |
133 | 135 |
134 uint16_t assigned_port; | 136 uint16_t assigned_port; |
135 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); | 137 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); |
136 http_server.WaitForIncomingMethodCall(); | 138 http_server.WaitForIncomingMethodCall(); |
137 | 139 |
138 HttpHandlerPtr http_handler_ptr; | 140 HttpHandlerPtr http_handler_ptr; |
139 GetHandler handler(GetProxy(&http_handler_ptr).Pass()); | 141 GetHandler handler(GetProxy(&http_handler_ptr).Pass()); |
140 | 142 |
141 // Set the test handler and wait for confirmation. | 143 // Set the test handler and wait for confirmation. |
142 http_server->SetHandler("/test", http_handler_ptr.Pass(), | 144 http_server->SetHandler("/test", http_handler_ptr.Pass(), |
143 [](bool result) { EXPECT_TRUE(result); }); | 145 [](bool result) { EXPECT_TRUE(result); }); |
144 http_server.WaitForIncomingMethodCall(); | 146 http_server.WaitForIncomingMethodCall(); |
145 | 147 |
146 mojo::URLLoaderPtr url_loader; | 148 mojo::URLLoaderPtr url_loader; |
147 network_service_->CreateURLLoader(GetProxy(&url_loader)); | 149 network_service_->CreateURLLoader(GetProxy(&url_loader)); |
148 | 150 |
149 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); | 151 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); |
150 url_request->url = | 152 url_request->url = |
151 base::StringPrintf("http://0.0.0.0:%u/test", assigned_port); | 153 base::StringPrintf("http://0.0.0.0:%u/test", assigned_port); |
152 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); | 154 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); |
153 base::RunLoop run_loop; | 155 base::RunLoop run_loop; |
154 run_loop.Run(); | 156 run_loop.Run(); |
155 } | 157 } |
156 | 158 |
157 // Verifies that the server correctly passes the POST request payload using | 159 // Verifies that the server correctly passes the POST request payload using |
158 // example PostHandler. | 160 // example PostHandler. |
159 TEST_F(HttpServerApplicationTest, PostData) { | 161 TEST_F(HttpServerApplicationTest, PostData) { |
160 http_server::HttpServerPtr http_server; | 162 http_server::HttpServerPtr http_server; |
161 http_server_factory_->CreateHttpServer(GetProxy(&http_server).Pass(), 0u); | 163 http_server_factory_->CreateHttpServer(GetProxy(&http_server).Pass(), |
| 164 nullptr); |
162 | 165 |
163 uint16_t assigned_port; | 166 uint16_t assigned_port; |
164 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); | 167 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); |
165 http_server.WaitForIncomingMethodCall(); | 168 http_server.WaitForIncomingMethodCall(); |
166 | 169 |
167 HttpHandlerPtr http_handler_ptr; | 170 HttpHandlerPtr http_handler_ptr; |
168 PostHandler handler(GetProxy(&http_handler_ptr).Pass()); | 171 PostHandler handler(GetProxy(&http_handler_ptr).Pass()); |
169 | 172 |
170 // Set the test handler and wait for confirmation. | 173 // Set the test handler and wait for confirmation. |
171 http_server->SetHandler("/post", http_handler_ptr.Pass(), | 174 http_server->SetHandler("/post", http_handler_ptr.Pass(), |
172 [](bool result) { EXPECT_TRUE(result); }); | 175 [](bool result) { EXPECT_TRUE(result); }); |
173 http_server.WaitForIncomingMethodCall(); | 176 http_server.WaitForIncomingMethodCall(); |
174 | 177 |
175 mojo::URLLoaderPtr url_loader; | 178 mojo::URLLoaderPtr url_loader; |
176 network_service_->CreateURLLoader(GetProxy(&url_loader)); | 179 network_service_->CreateURLLoader(GetProxy(&url_loader)); |
177 | 180 |
178 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); | 181 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); |
179 url_request->url = | 182 url_request->url = |
180 base::StringPrintf("http://0.0.0.0:%u/post", assigned_port); | 183 base::StringPrintf("http://0.0.0.0:%u/post", assigned_port); |
181 url_request->method = "POST"; | 184 url_request->method = "POST"; |
182 url_request->body.resize(1); | 185 url_request->body.resize(1); |
183 WriteMessageToDataPipe(kExampleMessage, &url_request->body[0]); | 186 WriteMessageToDataPipe(kExampleMessage, &url_request->body[0]); |
184 | 187 |
185 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); | 188 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); |
186 base::RunLoop run_loop; | 189 base::RunLoop run_loop; |
187 run_loop.Run(); | 190 run_loop.Run(); |
188 } | 191 } |
189 | 192 |
190 } // namespace http_server | 193 } // namespace http_server |
OLD | NEW |