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/base_paths.h" | 5 #include "base/base_paths.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "mojo/common/data_pipe_utils.h" | 13 #include "mojo/common/data_pipe_utils.h" |
14 #include "mojo/public/cpp/application/application_impl.h" | 14 #include "mojo/public/cpp/application/application_impl.h" |
15 #include "mojo/public/cpp/application/application_test_base.h" | 15 #include "mojo/public/cpp/application/application_test_base.h" |
16 #include "mojo/public/cpp/system/macros.h" | 16 #include "mojo/public/cpp/system/macros.h" |
17 #include "mojo/services/http_server/public/cpp/http_server_util.h" | 17 #include "mojo/services/http_server/public/cpp/http_server_util.h" |
18 #include "mojo/services/http_server/public/interfaces/http_server.mojom.h" | 18 #include "mojo/services/http_server/public/interfaces/http_server.mojom.h" |
19 #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" |
20 #include "mojo/services/network/public/interfaces/net_address.mojom.h" | 20 #include "mojo/services/network/public/interfaces/net_address.mojom.h" |
21 #include "shell/kPingable.h" | 21 #include "shell/kPingable.h" |
22 #include "shell/test/pingable.mojom.h" | 22 #include "shell/test/pingable.mojom.h" |
23 | 23 |
24 namespace mojo { | |
25 namespace { | 24 namespace { |
26 | 25 |
27 std::string GetURL(uint16_t port, const std::string& path) { | 26 std::string GetURL(uint16_t port, const std::string& path) { |
28 return base::StringPrintf("http://127.0.0.1:%u/%s", | 27 return base::StringPrintf("http://127.0.0.1:%u/%s", |
29 static_cast<unsigned>(port), path.c_str()); | 28 static_cast<unsigned>(port), path.c_str()); |
30 } | 29 } |
31 | 30 |
32 class GetHandler : public http_server::HttpHandler { | 31 class GetHandler : public http_server::HttpHandler { |
33 public: | 32 public: |
34 GetHandler(InterfaceRequest<http_server::HttpHandler> request, uint16_t port) | 33 GetHandler(mojo::InterfaceRequest<http_server::HttpHandler> request, |
35 : binding_(this, request.Pass()), port_(port) { | 34 uint16_t port) |
36 } | 35 : binding_(this, request.Pass()), port_(port) {} |
37 ~GetHandler() override {} | 36 ~GetHandler() override {} |
38 | 37 |
39 private: | 38 private: |
40 // http_server::HttpHandler: | 39 // http_server::HttpHandler: |
41 void HandleRequest( | 40 void HandleRequest(http_server::HttpRequestPtr request, |
42 http_server::HttpRequestPtr request, | 41 const mojo::Callback<void(http_server::HttpResponsePtr)>& |
43 const Callback<void(http_server::HttpResponsePtr)>& callback) override { | 42 callback) override { |
44 http_server::HttpResponsePtr response; | 43 http_server::HttpResponsePtr response; |
45 if (StartsWithASCII(request->relative_url, "/app", true)) { | 44 if (StartsWithASCII(request->relative_url, "/app", true)) { |
46 response = http_server::CreateHttpResponse( | 45 response = http_server::CreateHttpResponse( |
47 200, std::string(kPingable.data, kPingable.size)); | 46 200, std::string(shell::test::kPingable.data, |
| 47 shell::test::kPingable.size)); |
48 response->content_type = "application/octet-stream"; | 48 response->content_type = "application/octet-stream"; |
49 } else if (request->relative_url == "/redirect") { | 49 } else if (request->relative_url == "/redirect") { |
50 response = http_server::HttpResponse::New(); | 50 response = http_server::HttpResponse::New(); |
51 response->status_code = 302; | 51 response->status_code = 302; |
52 response->custom_headers.insert("Location", GetURL(port_, "app")); | 52 response->custom_headers.insert("Location", GetURL(port_, "app")); |
53 } else { | 53 } else { |
54 NOTREACHED(); | 54 NOTREACHED(); |
55 } | 55 } |
56 | 56 |
57 callback.Run(response.Pass()); | 57 callback.Run(response.Pass()); |
58 } | 58 } |
59 | 59 |
60 Binding<http_server::HttpHandler> binding_; | 60 mojo::Binding<http_server::HttpHandler> binding_; |
61 uint16_t port_; | 61 uint16_t port_; |
62 | 62 |
63 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); | 63 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); |
64 }; | 64 }; |
65 | 65 |
66 typedef test::ApplicationTestBase ShellAppTest; | 66 typedef mojo::test::ApplicationTestBase ShellAppTest; |
67 | 67 |
68 class ShellHTTPAppTest : public test::ApplicationTestBase { | 68 class ShellHTTPAppTest : public ShellAppTest { |
69 public: | 69 public: |
70 ShellHTTPAppTest() : ApplicationTestBase() {} | 70 ShellHTTPAppTest() {} |
71 ~ShellHTTPAppTest() override {} | 71 ~ShellHTTPAppTest() override {} |
72 | 72 |
73 protected: | 73 protected: |
74 // ApplicationTestBase: | |
75 void SetUp() override { | 74 void SetUp() override { |
76 ApplicationTestBase::SetUp(); | 75 ShellAppTest::SetUp(); |
77 | 76 |
78 application_impl()->ConnectToService("mojo:http_server", | 77 application_impl()->ConnectToService("mojo:http_server", |
79 &http_server_factory_); | 78 &http_server_factory_); |
80 | 79 |
81 NetAddressPtr local_address(NetAddress::New()); | 80 mojo::NetAddressPtr local_address(mojo::NetAddress::New()); |
82 local_address->family = NET_ADDRESS_FAMILY_IPV4; | 81 local_address->family = mojo::NET_ADDRESS_FAMILY_IPV4; |
83 local_address->ipv4 = NetAddressIPv4::New(); | 82 local_address->ipv4 = mojo::NetAddressIPv4::New(); |
84 local_address->ipv4->addr.resize(4); | 83 local_address->ipv4->addr.resize(4); |
85 local_address->ipv4->addr[0] = 127; | 84 local_address->ipv4->addr[0] = 127; |
86 local_address->ipv4->addr[1] = 0; | 85 local_address->ipv4->addr[1] = 0; |
87 local_address->ipv4->addr[2] = 0; | 86 local_address->ipv4->addr[2] = 0; |
88 local_address->ipv4->addr[3] = 1; | 87 local_address->ipv4->addr[3] = 1; |
89 local_address->ipv4->port = 0; | 88 local_address->ipv4->port = 0; |
90 http_server_factory_->CreateHttpServer(GetProxy(&http_server_), | 89 http_server_factory_->CreateHttpServer(mojo::GetProxy(&http_server_), |
91 local_address.Pass()); | 90 local_address.Pass()); |
92 | 91 |
93 http_server_->GetPort([this](uint16_t p) { port_ = p; }); | 92 http_server_->GetPort([this](uint16_t p) { port_ = p; }); |
94 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall()); | 93 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall()); |
95 | 94 |
96 InterfacePtr<http_server::HttpHandler> http_handler; | 95 http_server::HttpHandlerPtr http_handler; |
97 handler_.reset(new GetHandler(GetProxy(&http_handler).Pass(), port_)); | 96 handler_.reset(new GetHandler(mojo::GetProxy(&http_handler).Pass(), port_)); |
98 http_server_->SetHandler(".*", http_handler.Pass(), | 97 http_server_->SetHandler(".*", http_handler.Pass(), |
99 [](bool result) { EXPECT_TRUE(result); }); | 98 [](bool result) { EXPECT_TRUE(result); }); |
100 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall()); | 99 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall()); |
101 } | 100 } |
102 | 101 |
103 std::string GetURL(const std::string& path) { | 102 std::string GetURL(const std::string& path) { return ::GetURL(port_, path); } |
104 return ::mojo::GetURL(port_, path); | |
105 } | |
106 | 103 |
107 http_server::HttpServerFactoryPtr http_server_factory_; | 104 http_server::HttpServerFactoryPtr http_server_factory_; |
108 http_server::HttpServerPtr http_server_; | 105 http_server::HttpServerPtr http_server_; |
109 scoped_ptr<GetHandler> handler_; | 106 scoped_ptr<GetHandler> handler_; |
110 uint16_t port_; | 107 uint16_t port_; |
111 | 108 |
112 private: | 109 private: |
113 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellHTTPAppTest); | 110 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellHTTPAppTest); |
114 }; | 111 }; |
115 | 112 |
116 // Test that we can load apps over http. | 113 // Test that we can load apps over http. |
117 TEST_F(ShellHTTPAppTest, Http) { | 114 TEST_F(ShellHTTPAppTest, Http) { |
118 InterfacePtr<Pingable> pingable; | 115 PingablePtr pingable; |
119 application_impl()->ConnectToService(GetURL("app"), &pingable); | 116 application_impl()->ConnectToService(GetURL("app"), &pingable); |
120 pingable->Ping("hello", | 117 pingable->Ping("hello", [this](const mojo::String& app_url, |
121 [this](const String& app_url, const String& connection_url, | 118 const mojo::String& connection_url, |
122 const String& message) { | 119 const mojo::String& message) { |
123 EXPECT_EQ(GetURL("app"), app_url); | 120 EXPECT_EQ(GetURL("app"), app_url); |
124 EXPECT_EQ(GetURL("app"), connection_url); | 121 EXPECT_EQ(GetURL("app"), connection_url); |
125 EXPECT_EQ("hello", message); | 122 EXPECT_EQ("hello", message); |
126 base::MessageLoop::current()->Quit(); | 123 base::MessageLoop::current()->Quit(); |
127 }); | 124 }); |
128 base::RunLoop().Run(); | 125 base::RunLoop().Run(); |
129 } | 126 } |
130 | 127 |
131 // Test that redirects work. | 128 // Test that redirects work. |
132 // TODO(aa): Test that apps receive the correct URL parameters. | 129 // TODO(aa): Test that apps receive the correct URL parameters. |
133 TEST_F(ShellHTTPAppTest, Redirect) { | 130 TEST_F(ShellHTTPAppTest, Redirect) { |
134 InterfacePtr<Pingable> pingable; | 131 PingablePtr pingable; |
135 application_impl()->ConnectToService(GetURL("redirect"), &pingable); | 132 application_impl()->ConnectToService(GetURL("redirect"), &pingable); |
136 pingable->Ping("hello", | 133 pingable->Ping("hello", [this](const mojo::String& app_url, |
137 [this](const String& app_url, const String& connection_url, | 134 const mojo::String& connection_url, |
138 const String& message) { | 135 const mojo::String& message) { |
139 EXPECT_EQ(GetURL("app"), app_url); | 136 EXPECT_EQ(GetURL("app"), app_url); |
140 EXPECT_EQ(GetURL("app"), connection_url); | 137 EXPECT_EQ(GetURL("app"), connection_url); |
141 EXPECT_EQ("hello", message); | 138 EXPECT_EQ("hello", message); |
142 base::MessageLoop::current()->Quit(); | 139 base::MessageLoop::current()->Quit(); |
143 }); | 140 }); |
144 base::RunLoop().Run(); | 141 base::RunLoop().Run(); |
145 } | 142 } |
146 | 143 |
147 // Test that querystring is not considered when resolving http applications. | 144 // Test that querystring is not considered when resolving http applications. |
148 // TODO(aa|qsr): Fix this test on Linux ASAN http://crbug.com/463662 | 145 // TODO(aa|qsr): Fix this test on Linux ASAN http://crbug.com/463662 |
149 #if defined(ADDRESS_SANITIZER) | 146 #if defined(ADDRESS_SANITIZER) |
150 #define MAYBE_QueryHandling DISABLED_QueryHandling | 147 #define MAYBE_QueryHandling DISABLED_QueryHandling |
151 #else | 148 #else |
152 #define MAYBE_QueryHandling QueryHandling | 149 #define MAYBE_QueryHandling QueryHandling |
153 #endif // ADDRESS_SANITIZER | 150 #endif // ADDRESS_SANITIZER |
154 TEST_F(ShellHTTPAppTest, MAYBE_QueryHandling) { | 151 TEST_F(ShellHTTPAppTest, MAYBE_QueryHandling) { |
155 InterfacePtr<Pingable> pingable1; | 152 PingablePtr pingable1; |
156 InterfacePtr<Pingable> pingable2; | 153 PingablePtr pingable2; |
157 application_impl()->ConnectToService(GetURL("app?foo"), &pingable1); | 154 application_impl()->ConnectToService(GetURL("app?foo"), &pingable1); |
158 application_impl()->ConnectToService(GetURL("app?bar"), &pingable2); | 155 application_impl()->ConnectToService(GetURL("app?bar"), &pingable2); |
159 | 156 |
160 int num_responses = 0; | 157 int num_responses = 0; |
161 auto callback = [this, &num_responses](const String& app_url, | 158 auto callback = [this, &num_responses](const mojo::String& app_url, |
162 const String& connection_url, | 159 const mojo::String& connection_url, |
163 const String& message) { | 160 const mojo::String& message) { |
164 EXPECT_EQ(GetURL("app"), app_url); | 161 EXPECT_EQ(GetURL("app"), app_url); |
165 EXPECT_EQ("hello", message); | 162 EXPECT_EQ("hello", message); |
166 ++num_responses; | 163 ++num_responses; |
167 if (num_responses == 1) { | 164 if (num_responses == 1) { |
168 EXPECT_EQ(GetURL("app?foo"), connection_url); | 165 EXPECT_EQ(GetURL("app?foo"), connection_url); |
169 } else if (num_responses == 2) { | 166 } else if (num_responses == 2) { |
170 EXPECT_EQ(GetURL("app?bar"), connection_url); | 167 EXPECT_EQ(GetURL("app?bar"), connection_url); |
171 base::MessageLoop::current()->Quit(); | 168 base::MessageLoop::current()->Quit(); |
172 } else { | 169 } else { |
173 CHECK(false); | 170 CHECK(false); |
174 } | 171 } |
175 }; | 172 }; |
176 pingable1->Ping("hello", callback); | 173 pingable1->Ping("hello", callback); |
177 pingable2->Ping("hello", callback); | 174 pingable2->Ping("hello", callback); |
178 base::RunLoop().Run(); | 175 base::RunLoop().Run(); |
179 } | 176 } |
180 | 177 |
181 // mojo: URLs can have querystrings too | 178 // mojo: URLs can have querystrings too |
182 TEST_F(ShellAppTest, MojoURLQueryHandling) { | 179 TEST_F(ShellAppTest, MojoURLQueryHandling) { |
183 InterfacePtr<Pingable> pingable; | 180 PingablePtr pingable; |
184 application_impl()->ConnectToService("mojo:pingable_app?foo", &pingable); | 181 application_impl()->ConnectToService("mojo:pingable_app?foo", &pingable); |
185 auto callback = [this](const String& app_url, const String& connection_url, | 182 auto callback = |
186 const String& message) { | 183 [this](const mojo::String& app_url, const mojo::String& connection_url, |
187 EXPECT_TRUE(EndsWith(app_url, "/pingable_app.mojo", true)); | 184 const mojo::String& message) { |
188 EXPECT_EQ(app_url.To<std::string>() + "?foo", connection_url); | 185 EXPECT_TRUE(EndsWith(app_url, "/pingable_app.mojo", true)); |
189 EXPECT_EQ("hello", message); | 186 EXPECT_EQ(app_url.To<std::string>() + "?foo", connection_url); |
190 base::MessageLoop::current()->Quit(); | 187 EXPECT_EQ("hello", message); |
191 }; | 188 base::MessageLoop::current()->Quit(); |
| 189 }; |
192 pingable->Ping("hello", callback); | 190 pingable->Ping("hello", callback); |
193 base::RunLoop().Run(); | 191 base::RunLoop().Run(); |
194 } | 192 } |
195 | 193 |
196 } // namespace | 194 } // namespace |
197 } // namespace mojo | |
OLD | NEW |