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

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: hate 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 }
qsr 2015/02/27 17:15:58 // namespace
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");
qsr 2015/02/27 17:15:58 I think you will have issue on android. You might
Aaron Boodman 2015/02/28 19:08:23 I'm not sure what that would mean. I'm trying to t
qsr 2015/03/02 08:51:31 Ok. For android, the issue is that you cannot us
Aaron Boodman 2015/03/02 15:36:36 I will look at this today. However, as it is my la
38 CHECK(base::PathExists(app_path_));
39 }
40 ~GetHandler() override {}
41
42 private:
43 // http_server::HttpHandler:
44 void HandleRequest(
45 http_server::HttpRequestPtr request,
46 const Callback<void(http_server::HttpResponsePtr)>& callback) override {
47 http_server::HttpResponsePtr response;
48 if (StartsWithASCII(request->relative_url, "/app", true)) {
49 // Super inefficient, but meh.
50 std::string data;
51 base::ReadFileToString(app_path_, &data);
52 response = http_server::CreateHttpResponse(200, data);
53 response->content_type = "application/octet-stream";
54 } else if (request->relative_url == "/redirect") {
55 response = http_server::HttpResponse::New();
56 response->status_code = 302;
57 response->custom_headers.insert("Location", GetURL(port_, "app"));
58 } else {
59 NOTREACHED();
60 }
61
62 callback.Run(response.Pass());
63 }
64
65 Binding<http_server::HttpHandler> binding_;
66 base::FilePath app_path_;
67 uint16_t port_;
68
69 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler);
70 };
71
72 class ShellAppTest : public test::ApplicationTestBase {
73 public:
74 ShellAppTest() : ApplicationTestBase() {}
75 ~ShellAppTest() override {}
76
77 protected:
78 // ApplicationTestBase:
79 void SetUp() override {
80 ApplicationTestBase::SetUp();
81
82 application_impl()->ConnectToService("mojo:http_server",
83 &http_server_factory_);
84
85 mojo::NetAddressPtr local_address(mojo::NetAddress::New());
86 local_address->family = mojo::NET_ADDRESS_FAMILY_IPV4;
87 local_address->ipv4 = mojo::NetAddressIPv4::New();
88 local_address->ipv4->addr.resize(4);
89 local_address->ipv4->addr[0] = 127;
90 local_address->ipv4->addr[1] = 0;
91 local_address->ipv4->addr[2] = 0;
92 local_address->ipv4->addr[3] = 1;
93 local_address->ipv4->port = 0;
94 http_server_factory_->CreateHttpServer(GetProxy(&http_server_),
95 local_address.Pass());
96
97 http_server_->GetPort([this](uint16_t p) { port_ = p; });
98 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall());
99
100 InterfacePtr<http_server::HttpHandler> http_handler;
101 handler_.reset(new GetHandler(GetProxy(&http_handler).Pass(), port_));
102 http_server_->SetHandler(".*", http_handler.Pass(),
103 [](bool result) { EXPECT_TRUE(result); });
104 EXPECT_TRUE(http_server_.WaitForIncomingMethodCall());
105 }
106
107 std::string GetURL(const std::string& path) {
108 return ::mojo::GetURL(port_, path);
109 }
110
111 http_server::HttpServerFactoryPtr http_server_factory_;
112 http_server::HttpServerPtr http_server_;
113 scoped_ptr<GetHandler> handler_;
114 uint16_t port_;
115
116 private:
117 MOJO_DISALLOW_COPY_AND_ASSIGN(ShellAppTest);
118 };
119
120 // Test that we can load apps over http.
121 TEST_F(ShellAppTest, Http) {
122 InterfacePtr<Pingable> pingable;
123 application_impl()->ConnectToService(GetURL("app"), &pingable);
124 pingable->Ping("hello", [](const String& app_url, const String& message) {
125 EXPECT_EQ("hello", message);
126 base::MessageLoop::current()->Quit();
127 });
128 base::RunLoop().Run();
129 }
130
131 // Test that redirects work.
132 // TODO(aa): Test that apps receive the correct URL parameters.
133 TEST_F(ShellAppTest, Redirect) {
134 InterfacePtr<Pingable> pingable;
135 application_impl()->ConnectToService(GetURL("redirect"), &pingable);
136 pingable->Ping("hello", [](const String& app_url, const String& message) {
137 EXPECT_EQ("hello", message);
138 base::MessageLoop::current()->Quit();
139 });
140 base::RunLoop().Run();
141 }
142
143 // Test that querystring is not considered when resolving http applications.
144 TEST_F(ShellAppTest, QueryHandling) {
145 InterfacePtr<Pingable> pingable1;
146 InterfacePtr<Pingable> pingable2;
147 application_impl()->ConnectToService(GetURL("app?foo"), &pingable1);
148 application_impl()->ConnectToService(GetURL("app?bar"), &pingable2);
149
150 int num_responses = 0;
151 auto callback =
152 [this, &num_responses](const String& app_url, const String& message) {
153 EXPECT_EQ(GetURL("app"), app_url);
154 EXPECT_EQ("hello", message);
155 if (++num_responses == 2) {
156 base::MessageLoop::current()->Quit();
157 }
158 };
159 pingable1->Ping("hello", callback);
160 pingable2->Ping("hello", callback);
161 base::RunLoop().Run();
162 }
163
164 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698