OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/base_paths.h" | |
6 #include "base/bind.h" | |
7 #include "base/files/file_path.h" | |
8 #include "base/files/file_util.h" | |
9 #include "base/path_service.h" | |
10 #include "base/run_loop.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "base/strings/stringprintf.h" | |
13 #include "mojo/common/data_pipe_utils.h" | |
14 #include "mojo/public/cpp/application/application_impl.h" | |
15 #include "mojo/public/cpp/application/application_test_base.h" | |
16 #include "mojo/public/cpp/system/macros.h" | |
17 #include "mojo/services/network/public/interfaces/net_address.mojom.h" | |
18 #include "services/http_server/public/http_server.mojom.h" | |
19 #include "services/http_server/public/http_server_factory.mojom.h" | |
20 #include "services/http_server/public/http_server_util.h" | |
21 #include "shell/test/pingable.mojom.h" | |
22 | |
23 namespace mojo { | |
24 | |
25 namespace { | |
26 | |
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()); | |
29 } | |
30 } | |
31 | |
32 class GetHandler : public http_server::HttpHandler { | |
33 public: | |
34 GetHandler(InterfaceRequest<http_server::HttpHandler> request, uint16_t port) | |
35 : binding_(this, request.Pass()), port_(port) { | |
36 CHECK(PathService::Get(base::FILE_MODULE, &app_path_)); | |
37 app_path_ = app_path_.DirName().Append("pingable_app.mojo"); | |
38 CHECK(base::PathExists(app_path_)); | |
39 } | |
40 ~GetHandler() override {} | |
41 | |
42 bool WaitForIncomingMethodCall() { | |
43 return binding_.WaitForIncomingMethodCall(); | |
44 } | |
45 | |
46 private: | |
47 // http_server::HttpHandler: | |
48 void HandleRequest( | |
49 http_server::HttpRequestPtr request, | |
50 const Callback<void(http_server::HttpResponsePtr)>& callback) override { | |
51 http_server::HttpResponsePtr response; | |
52 if (StartsWithASCII(request->relative_url, "/app", true)) { | |
53 // Super inefficient, but meh. | |
54 std::string data; | |
55 base::ReadFileToString(app_path_, &data); | |
56 response = http_server::CreateHttpResponse(200, data); | |
57 response->content_type = "application/octet-stream"; | |
58 } else if (request->relative_url == "/redirect") { | |
59 response = http_server::HttpResponse::New(); | |
60 response->status_code = 302; | |
61 response->custom_headers.insert("Location", GetURL(port_, "app")); | |
62 } else { | |
63 NOTREACHED(); | |
64 } | |
65 | |
66 callback.Run(response.Pass()); | |
67 } | |
68 | |
69 Binding<http_server::HttpHandler> binding_; | |
70 base::FilePath app_path_; | |
71 uint16_t port_; | |
72 | |
73 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); | |
74 }; | |
75 | |
76 class ShellAppTest : public test::ApplicationTestBase { | |
77 public: | |
78 ShellAppTest() : ApplicationTestBase() {} | |
79 ~ShellAppTest() override {} | |
80 | |
81 protected: | |
82 // ApplicationTestBase: | |
83 void SetUp() override { | |
84 ApplicationTestBase::SetUp(); | |
85 | |
86 application_impl()->ConnectToService("mojo:http_server", | |
87 &http_server_factory_); | |
88 | |
89 mojo::NetAddressPtr local_address(mojo::NetAddress::New()); | |
90 local_address->family = mojo::NET_ADDRESS_FAMILY_IPV4; | |
91 local_address->ipv4 = mojo::NetAddressIPv4::New(); | |
92 local_address->ipv4->addr.resize(4); | |
93 local_address->ipv4->addr[0] = 127; | |
94 local_address->ipv4->addr[1] = 0; | |
95 local_address->ipv4->addr[2] = 0; | |
96 local_address->ipv4->addr[3] = 1; | |
97 local_address->ipv4->port = 0; | |
98 http_server_factory_->CreateHttpServer(GetProxy(&http_server_), | |
99 local_address.Pass()); | |
100 | |
101 http_server_->GetPort([this](uint16_t p) { port_ = p; }); | |
102 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall()); | |
103 | |
104 InterfacePtr<http_server::HttpHandler> http_handler; | |
105 handler_.reset(new GetHandler(GetProxy(&http_handler).Pass(), port_)); | |
106 http_server_->SetHandler(".*", http_handler.Pass(), | |
107 [](bool result) { EXPECT_TRUE(result); }); | |
108 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall()); | |
109 } | |
110 | |
111 std::string GetURL(const std::string& path) { | |
112 return ::mojo::GetURL(port_, path); | |
113 } | |
114 | |
115 http_server::HttpServerFactoryPtr http_server_factory_; | |
116 http_server::HttpServerPtr http_server_; | |
117 scoped_ptr<GetHandler> handler_; | |
118 uint16_t port_; | |
119 | |
120 private: | |
121 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellAppTest); | |
122 }; | |
123 | |
124 // Test that we can load apps over http. | |
125 TEST_F(ShellAppTest, Http) { | |
126 InterfacePtr<Pingable> pingable; | |
127 application_impl()->ConnectToService(GetURL("app"), &pingable); | |
128 pingable->Ping("hello", [](const String& app_url, const String& message) { | |
129 EXPECT_EQ("hello", message); | |
130 base::MessageLoop::current()->Quit(); | |
131 }); | |
132 base::RunLoop().Run(); | |
133 } | |
134 | |
135 // Test that redirects work. | |
136 // TODO(aa): Test that apps receive the correct URL parameters. | |
137 // TODO(aa): This test runs and passes, except it crashes with a scary-looking | |
138 // segfault+smashed stack. I suspect something in HttpServer having to do with | |
qsr
2015/02/27 15:01:41
Collin is using the http server with headers and d
| |
139 // using headers. | |
140 TEST_F(ShellAppTest, DISABLED_Redirect) { | |
141 InterfacePtr<Pingable> pingable; | |
142 application_impl()->ConnectToService(GetURL("redirect"), &pingable); | |
143 pingable->Ping("hello", [](const String& app_url, const String& message) { | |
144 EXPECT_EQ("hello", message); | |
145 base::MessageLoop::current()->Quit(); | |
146 }); | |
147 base::RunLoop().Run(); | |
148 } | |
149 | |
150 // Test that querystring is not considered when resolving http applications. | |
151 TEST_F(ShellAppTest, QueryHandling) { | |
152 InterfacePtr<Pingable> pingable1; | |
153 InterfacePtr<Pingable> pingable2; | |
154 application_impl()->ConnectToService(GetURL("app?foo"), &pingable1); | |
155 application_impl()->ConnectToService(GetURL("app?bar"), &pingable2); | |
156 | |
157 int num_responses = 0; | |
158 auto callback = | |
159 [this, &num_responses](const String& app_url, const String& message) { | |
160 EXPECT_EQ(GetURL("app"), app_url); | |
161 EXPECT_EQ("hello", message); | |
162 if (++num_responses == 2) { | |
163 base::MessageLoop::current()->Quit(); | |
164 } | |
165 }; | |
166 pingable1->Ping("hello", callback); | |
167 pingable2->Ping("hello", callback); | |
168 base::RunLoop().Run(); | |
169 } | |
170 | |
171 } // namespace mojo | |
OLD | NEW |