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