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

Side by Side Diff: shell/shell_apptest.cc

Issue 943053003: Simple multi-url support for mojo apps (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 9 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
(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 } // namespace
32
33 class GetHandler : public http_server::HttpHandler {
34 public:
35 GetHandler(InterfaceRequest<http_server::HttpHandler> request, uint16_t 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 }
41 ~GetHandler() override {}
42
43 private:
44 // http_server::HttpHandler:
45 void HandleRequest(
46 http_server::HttpRequestPtr request,
47 const Callback<void(http_server::HttpResponsePtr)>& callback) override {
48 http_server::HttpResponsePtr response;
49 if (StartsWithASCII(request->relative_url, "/app", true)) {
50 // Super inefficient, but meh.
51 std::string data;
52 base::ReadFileToString(app_path_, &data);
53 response = http_server::CreateHttpResponse(200, data);
54 response->content_type = "application/octet-stream";
55 } else if (request->relative_url == "/redirect") {
56 response = http_server::HttpResponse::New();
57 response->status_code = 302;
58 response->custom_headers.insert("Location", GetURL(port_, "app"));
59 } else {
60 NOTREACHED();
61 }
62
63 callback.Run(response.Pass());
64 }
65
66 Binding<http_server::HttpHandler> binding_;
67 base::FilePath app_path_;
68 uint16_t port_;
69
70 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler);
71 };
72
73 class ShellAppTest : public test::ApplicationTestBase {
74 public:
75 ShellAppTest() : ApplicationTestBase() {}
76 ~ShellAppTest() override {}
77
78 protected:
79 // ApplicationTestBase:
80 void SetUp() override {
81 ApplicationTestBase::SetUp();
82
83 application_impl()->ConnectToService("mojo:http_server",
84 &http_server_factory_);
85
86 mojo::NetAddressPtr local_address(mojo::NetAddress::New());
87 local_address->family = mojo::NET_ADDRESS_FAMILY_IPV4;
88 local_address->ipv4 = mojo::NetAddressIPv4::New();
89 local_address->ipv4->addr.resize(4);
90 local_address->ipv4->addr[0] = 127;
91 local_address->ipv4->addr[1] = 0;
92 local_address->ipv4->addr[2] = 0;
93 local_address->ipv4->addr[3] = 1;
94 local_address->ipv4->port = 0;
95 http_server_factory_->CreateHttpServer(GetProxy(&http_server_),
96 local_address.Pass());
97
98 http_server_->GetPort([this](uint16_t p) { port_ = p; });
99 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall());
100
101 InterfacePtr<http_server::HttpHandler> http_handler;
102 handler_.reset(new GetHandler(GetProxy(&http_handler).Pass(), port_));
103 http_server_->SetHandler(".*", http_handler.Pass(),
104 [](bool result) { EXPECT_TRUE(result); });
105 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall());
106 }
107
108 std::string GetURL(const std::string& path) {
109 return ::mojo::GetURL(port_, path);
110 }
111
112 http_server::HttpServerFactoryPtr http_server_factory_;
113 http_server::HttpServerPtr http_server_;
114 scoped_ptr<GetHandler> handler_;
115 uint16_t port_;
116
117 private:
118 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellAppTest);
119 };
120
121 // Test that we can load apps over http.
122 TEST_F(ShellAppTest, Http) {
123 InterfacePtr<Pingable> pingable;
124 application_impl()->ConnectToService(GetURL("app"), &pingable);
125 pingable->Ping("hello",
126 [this](const String& app_url, const String& connection_url,
127 const String& message) {
128 EXPECT_EQ(GetURL("app"), app_url);
129 EXPECT_EQ(GetURL("app"), connection_url);
130 EXPECT_EQ("hello", message);
131 base::MessageLoop::current()->Quit();
132 });
133 base::RunLoop().Run();
134 }
135
136 // Test that redirects work.
137 // TODO(aa): Test that apps receive the correct URL parameters.
138 TEST_F(ShellAppTest, Redirect) {
139 InterfacePtr<Pingable> pingable;
140 application_impl()->ConnectToService(GetURL("redirect"), &pingable);
141 pingable->Ping("hello",
142 [this](const String& app_url, const String& connection_url,
143 const String& message) {
144 EXPECT_EQ(GetURL("app"), app_url);
145 EXPECT_EQ(GetURL("app"), connection_url);
146 EXPECT_EQ("hello", message);
147 base::MessageLoop::current()->Quit();
148 });
149 base::RunLoop().Run();
150 }
151
152 // Test that querystring is not considered when resolving http applications.
153 TEST_F(ShellAppTest, QueryHandling) {
154 InterfacePtr<Pingable> pingable1;
155 InterfacePtr<Pingable> pingable2;
156 application_impl()->ConnectToService(GetURL("app?foo"), &pingable1);
157 application_impl()->ConnectToService(GetURL("app?bar"), &pingable2);
158
159 int num_responses = 0;
160 auto callback = [this, &num_responses](const String& app_url,
161 const String& connection_url,
162 const String& message) {
163 EXPECT_EQ(GetURL("app"), app_url);
164 EXPECT_EQ("hello", message);
165 ++num_responses;
166 if (num_responses == 1) {
167 EXPECT_EQ(GetURL("app?foo"), connection_url);
168 } else if (num_responses == 2) {
169 EXPECT_EQ(GetURL("app?bar"), connection_url);
170 base::MessageLoop::current()->Quit();
171 } else {
172 CHECK(false);
173 }
174 };
175 pingable1->Ping("hello", callback);
176 pingable2->Ping("hello", callback);
177 base::RunLoop().Run();
178 }
179
180 // mojo: URLs can have querystrings too
181 TEST_F(ShellAppTest, MojoURLQueryHandling) {
182 InterfacePtr<Pingable> pingable;
183 application_impl()->ConnectToService("mojo:pingable_app?foo", &pingable);
184 auto callback = [this](const String& app_url,
185 const String& connection_url,
186 const String& message) {
187 EXPECT_TRUE(EndsWith(app_url, "/pingable_app.mojo", true));
188 EXPECT_EQ(app_url.To<std::string>() + "?foo", connection_url);
189 EXPECT_EQ("hello", message);
190 base::MessageLoop::current()->Quit();
191 };
192 pingable->Ping("hello", callback);
193 base::RunLoop().Run();
194
195 }
196
197 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698