Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: services/http_server/http_server_apptest.cc

Issue 953513002: http_server: accept full NetAddress in CreateHttpServer. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // ApplicationTestBase: 104 // ApplicationTestBase:
104 void SetUp() override { 105 void SetUp() override {
105 ApplicationTestBase::SetUp(); 106 ApplicationTestBase::SetUp();
106 107
107 application_impl()->ConnectToService("mojo:http_server", 108 application_impl()->ConnectToService("mojo:http_server",
108 &http_server_factory_); 109 &http_server_factory_);
109 application_impl()->ConnectToService("mojo:network_service", 110 application_impl()->ConnectToService("mojo:network_service",
110 &network_service_); 111 &network_service_);
111 } 112 }
112 113
114 http_server::HttpServerPtr CreateHttpServer();
115
113 http_server::HttpServerFactoryPtr http_server_factory_; 116 http_server::HttpServerFactoryPtr http_server_factory_;
114 mojo::NetworkServicePtr network_service_; 117 mojo::NetworkServicePtr network_service_;
115 118
116 private: 119 private:
117 MOJO_DISALLOW_COPY_AND_ASSIGN(HttpServerApplicationTest); 120 MOJO_DISALLOW_COPY_AND_ASSIGN(HttpServerApplicationTest);
118 }; 121 };
119 122
123 http_server::HttpServerPtr HttpServerApplicationTest::CreateHttpServer() {
124 http_server::HttpServerPtr http_server;
125 mojo::NetAddressPtr local_address(mojo::NetAddress::New());
126 local_address->family = mojo::NET_ADDRESS_FAMILY_IPV4;
127 local_address->ipv4 = mojo::NetAddressIPv4::New();
128 local_address->ipv4->addr.resize(4);
129 local_address->ipv4->addr[0] = 0;
130 local_address->ipv4->addr[1] = 0;
131 local_address->ipv4->addr[2] = 0;
132 local_address->ipv4->addr[3] = 0;
133 local_address->ipv4->port = 0;
134 http_server_factory_->CreateHttpServer(GetProxy(&http_server).Pass(),
135 local_address.Pass());
136 return http_server.Pass();
137 }
138
120 void CheckServerResponse(mojo::URLResponsePtr response) { 139 void CheckServerResponse(mojo::URLResponsePtr response) {
121 EXPECT_EQ(200u, response->status_code); 140 EXPECT_EQ(200u, response->status_code);
122 std::string response_body; 141 std::string response_body;
123 mojo::common::BlockingCopyToString(response->body.Pass(), &response_body); 142 mojo::common::BlockingCopyToString(response->body.Pass(), &response_body);
124 EXPECT_EQ(kExampleMessage, response_body); 143 EXPECT_EQ(kExampleMessage, response_body);
125 base::MessageLoop::current()->Quit(); 144 base::MessageLoop::current()->Quit();
126 } 145 }
127 146
128 // Verifies that the server responds to http GET requests using example 147 // Verifies that the server responds to http GET requests using example
129 // GetHandler. 148 // GetHandler.
130 TEST_F(HttpServerApplicationTest, ServerResponse) { 149 TEST_F(HttpServerApplicationTest, ServerResponse) {
131 http_server::HttpServerPtr http_server; 150 http_server::HttpServerPtr http_server(CreateHttpServer());
132 http_server_factory_->CreateHttpServer(GetProxy(&http_server).Pass(), 0u);
133
134 uint16_t assigned_port; 151 uint16_t assigned_port;
135 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); 152 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; });
136 http_server.WaitForIncomingMethodCall(); 153 http_server.WaitForIncomingMethodCall();
137 154
138 HttpHandlerPtr http_handler_ptr; 155 HttpHandlerPtr http_handler_ptr;
139 GetHandler handler(GetProxy(&http_handler_ptr).Pass()); 156 GetHandler handler(GetProxy(&http_handler_ptr).Pass());
140 157
141 // Set the test handler and wait for confirmation. 158 // Set the test handler and wait for confirmation.
142 http_server->SetHandler("/test", http_handler_ptr.Pass(), 159 http_server->SetHandler("/test", http_handler_ptr.Pass(),
143 [](bool result) { EXPECT_TRUE(result); }); 160 [](bool result) { EXPECT_TRUE(result); });
144 http_server.WaitForIncomingMethodCall(); 161 http_server.WaitForIncomingMethodCall();
145 162
146 mojo::URLLoaderPtr url_loader; 163 mojo::URLLoaderPtr url_loader;
147 network_service_->CreateURLLoader(GetProxy(&url_loader)); 164 network_service_->CreateURLLoader(GetProxy(&url_loader));
148 165
149 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); 166 mojo::URLRequestPtr url_request = mojo::URLRequest::New();
150 url_request->url = 167 url_request->url =
151 base::StringPrintf("http://0.0.0.0:%u/test", assigned_port); 168 base::StringPrintf("http://0.0.0.0:%u/test", assigned_port);
152 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); 169 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse));
153 base::RunLoop run_loop; 170 base::RunLoop run_loop;
154 run_loop.Run(); 171 run_loop.Run();
155 } 172 }
156 173
157 // Verifies that the server correctly passes the POST request payload using 174 // Verifies that the server correctly passes the POST request payload using
158 // example PostHandler. 175 // example PostHandler.
159 TEST_F(HttpServerApplicationTest, PostData) { 176 TEST_F(HttpServerApplicationTest, PostData) {
160 http_server::HttpServerPtr http_server; 177 http_server::HttpServerPtr http_server = CreateHttpServer();
161 http_server_factory_->CreateHttpServer(GetProxy(&http_server).Pass(), 0u);
162
163 uint16_t assigned_port; 178 uint16_t assigned_port;
164 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); 179 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; });
165 http_server.WaitForIncomingMethodCall(); 180 http_server.WaitForIncomingMethodCall();
166 181
167 HttpHandlerPtr http_handler_ptr; 182 HttpHandlerPtr http_handler_ptr;
168 PostHandler handler(GetProxy(&http_handler_ptr).Pass()); 183 PostHandler handler(GetProxy(&http_handler_ptr).Pass());
169 184
170 // Set the test handler and wait for confirmation. 185 // Set the test handler and wait for confirmation.
171 http_server->SetHandler("/post", http_handler_ptr.Pass(), 186 http_server->SetHandler("/post", http_handler_ptr.Pass(),
172 [](bool result) { EXPECT_TRUE(result); }); 187 [](bool result) { EXPECT_TRUE(result); });
173 http_server.WaitForIncomingMethodCall(); 188 http_server.WaitForIncomingMethodCall();
174 189
175 mojo::URLLoaderPtr url_loader; 190 mojo::URLLoaderPtr url_loader;
176 network_service_->CreateURLLoader(GetProxy(&url_loader)); 191 network_service_->CreateURLLoader(GetProxy(&url_loader));
177 192
178 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); 193 mojo::URLRequestPtr url_request = mojo::URLRequest::New();
179 url_request->url = 194 url_request->url =
180 base::StringPrintf("http://0.0.0.0:%u/post", assigned_port); 195 base::StringPrintf("http://0.0.0.0:%u/post", assigned_port);
181 url_request->method = "POST"; 196 url_request->method = "POST";
182 url_request->body.resize(1); 197 url_request->body.resize(1);
183 WriteMessageToDataPipe(kExampleMessage, &url_request->body[0]); 198 WriteMessageToDataPipe(kExampleMessage, &url_request->body[0]);
184 199
185 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); 200 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse));
186 base::RunLoop run_loop; 201 base::RunLoop run_loop;
187 run_loop.Run(); 202 run_loop.Run();
188 } 203 }
189 204
190 } // namespace http_server 205 } // namespace http_server
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698