| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "mojo/common/data_pipe_utils.h" | 8 #include "mojo/common/data_pipe_utils.h" |
| 9 #include "mojo/public/cpp/application/application_impl.h" | 9 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "mojo/public/cpp/application/application_test_base.h" | 10 #include "mojo/public/cpp/application/application_test_base.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 // Test handler that responds to all requests with the status OK and | 45 // Test handler that responds to all requests with the status OK and |
| 46 // kExampleMessage. | 46 // kExampleMessage. |
| 47 class GetHandler : public http_server::HttpHandler { | 47 class GetHandler : public http_server::HttpHandler { |
| 48 public: | 48 public: |
| 49 GetHandler(mojo::InterfaceRequest<HttpHandler> request) | 49 GetHandler(mojo::InterfaceRequest<HttpHandler> request) |
| 50 : binding_(this, request.Pass()) {} | 50 : binding_(this, request.Pass()) {} |
| 51 ~GetHandler() override {} | 51 ~GetHandler() override {} |
| 52 | 52 |
| 53 bool WaitForIncomingMethodCall() { | |
| 54 return binding_.WaitForIncomingMethodCall(); | |
| 55 } | |
| 56 | |
| 57 private: | 53 private: |
| 58 // http_server::HttpHandler: | 54 // http_server::HttpHandler: |
| 59 void HandleRequest(http_server::HttpRequestPtr request, | 55 void HandleRequest(http_server::HttpRequestPtr request, |
| 60 const mojo::Callback<void(http_server::HttpResponsePtr)>& | 56 const mojo::Callback<void(http_server::HttpResponsePtr)>& |
| 61 callback) override { | 57 callback) override { |
| 62 callback.Run(http_server::CreateHttpResponse(200, kExampleMessage)); | 58 callback.Run(http_server::CreateHttpResponse(200, kExampleMessage)); |
| 63 } | 59 } |
| 64 | 60 |
| 65 mojo::Binding<http_server::HttpHandler> binding_; | 61 mojo::Binding<http_server::HttpHandler> binding_; |
| 66 | 62 |
| 67 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); | 63 MOJO_DISALLOW_COPY_AND_ASSIGN(GetHandler); |
| 68 }; | 64 }; |
| 69 | 65 |
| 70 // Test handler that responds to POST requests with the status OK and message | 66 // Test handler that responds to POST requests with the status OK and message |
| 71 // read from the payload of the request. | 67 // read from the payload of the request. |
| 72 class PostHandler : public http_server::HttpHandler { | 68 class PostHandler : public http_server::HttpHandler { |
| 73 public: | 69 public: |
| 74 PostHandler(mojo::InterfaceRequest<HttpHandler> request) | 70 PostHandler(mojo::InterfaceRequest<HttpHandler> request) |
| 75 : binding_(this, request.Pass()) {} | 71 : binding_(this, request.Pass()) {} |
| 76 ~PostHandler() override {} | 72 ~PostHandler() override {} |
| 77 | 73 |
| 78 bool WaitForIncomingMethodCall() { | |
| 79 return binding_.WaitForIncomingMethodCall(); | |
| 80 } | |
| 81 | |
| 82 private: | 74 private: |
| 83 // http_server::HttpHandler: | 75 // http_server::HttpHandler: |
| 84 void HandleRequest(http_server::HttpRequestPtr request, | 76 void HandleRequest(http_server::HttpRequestPtr request, |
| 85 const mojo::Callback<void(http_server::HttpResponsePtr)>& | 77 const mojo::Callback<void(http_server::HttpResponsePtr)>& |
| 86 callback) override { | 78 callback) override { |
| 87 DCHECK_EQ("POST", request->method); | 79 DCHECK_EQ("POST", request->method); |
| 88 std::string message; | 80 std::string message; |
| 89 mojo::common::BlockingCopyToString(request->body.Pass(), &message); | 81 mojo::common::BlockingCopyToString(request->body.Pass(), &message); |
| 90 callback.Run(http_server::CreateHttpResponse(200, message)); | 82 callback.Run(http_server::CreateHttpResponse(200, message)); |
| 91 } | 83 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 EXPECT_EQ(kExampleMessage, response_body); | 135 EXPECT_EQ(kExampleMessage, response_body); |
| 144 base::MessageLoop::current()->Quit(); | 136 base::MessageLoop::current()->Quit(); |
| 145 } | 137 } |
| 146 | 138 |
| 147 // Verifies that the server responds to http GET requests using example | 139 // Verifies that the server responds to http GET requests using example |
| 148 // GetHandler. | 140 // GetHandler. |
| 149 TEST_F(HttpServerApplicationTest, ServerResponse) { | 141 TEST_F(HttpServerApplicationTest, ServerResponse) { |
| 150 http_server::HttpServerPtr http_server(CreateHttpServer()); | 142 http_server::HttpServerPtr http_server(CreateHttpServer()); |
| 151 uint16_t assigned_port; | 143 uint16_t assigned_port; |
| 152 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); | 144 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); |
| 153 http_server.WaitForIncomingMethodCall(); | 145 http_server.WaitForIncomingResponse(); |
| 154 | 146 |
| 155 HttpHandlerPtr http_handler_ptr; | 147 HttpHandlerPtr http_handler_ptr; |
| 156 GetHandler handler(GetProxy(&http_handler_ptr).Pass()); | 148 GetHandler handler(GetProxy(&http_handler_ptr).Pass()); |
| 157 | 149 |
| 158 // Set the test handler and wait for confirmation. | 150 // Set the test handler and wait for confirmation. |
| 159 http_server->SetHandler("/test", http_handler_ptr.Pass(), | 151 http_server->SetHandler("/test", http_handler_ptr.Pass(), |
| 160 [](bool result) { EXPECT_TRUE(result); }); | 152 [](bool result) { EXPECT_TRUE(result); }); |
| 161 http_server.WaitForIncomingMethodCall(); | 153 http_server.WaitForIncomingResponse(); |
| 162 | 154 |
| 163 mojo::URLLoaderPtr url_loader; | 155 mojo::URLLoaderPtr url_loader; |
| 164 network_service_->CreateURLLoader(GetProxy(&url_loader)); | 156 network_service_->CreateURLLoader(GetProxy(&url_loader)); |
| 165 | 157 |
| 166 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); | 158 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); |
| 167 url_request->url = | 159 url_request->url = |
| 168 base::StringPrintf("http://127.0.0.1:%u/test", assigned_port); | 160 base::StringPrintf("http://127.0.0.1:%u/test", assigned_port); |
| 169 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); | 161 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); |
| 170 base::RunLoop run_loop; | 162 base::RunLoop run_loop; |
| 171 run_loop.Run(); | 163 run_loop.Run(); |
| 172 } | 164 } |
| 173 | 165 |
| 174 // Verifies that the server correctly passes the POST request payload using | 166 // Verifies that the server correctly passes the POST request payload using |
| 175 // example PostHandler. | 167 // example PostHandler. |
| 176 TEST_F(HttpServerApplicationTest, PostData) { | 168 TEST_F(HttpServerApplicationTest, PostData) { |
| 177 http_server::HttpServerPtr http_server(CreateHttpServer()); | 169 http_server::HttpServerPtr http_server(CreateHttpServer()); |
| 178 uint16_t assigned_port; | 170 uint16_t assigned_port; |
| 179 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); | 171 http_server->GetPort([&assigned_port](uint16_t p) { assigned_port = p; }); |
| 180 http_server.WaitForIncomingMethodCall(); | 172 http_server.WaitForIncomingResponse(); |
| 181 | 173 |
| 182 HttpHandlerPtr http_handler_ptr; | 174 HttpHandlerPtr http_handler_ptr; |
| 183 PostHandler handler(GetProxy(&http_handler_ptr).Pass()); | 175 PostHandler handler(GetProxy(&http_handler_ptr).Pass()); |
| 184 | 176 |
| 185 // Set the test handler and wait for confirmation. | 177 // Set the test handler and wait for confirmation. |
| 186 http_server->SetHandler("/post", http_handler_ptr.Pass(), | 178 http_server->SetHandler("/post", http_handler_ptr.Pass(), |
| 187 [](bool result) { EXPECT_TRUE(result); }); | 179 [](bool result) { EXPECT_TRUE(result); }); |
| 188 http_server.WaitForIncomingMethodCall(); | 180 http_server.WaitForIncomingResponse(); |
| 189 | 181 |
| 190 mojo::URLLoaderPtr url_loader; | 182 mojo::URLLoaderPtr url_loader; |
| 191 network_service_->CreateURLLoader(GetProxy(&url_loader)); | 183 network_service_->CreateURLLoader(GetProxy(&url_loader)); |
| 192 | 184 |
| 193 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); | 185 mojo::URLRequestPtr url_request = mojo::URLRequest::New(); |
| 194 url_request->url = | 186 url_request->url = |
| 195 base::StringPrintf("http://127.0.0.1:%u/post", assigned_port); | 187 base::StringPrintf("http://127.0.0.1:%u/post", assigned_port); |
| 196 url_request->method = "POST"; | 188 url_request->method = "POST"; |
| 197 url_request->body.resize(1); | 189 url_request->body.resize(1); |
| 198 WriteMessageToDataPipe(kExampleMessage, &url_request->body[0]); | 190 WriteMessageToDataPipe(kExampleMessage, &url_request->body[0]); |
| 199 | 191 |
| 200 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); | 192 url_loader->Start(url_request.Pass(), base::Bind(&CheckServerResponse)); |
| 201 base::RunLoop run_loop; | 193 base::RunLoop run_loop; |
| 202 run_loop.Run(); | 194 run_loop.Run(); |
| 203 } | 195 } |
| 204 | 196 |
| 205 } // namespace http_server | 197 } // namespace http_server |
| OLD | NEW |