OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <utility> | 5 #include <utility> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 return connect_result_; | 84 return connect_result_; |
85 } | 85 } |
86 | 86 |
87 void Send(const std::string& data) { | 87 void Send(const std::string& data) { |
88 write_buffer_ = | 88 write_buffer_ = |
89 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); | 89 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); |
90 Write(); | 90 Write(); |
91 } | 91 } |
92 | 92 |
93 bool Read(std::string* message) { | 93 bool Read(std::string* message) { |
94 net::TestCompletionCallback callback; | 94 return Read(message, 1); |
95 ReadInternal(callback.callback()); | 95 } |
96 int bytes_received = callback.WaitForResult(); | 96 |
97 if (bytes_received <= 0) | 97 bool Read(std::string* message, int expected_bytes) { |
98 return false; | 98 int total_bytes_received = 0; |
mef
2014/04/23 21:46:03
do you need to clear a |message| here?
gunsch
2014/04/24 20:02:47
Done.
| |
99 *message = std::string(read_buffer_->data(), bytes_received); | 99 while (total_bytes_received < expected_bytes) { |
100 net::TestCompletionCallback callback; | |
101 ReadInternal(callback.callback()); | |
102 int bytes_received = callback.WaitForResult(); | |
103 if (bytes_received <= 0) | |
104 return false; | |
105 | |
106 total_bytes_received += bytes_received; | |
107 message->append(read_buffer_->data(), bytes_received); | |
108 } | |
100 return true; | 109 return true; |
101 } | 110 } |
102 | 111 |
103 private: | 112 private: |
104 void OnConnect(const base::Closure& quit_loop, int result) { | 113 void OnConnect(const base::Closure& quit_loop, int result) { |
105 connect_result_ = result; | 114 connect_result_ = result; |
106 quit_loop.Run(); | 115 quit_loop.Run(); |
107 } | 116 } |
108 | 117 |
109 void Write() { | 118 void Write() { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 310 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
302 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 311 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
303 server_->Send200(GetConnectionId(0), "Response!", "text/plain"); | 312 server_->Send200(GetConnectionId(0), "Response!", "text/plain"); |
304 | 313 |
305 std::string response; | 314 std::string response; |
306 ASSERT_TRUE(client.Read(&response)); | 315 ASSERT_TRUE(client.Read(&response)); |
307 ASSERT_TRUE(StartsWithASCII(response, "HTTP/1.1 200 OK", true)); | 316 ASSERT_TRUE(StartsWithASCII(response, "HTTP/1.1 200 OK", true)); |
308 ASSERT_TRUE(EndsWith(response, "Response!", true)); | 317 ASSERT_TRUE(EndsWith(response, "Response!", true)); |
309 } | 318 } |
310 | 319 |
311 // Flaky on at least OS X and Vista. http://crbug.com/365067. | 320 TEST_F(HttpServerTest, SendRaw) { |
312 TEST_F(HttpServerTest, DISABLED_SendRaw) { | |
313 TestHttpClient client; | 321 TestHttpClient client; |
314 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 322 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
315 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 323 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
316 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 324 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
317 server_->SendRaw(GetConnectionId(0), "Raw Data "); | 325 server_->SendRaw(GetConnectionId(0), "Raw Data "); |
318 server_->SendRaw(GetConnectionId(0), "More Data"); | 326 server_->SendRaw(GetConnectionId(0), "More Data"); |
319 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); | 327 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); |
320 | 328 |
321 std::string response; | 329 std::string response; |
322 ASSERT_TRUE(client.Read(&response)); | 330 ASSERT_TRUE(client.Read(&response, 37)); |
mef
2014/04/23 21:46:03
How was this magical number calculated?
gunsch
2014/04/24 20:02:47
Done.
| |
323 ASSERT_EQ("Raw Data More DataThird Piece of Data", response); | 331 ASSERT_EQ("Raw Data More DataThird Piece of Data", response); |
324 } | 332 } |
325 | 333 |
326 namespace { | 334 namespace { |
327 | 335 |
328 class MockStreamListenSocket : public StreamListenSocket { | 336 class MockStreamListenSocket : public StreamListenSocket { |
329 public: | 337 public: |
330 MockStreamListenSocket(StreamListenSocket::Delegate* delegate) | 338 MockStreamListenSocket(StreamListenSocket::Delegate* delegate) |
331 : StreamListenSocket(kInvalidSocket, delegate) {} | 339 : StreamListenSocket(kInvalidSocket, delegate) {} |
332 | 340 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
393 | 401 |
394 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 402 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
395 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 403 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
396 std::string response3; | 404 std::string response3; |
397 ASSERT_TRUE(client.Read(&response3)); | 405 ASSERT_TRUE(client.Read(&response3)); |
398 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 406 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); |
399 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 407 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); |
400 } | 408 } |
401 | 409 |
402 } // namespace net | 410 } // namespace net |
OLD | NEW |