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 <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "mojo/common/data_pipe_utils.h" | 15 #include "mojo/common/data_pipe_utils.h" |
16 #include "mojo/public/cpp/system/macros.h" | 16 #include "mojo/public/cpp/system/macros.h" |
17 #include "mojo/runner/kPingable.h" | |
18 #include "mojo/runner/test/pingable.mojom.h" | |
19 #include "mojo/services/http_server/public/cpp/http_server_util.h" | 17 #include "mojo/services/http_server/public/cpp/http_server_util.h" |
20 #include "mojo/services/http_server/public/interfaces/http_server.mojom.h" | 18 #include "mojo/services/http_server/public/interfaces/http_server.mojom.h" |
21 #include "mojo/services/http_server/public/interfaces/http_server_factory.mojom.
h" | 19 #include "mojo/services/http_server/public/interfaces/http_server_factory.mojom.
h" |
22 #include "mojo/services/network/public/interfaces/net_address.mojom.h" | 20 #include "mojo/services/network/public/interfaces/net_address.mojom.h" |
23 #include "mojo/shell/public/cpp/application_impl.h" | 21 #include "mojo/shell/public/cpp/application_impl.h" |
24 #include "mojo/shell/public/cpp/application_test_base.h" | 22 #include "mojo/shell/public/cpp/application_test_base.h" |
| 23 #include "mojo/shell/standalone/kPingable.h" |
| 24 #include "mojo/shell/standalone/test/pingable.mojom.h" |
25 | 25 |
26 namespace mojo { | 26 namespace mojo { |
27 namespace { | 27 namespace { |
28 | 28 |
29 std::string GetURL(uint16_t port, const std::string& path) { | 29 std::string GetURL(uint16_t port, const std::string& path) { |
30 return base::StringPrintf("http://127.0.0.1:%u/%s", | 30 return base::StringPrintf("http://127.0.0.1:%u/%s", |
31 static_cast<unsigned>(port), path.c_str()); | 31 static_cast<unsigned>(port), path.c_str()); |
32 } | 32 } |
33 | 33 |
34 class GetHandler : public http_server::HttpHandler { | 34 class GetHandler : public http_server::HttpHandler { |
35 public: | 35 public: |
36 GetHandler(InterfaceRequest<http_server::HttpHandler> request, uint16_t port) | 36 GetHandler(InterfaceRequest<http_server::HttpHandler> request, uint16_t port) |
37 : binding_(this, request.Pass()), port_(port) { | 37 : binding_(this, std::move(request)), port_(port) {} |
38 } | |
39 ~GetHandler() override {} | 38 ~GetHandler() override {} |
40 | 39 |
41 private: | 40 private: |
42 // http_server::HttpHandler: | 41 // http_server::HttpHandler: |
43 void HandleRequest( | 42 void HandleRequest( |
44 http_server::HttpRequestPtr request, | 43 http_server::HttpRequestPtr request, |
45 const Callback<void(http_server::HttpResponsePtr)>& callback) override { | 44 const Callback<void(http_server::HttpResponsePtr)>& callback) override { |
46 http_server::HttpResponsePtr response; | 45 http_server::HttpResponsePtr response; |
47 if (base::StartsWith(request->relative_url, "/app", | 46 if (base::StartsWith(request->relative_url, "/app", |
48 base::CompareCase::SENSITIVE)) { | 47 base::CompareCase::SENSITIVE)) { |
49 response = http_server::CreateHttpResponse( | 48 response = http_server::CreateHttpResponse( |
50 200, std::string(kPingable.data, kPingable.size)); | 49 200, std::string(kPingable.data, kPingable.size)); |
51 response->content_type = "application/octet-stream"; | 50 response->content_type = "application/octet-stream"; |
52 } else if (request->relative_url == "/redirect") { | 51 } else if (request->relative_url == "/redirect") { |
53 response = http_server::HttpResponse::New(); | 52 response = http_server::HttpResponse::New(); |
54 response->status_code = 302; | 53 response->status_code = 302; |
55 response->custom_headers.insert("Location", GetURL(port_, "app")); | 54 response->custom_headers.insert("Location", GetURL(port_, "app")); |
56 } else { | 55 } else { |
57 NOTREACHED(); | 56 NOTREACHED(); |
58 } | 57 } |
59 | 58 |
60 callback.Run(response.Pass()); | 59 callback.Run(std::move(response)); |
61 } | 60 } |
62 | 61 |
63 Binding<http_server::HttpHandler> binding_; | 62 Binding<http_server::HttpHandler> binding_; |
64 uint16_t port_; | 63 uint16_t port_; |
65 | 64 |
66 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); | 65 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); |
67 }; | 66 }; |
68 | 67 |
69 typedef test::ApplicationTestBase ShellAppTest; | 68 typedef test::ApplicationTestBase ShellAppTest; |
70 | 69 |
(...skipping 13 matching lines...) Expand all Loading... |
84 NetAddressPtr local_address(NetAddress::New()); | 83 NetAddressPtr local_address(NetAddress::New()); |
85 local_address->family = NET_ADDRESS_FAMILY_IPV4; | 84 local_address->family = NET_ADDRESS_FAMILY_IPV4; |
86 local_address->ipv4 = NetAddressIPv4::New(); | 85 local_address->ipv4 = NetAddressIPv4::New(); |
87 local_address->ipv4->addr.resize(4); | 86 local_address->ipv4->addr.resize(4); |
88 local_address->ipv4->addr[0] = 127; | 87 local_address->ipv4->addr[0] = 127; |
89 local_address->ipv4->addr[1] = 0; | 88 local_address->ipv4->addr[1] = 0; |
90 local_address->ipv4->addr[2] = 0; | 89 local_address->ipv4->addr[2] = 0; |
91 local_address->ipv4->addr[3] = 1; | 90 local_address->ipv4->addr[3] = 1; |
92 local_address->ipv4->port = 0; | 91 local_address->ipv4->port = 0; |
93 http_server_factory_->CreateHttpServer(GetProxy(&http_server_), | 92 http_server_factory_->CreateHttpServer(GetProxy(&http_server_), |
94 local_address.Pass()); | 93 std::move(local_address)); |
95 | 94 |
96 http_server_->GetPort([this](uint16_t p) { port_ = p; }); | 95 http_server_->GetPort([this](uint16_t p) { port_ = p; }); |
97 EXPECT_TRUE(http_server_.WaitForIncomingResponse()); | 96 EXPECT_TRUE(http_server_.WaitForIncomingResponse()); |
98 | 97 |
99 InterfacePtr<http_server::HttpHandler> http_handler; | 98 InterfacePtr<http_server::HttpHandler> http_handler; |
100 handler_.reset(new GetHandler(GetProxy(&http_handler).Pass(), port_)); | 99 handler_.reset(new GetHandler(GetProxy(&http_handler).Pass(), port_)); |
101 http_server_->SetHandler(".*", http_handler.Pass(), | 100 http_server_->SetHandler(".*", std::move(http_handler), |
102 [](bool result) { EXPECT_TRUE(result); }); | 101 [](bool result) { EXPECT_TRUE(result); }); |
103 EXPECT_TRUE(http_server_.WaitForIncomingResponse()); | 102 EXPECT_TRUE(http_server_.WaitForIncomingResponse()); |
104 } | 103 } |
105 | 104 |
106 std::string GetURL(const std::string& path) { | 105 std::string GetURL(const std::string& path) { |
107 return ::mojo::GetURL(port_, path); | 106 return ::mojo::GetURL(port_, path); |
108 } | 107 } |
109 | 108 |
110 http_server::HttpServerFactoryPtr http_server_factory_; | 109 http_server::HttpServerFactoryPtr http_server_factory_; |
111 http_server::HttpServerPtr http_server_; | 110 http_server::HttpServerPtr http_server_; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 EXPECT_EQ(app_url.To<std::string>() + "?foo", connection_url); | 191 EXPECT_EQ(app_url.To<std::string>() + "?foo", connection_url); |
193 EXPECT_EQ("hello", message); | 192 EXPECT_EQ("hello", message); |
194 base::MessageLoop::current()->QuitWhenIdle(); | 193 base::MessageLoop::current()->QuitWhenIdle(); |
195 }; | 194 }; |
196 pingable->Ping("hello", callback); | 195 pingable->Ping("hello", callback); |
197 base::RunLoop().Run(); | 196 base::RunLoop().Run(); |
198 } | 197 } |
199 | 198 |
200 } // namespace | 199 } // namespace |
201 } // namespace mojo | 200 } // namespace mojo |
OLD | NEW |