| 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/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/memory/linked_ptr.h" | 6 #include "base/memory/linked_ptr.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 std::string message = data.method + " " + data.url + " HTTP/1.1\r\n"; | 61 std::string message = data.method + " " + data.url + " HTTP/1.1\r\n"; |
| 62 for (const auto& item : data.headers) | 62 for (const auto& item : data.headers) |
| 63 message += item.first + ": " + item.second + "\r\n"; | 63 message += item.first + ": " + item.second + "\r\n"; |
| 64 message += "\r\n"; | 64 message += "\r\n"; |
| 65 if (data.body) | 65 if (data.body) |
| 66 message += *data.body; | 66 message += *data.body; |
| 67 | 67 |
| 68 return message; | 68 return message; |
| 69 } | 69 } |
| 70 | 70 |
| 71 URLResponsePtr MakeResponseStruct(const TestResponse& data) { | 71 HttpResponsePtr MakeResponseStruct(const TestResponse& data) { |
| 72 URLResponsePtr response(URLResponse::New()); | 72 HttpResponsePtr response(HttpResponse::New()); |
| 73 response->status_code = data.status_code; | 73 response->status_code = data.status_code; |
| 74 response->headers.resize(data.headers.size()); | 74 response->headers.resize(data.headers.size()); |
| 75 size_t index = 0; | 75 size_t index = 0; |
| 76 for (const auto& item : data.headers) { | 76 for (const auto& item : data.headers) { |
| 77 HTTPHeaderPtr header(HTTPHeader::New()); | 77 HTTPHeaderPtr header(HTTPHeader::New()); |
| 78 header->name = item.first; | 78 header->name = item.first; |
| 79 header->value = item.second; | 79 header->value = item.second; |
| 80 response->headers[index++] = header.Pass(); | 80 response->headers[index++] = header.Pass(); |
| 81 } | 81 } |
| 82 | 82 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 109 header_map[lower_name] = headers[i]->value; | 109 header_map[lower_name] = headers[i]->value; |
| 110 } | 110 } |
| 111 | 111 |
| 112 for (const auto& item : expected) { | 112 for (const auto& item : expected) { |
| 113 std::string lower_name = base::StringToLowerASCII(item.first); | 113 std::string lower_name = base::StringToLowerASCII(item.first); |
| 114 EXPECT_NE(header_map.end(), header_map.find(lower_name)); | 114 EXPECT_NE(header_map.end(), header_map.find(lower_name)); |
| 115 EXPECT_EQ(item.second, header_map[lower_name]); | 115 EXPECT_EQ(item.second, header_map[lower_name]); |
| 116 } | 116 } |
| 117 } | 117 } |
| 118 | 118 |
| 119 void CheckRequest(const TestRequest& expected, URLRequestPtr request) { | 119 void CheckRequest(const TestRequest& expected, HttpRequestPtr request) { |
| 120 EXPECT_EQ(expected.method, request->method); | 120 EXPECT_EQ(expected.method, request->method); |
| 121 EXPECT_EQ(expected.url, request->url); | 121 EXPECT_EQ(expected.url, request->url); |
| 122 CheckHeaders(expected.headers, request->headers); | 122 CheckHeaders(expected.headers, request->headers); |
| 123 if (expected.body) { | 123 if (expected.body) { |
| 124 EXPECT_EQ(1u, request->body.size()); | 124 EXPECT_TRUE(request->body.is_valid()); |
| 125 std::string body; | 125 std::string body; |
| 126 common::BlockingCopyToString(request->body[0].Pass(), &body); | 126 common::BlockingCopyToString(request->body.Pass(), &body); |
| 127 EXPECT_EQ(*expected.body, body); | 127 EXPECT_EQ(*expected.body, body); |
| 128 } else { | 128 } else { |
| 129 EXPECT_EQ(0u, request->body.size()); | 129 EXPECT_FALSE(request->body.is_valid()); |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 | 132 |
| 133 void CheckResponse(const TestResponse& expected, const std::string& response) { | 133 void CheckResponse(const TestResponse& expected, const std::string& response) { |
| 134 int header_end = | 134 int header_end = |
| 135 net::HttpUtil::LocateEndOfHeaders(response.c_str(), response.size()); | 135 net::HttpUtil::LocateEndOfHeaders(response.c_str(), response.size()); |
| 136 std::string assembled_headers = | 136 std::string assembled_headers = |
| 137 net::HttpUtil::AssembleRawHeaders(response.c_str(), header_end); | 137 net::HttpUtil::AssembleRawHeaders(response.c_str(), header_end); |
| 138 scoped_refptr<net::HttpResponseHeaders> parsed_headers( | 138 scoped_refptr<net::HttpResponseHeaders> parsed_headers( |
| 139 new net::HttpResponseHeaders(assembled_headers)); | 139 new net::HttpResponseHeaders(assembled_headers)); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 scoped_refptr<net::DrainableIOBuffer> write_buffer_; | 257 scoped_refptr<net::DrainableIOBuffer> write_buffer_; |
| 258 scoped_ptr<net::TCPClientSocket> socket_; | 258 scoped_ptr<net::TCPClientSocket> socket_; |
| 259 int connect_result_; | 259 int connect_result_; |
| 260 | 260 |
| 261 DISALLOW_COPY_AND_ASSIGN(TestHttpClient); | 261 DISALLOW_COPY_AND_ASSIGN(TestHttpClient); |
| 262 }; | 262 }; |
| 263 | 263 |
| 264 class HttpConnectionDelegateImpl : public HttpConnectionDelegate { | 264 class HttpConnectionDelegateImpl : public HttpConnectionDelegate { |
| 265 public: | 265 public: |
| 266 struct PendingRequest { | 266 struct PendingRequest { |
| 267 URLRequestPtr request; | 267 HttpRequestPtr request; |
| 268 OnReceivedRequestCallback callback; | 268 OnReceivedRequestCallback callback; |
| 269 }; | 269 }; |
| 270 | 270 |
| 271 HttpConnectionDelegateImpl(HttpConnectionPtr connection, | 271 HttpConnectionDelegateImpl(HttpConnectionPtr connection, |
| 272 InterfaceRequest<HttpConnectionDelegate> request) | 272 InterfaceRequest<HttpConnectionDelegate> request) |
| 273 : connection_(connection.Pass()), | 273 : connection_(connection.Pass()), |
| 274 binding_(this, request.Pass()), | 274 binding_(this, request.Pass()), |
| 275 run_loop_(nullptr), | 275 run_loop_(nullptr), |
| 276 wait_for_request_count_(0) {} | 276 wait_for_request_count_(0) {} |
| 277 ~HttpConnectionDelegateImpl() override {} | 277 ~HttpConnectionDelegateImpl() override {} |
| 278 | 278 |
| 279 // HttpConnectionDelegate implementation: | 279 // HttpConnectionDelegate implementation: |
| 280 void OnReceivedRequest(URLRequestPtr request, | 280 void OnReceivedRequest(HttpRequestPtr request, |
| 281 const OnReceivedRequestCallback& callback) override { | 281 const OnReceivedRequestCallback& callback) override { |
| 282 linked_ptr<PendingRequest> pending_request(new PendingRequest); | 282 linked_ptr<PendingRequest> pending_request(new PendingRequest); |
| 283 pending_request->request = request.Pass(); | 283 pending_request->request = request.Pass(); |
| 284 pending_request->callback = callback; | 284 pending_request->callback = callback; |
| 285 pending_requests_.push_back(pending_request); | 285 pending_requests_.push_back(pending_request); |
| 286 if (run_loop_ && pending_requests_.size() >= wait_for_request_count_) { | 286 if (run_loop_ && pending_requests_.size() >= wait_for_request_count_) { |
| 287 wait_for_request_count_ = 0; | 287 wait_for_request_count_ = 0; |
| 288 run_loop_->Quit(); | 288 run_loop_->Quit(); |
| 289 } | 289 } |
| 290 } | 290 } |
| 291 | 291 |
| 292 void OnReceivedWebSocketRequest( | 292 void OnReceivedWebSocketRequest( |
| 293 URLRequestPtr request, | 293 HttpRequestPtr request, |
| 294 const OnReceivedWebSocketRequestCallback& callback) override { | 294 const OnReceivedWebSocketRequestCallback& callback) override { |
| 295 NOTREACHED(); | 295 NOTREACHED(); |
| 296 } | 296 } |
| 297 | 297 |
| 298 void SendResponse(URLResponsePtr response) { | 298 void SendResponse(HttpResponsePtr response) { |
| 299 ASSERT_FALSE(pending_requests_.empty()); | 299 ASSERT_FALSE(pending_requests_.empty()); |
| 300 linked_ptr<PendingRequest> request = pending_requests_[0]; | 300 linked_ptr<PendingRequest> request = pending_requests_[0]; |
| 301 pending_requests_.erase(pending_requests_.begin()); | 301 pending_requests_.erase(pending_requests_.begin()); |
| 302 request->callback.Run(response.Pass()); | 302 request->callback.Run(response.Pass()); |
| 303 } | 303 } |
| 304 | 304 |
| 305 void WaitForRequest(size_t count) { | 305 void WaitForRequest(size_t count) { |
| 306 DCHECK(!run_loop_); | 306 DCHECK(!run_loop_); |
| 307 | 307 |
| 308 wait_for_request_count_ = count; | 308 wait_for_request_count_ = count; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 make_scoped_ptr(new std::string("This is a test response..."))}; | 472 make_scoped_ptr(new std::string("This is a test response..."))}; |
| 473 connection.SendResponse(MakeResponseStruct(response_data)); | 473 connection.SendResponse(MakeResponseStruct(response_data)); |
| 474 | 474 |
| 475 std::string response_message; | 475 std::string response_message; |
| 476 client.ReadResponse(&response_message); | 476 client.ReadResponse(&response_message); |
| 477 | 477 |
| 478 CheckResponse(response_data, response_message); | 478 CheckResponse(response_data, response_message); |
| 479 } | 479 } |
| 480 | 480 |
| 481 } // namespace mojo | 481 } // namespace mojo |
| OLD | NEW |