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/network/public/interfaces/net_address.mojom.h" | 17 #include "mojo/services/network/public/interfaces/net_address.mojom.h" |
18 #include "services/http_server/public/http_server.mojom.h" | 18 #include "services/http_server/public/http_server.mojom.h" |
19 #include "services/http_server/public/http_server_factory.mojom.h" | 19 #include "services/http_server/public/http_server_factory.mojom.h" |
20 #include "services/http_server/public/http_server_util.h" | 20 #include "services/http_server/public/http_server_util.h" |
| 21 #include "shell/kPingable.h" |
21 #include "shell/test/pingable.mojom.h" | 22 #include "shell/test/pingable.mojom.h" |
22 | |
23 namespace mojo { | 23 namespace mojo { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 std::string GetURL(uint16_t port, const std::string& path) { | 27 std::string GetURL(uint16_t port, const std::string& path) { |
28 return base::StringPrintf("http://127.0.0.1:%u/%s", port, path.c_str()); | 28 return base::StringPrintf("http://127.0.0.1:%u/%s", port, path.c_str()); |
29 } | 29 } |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 class GetHandler : public http_server::HttpHandler { | 33 class GetHandler : public http_server::HttpHandler { |
34 public: | 34 public: |
35 GetHandler(InterfaceRequest<http_server::HttpHandler> request, uint16_t port) | 35 GetHandler(InterfaceRequest<http_server::HttpHandler> request, uint16_t port) |
36 : binding_(this, request.Pass()), port_(port) { | 36 : binding_(this, request.Pass()), port_(port) { |
37 CHECK(PathService::Get(base::FILE_MODULE, &app_path_)); | |
38 app_path_ = app_path_.DirName().Append("pingable_app.mojo"); | |
39 CHECK(base::PathExists(app_path_)); | |
40 } | 37 } |
41 ~GetHandler() override {} | 38 ~GetHandler() override {} |
42 | 39 |
43 private: | 40 private: |
44 // http_server::HttpHandler: | 41 // http_server::HttpHandler: |
45 void HandleRequest( | 42 void HandleRequest( |
46 http_server::HttpRequestPtr request, | 43 http_server::HttpRequestPtr request, |
47 const Callback<void(http_server::HttpResponsePtr)>& callback) override { | 44 const Callback<void(http_server::HttpResponsePtr)>& callback) override { |
48 http_server::HttpResponsePtr response; | 45 http_server::HttpResponsePtr response; |
49 if (StartsWithASCII(request->relative_url, "/app", true)) { | 46 if (StartsWithASCII(request->relative_url, "/app", true)) { |
50 // Super inefficient, but meh. | 47 response = http_server::CreateHttpResponse( |
51 std::string data; | 48 200, std::string(kPingable.data, kPingable.size)); |
52 base::ReadFileToString(app_path_, &data); | |
53 response = http_server::CreateHttpResponse(200, data); | |
54 response->content_type = "application/octet-stream"; | 49 response->content_type = "application/octet-stream"; |
55 } else if (request->relative_url == "/redirect") { | 50 } else if (request->relative_url == "/redirect") { |
56 response = http_server::HttpResponse::New(); | 51 response = http_server::HttpResponse::New(); |
57 response->status_code = 302; | 52 response->status_code = 302; |
58 response->custom_headers.insert("Location", GetURL(port_, "app")); | 53 response->custom_headers.insert("Location", GetURL(port_, "app")); |
59 } else { | 54 } else { |
60 NOTREACHED(); | 55 NOTREACHED(); |
61 } | 56 } |
62 | 57 |
63 callback.Run(response.Pass()); | 58 callback.Run(response.Pass()); |
64 } | 59 } |
65 | 60 |
66 Binding<http_server::HttpHandler> binding_; | 61 Binding<http_server::HttpHandler> binding_; |
67 base::FilePath app_path_; | |
68 uint16_t port_; | 62 uint16_t port_; |
69 | 63 |
70 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); | 64 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); |
71 }; | 65 }; |
72 | 66 |
73 typedef test::ApplicationTestBase ShellAppTest; | 67 typedef test::ApplicationTestBase ShellAppTest; |
74 | 68 |
75 class ShellHTTPAppTest : public test::ApplicationTestBase { | 69 class ShellHTTPAppTest : public test::ApplicationTestBase { |
76 public: | 70 public: |
77 ShellHTTPAppTest() : ApplicationTestBase() {} | 71 ShellHTTPAppTest() : ApplicationTestBase() {} |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 107 |
114 http_server::HttpServerFactoryPtr http_server_factory_; | 108 http_server::HttpServerFactoryPtr http_server_factory_; |
115 http_server::HttpServerPtr http_server_; | 109 http_server::HttpServerPtr http_server_; |
116 scoped_ptr<GetHandler> handler_; | 110 scoped_ptr<GetHandler> handler_; |
117 uint16_t port_; | 111 uint16_t port_; |
118 | 112 |
119 private: | 113 private: |
120 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellHTTPAppTest); | 114 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellHTTPAppTest); |
121 }; | 115 }; |
122 | 116 |
123 #if defined(OS_ANDROID) | |
124 // These tests rely on data that needs to be bundled into the apptest binary in | |
125 // order to work on Android. | |
126 #else // !OS_ANDROID | |
127 // Test that we can load apps over http. | 117 // Test that we can load apps over http. |
128 TEST_F(ShellHTTPAppTest, Http) { | 118 TEST_F(ShellHTTPAppTest, Http) { |
129 InterfacePtr<Pingable> pingable; | 119 InterfacePtr<Pingable> pingable; |
130 application_impl()->ConnectToService(GetURL("app"), &pingable); | 120 application_impl()->ConnectToService(GetURL("app"), &pingable); |
131 pingable->Ping("hello", | 121 pingable->Ping("hello", |
132 [this](const String& app_url, const String& connection_url, | 122 [this](const String& app_url, const String& connection_url, |
133 const String& message) { | 123 const String& message) { |
134 EXPECT_EQ(GetURL("app"), app_url); | 124 EXPECT_EQ(GetURL("app"), app_url); |
135 EXPECT_EQ(GetURL("app"), connection_url); | 125 EXPECT_EQ(GetURL("app"), connection_url); |
136 EXPECT_EQ("hello", message); | 126 EXPECT_EQ("hello", message); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 EXPECT_EQ(GetURL("app?bar"), connection_url); | 171 EXPECT_EQ(GetURL("app?bar"), connection_url); |
182 base::MessageLoop::current()->Quit(); | 172 base::MessageLoop::current()->Quit(); |
183 } else { | 173 } else { |
184 CHECK(false); | 174 CHECK(false); |
185 } | 175 } |
186 }; | 176 }; |
187 pingable1->Ping("hello", callback); | 177 pingable1->Ping("hello", callback); |
188 pingable2->Ping("hello", callback); | 178 pingable2->Ping("hello", callback); |
189 base::RunLoop().Run(); | 179 base::RunLoop().Run(); |
190 } | 180 } |
191 #endif // OS_ANDROID | |
192 | 181 |
193 // mojo: URLs can have querystrings too | 182 // mojo: URLs can have querystrings too |
194 TEST_F(ShellAppTest, MojoURLQueryHandling) { | 183 TEST_F(ShellAppTest, MojoURLQueryHandling) { |
195 InterfacePtr<Pingable> pingable; | 184 InterfacePtr<Pingable> pingable; |
196 application_impl()->ConnectToService("mojo:pingable_app?foo", &pingable); | 185 application_impl()->ConnectToService("mojo:pingable_app?foo", &pingable); |
197 auto callback = [this](const String& app_url, const String& connection_url, | 186 auto callback = [this](const String& app_url, const String& connection_url, |
198 const String& message) { | 187 const String& message) { |
199 EXPECT_TRUE(EndsWith(app_url, "/pingable_app.mojo", true)); | 188 EXPECT_TRUE(EndsWith(app_url, "/pingable_app.mojo", true)); |
200 EXPECT_EQ(app_url.To<std::string>() + "?foo", connection_url); | 189 EXPECT_EQ(app_url.To<std::string>() + "?foo", connection_url); |
201 EXPECT_EQ("hello", message); | 190 EXPECT_EQ("hello", message); |
202 base::MessageLoop::current()->Quit(); | 191 base::MessageLoop::current()->Quit(); |
203 }; | 192 }; |
204 pingable->Ping("hello", callback); | 193 pingable->Ping("hello", callback); |
205 base::RunLoop().Run(); | 194 base::RunLoop().Run(); |
206 } | 195 } |
207 | 196 |
208 } // namespace mojo | 197 } // namespace mojo |
OLD | NEW |