OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 5 #ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
6 #define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 6 #define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
7 | 7 |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include <sstream> | 10 #include <sstream> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
18 #include "base/platform_thread.h" | 18 #include "base/platform_thread.h" |
19 #include "base/process_util.h" | 19 #include "base/process_util.h" |
20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
21 #include "base/thread.h" | 21 #include "base/thread.h" |
22 #include "base/time.h" | 22 #include "base/time.h" |
23 #include "base/waitable_event.h" | 23 #include "base/waitable_event.h" |
| 24 #include "net/base/io_buffer.h" |
24 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
25 #include "net/http/http_network_layer.h" | 26 #include "net/http/http_network_layer.h" |
26 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
27 #include "net/proxy/proxy_service.h" | 28 #include "net/proxy/proxy_service.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 29 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "googleurl/src/url_util.h" | 30 #include "googleurl/src/url_util.h" |
30 | 31 |
31 const int kHTTPDefaultPort = 1337; | 32 const int kHTTPDefaultPort = 1337; |
32 const int kFTPDefaultPort = 1338; | 33 const int kFTPDefaultPort = 1338; |
33 | 34 |
(...skipping 21 matching lines...) Expand all Loading... |
55 TestDelegate() | 56 TestDelegate() |
56 : cancel_in_rr_(false), | 57 : cancel_in_rr_(false), |
57 cancel_in_rs_(false), | 58 cancel_in_rs_(false), |
58 cancel_in_rd_(false), | 59 cancel_in_rd_(false), |
59 cancel_in_rd_pending_(false), | 60 cancel_in_rd_pending_(false), |
60 quit_on_complete_(true), | 61 quit_on_complete_(true), |
61 response_started_count_(0), | 62 response_started_count_(0), |
62 received_bytes_count_(0), | 63 received_bytes_count_(0), |
63 received_redirect_count_(0), | 64 received_redirect_count_(0), |
64 received_data_before_response_(false), | 65 received_data_before_response_(false), |
65 request_failed_(false) { | 66 request_failed_(false), |
| 67 buf_(new net::IOBuffer(kBufferSize)) { |
66 } | 68 } |
67 | 69 |
68 virtual void OnReceivedRedirect(URLRequest* request, const GURL& new_url) { | 70 virtual void OnReceivedRedirect(URLRequest* request, const GURL& new_url) { |
69 received_redirect_count_++; | 71 received_redirect_count_++; |
70 if (cancel_in_rr_) | 72 if (cancel_in_rr_) |
71 request->Cancel(); | 73 request->Cancel(); |
72 } | 74 } |
73 | 75 |
74 virtual void OnResponseStarted(URLRequest* request) { | 76 virtual void OnResponseStarted(URLRequest* request) { |
75 // It doesn't make sense for the request to have IO pending at this point. | 77 // It doesn't make sense for the request to have IO pending at this point. |
76 DCHECK(!request->status().is_io_pending()); | 78 DCHECK(!request->status().is_io_pending()); |
77 | 79 |
78 response_started_count_++; | 80 response_started_count_++; |
79 if (cancel_in_rs_) { | 81 if (cancel_in_rs_) { |
80 request->Cancel(); | 82 request->Cancel(); |
81 OnResponseCompleted(request); | 83 OnResponseCompleted(request); |
82 } else if (!request->status().is_success()) { | 84 } else if (!request->status().is_success()) { |
83 DCHECK(request->status().status() == URLRequestStatus::FAILED || | 85 DCHECK(request->status().status() == URLRequestStatus::FAILED || |
84 request->status().status() == URLRequestStatus::CANCELED); | 86 request->status().status() == URLRequestStatus::CANCELED); |
85 request_failed_ = true; | 87 request_failed_ = true; |
86 OnResponseCompleted(request); | 88 OnResponseCompleted(request); |
87 } else { | 89 } else { |
88 // Initiate the first read. | 90 // Initiate the first read. |
89 int bytes_read = 0; | 91 int bytes_read = 0; |
90 if (request->Read(buf_, sizeof(buf_), &bytes_read)) | 92 if (request->Read(buf_, kBufferSize, &bytes_read)) |
91 OnReadCompleted(request, bytes_read); | 93 OnReadCompleted(request, bytes_read); |
92 else if (!request->status().is_io_pending()) | 94 else if (!request->status().is_io_pending()) |
93 OnResponseCompleted(request); | 95 OnResponseCompleted(request); |
94 } | 96 } |
95 } | 97 } |
96 | 98 |
97 virtual void OnReadCompleted(URLRequest* request, int bytes_read) { | 99 virtual void OnReadCompleted(URLRequest* request, int bytes_read) { |
98 // It doesn't make sense for the request to have IO pending at this point. | 100 // It doesn't make sense for the request to have IO pending at this point. |
99 DCHECK(!request->status().is_io_pending()); | 101 DCHECK(!request->status().is_io_pending()); |
100 | 102 |
101 if (response_started_count_ == 0) | 103 if (response_started_count_ == 0) |
102 received_data_before_response_ = true; | 104 received_data_before_response_ = true; |
103 | 105 |
104 if (cancel_in_rd_) | 106 if (cancel_in_rd_) |
105 request->Cancel(); | 107 request->Cancel(); |
106 | 108 |
107 if (bytes_read >= 0) { | 109 if (bytes_read >= 0) { |
108 // There is data to read. | 110 // There is data to read. |
109 received_bytes_count_ += bytes_read; | 111 received_bytes_count_ += bytes_read; |
110 | 112 |
111 // consume the data | 113 // consume the data |
112 data_received_.append(buf_, bytes_read); | 114 data_received_.append(buf_->data(), bytes_read); |
113 } | 115 } |
114 | 116 |
115 // If it was not end of stream, request to read more. | 117 // If it was not end of stream, request to read more. |
116 if (request->status().is_success() && bytes_read > 0) { | 118 if (request->status().is_success() && bytes_read > 0) { |
117 bytes_read = 0; | 119 bytes_read = 0; |
118 while (request->Read(buf_, sizeof(buf_), &bytes_read)) { | 120 while (request->Read(buf_, kBufferSize, &bytes_read)) { |
119 if (bytes_read > 0) { | 121 if (bytes_read > 0) { |
120 data_received_.append(buf_, bytes_read); | 122 data_received_.append(buf_->data(), bytes_read); |
121 received_bytes_count_ += bytes_read; | 123 received_bytes_count_ += bytes_read; |
122 } else { | 124 } else { |
123 break; | 125 break; |
124 } | 126 } |
125 } | 127 } |
126 } | 128 } |
127 if (!request->status().is_io_pending()) | 129 if (!request->status().is_io_pending()) |
128 OnResponseCompleted(request); | 130 OnResponseCompleted(request); |
129 else if (cancel_in_rd_pending_) | 131 else if (cancel_in_rd_pending_) |
130 request->Cancel(); | 132 request->Cancel(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 const std::string& data_received() const { return data_received_; } | 168 const std::string& data_received() const { return data_received_; } |
167 int bytes_received() const { return static_cast<int>(data_received_.size()); } | 169 int bytes_received() const { return static_cast<int>(data_received_.size()); } |
168 int response_started_count() const { return response_started_count_; } | 170 int response_started_count() const { return response_started_count_; } |
169 int received_redirect_count() const { return received_redirect_count_; } | 171 int received_redirect_count() const { return received_redirect_count_; } |
170 bool received_data_before_response() const { | 172 bool received_data_before_response() const { |
171 return received_data_before_response_; | 173 return received_data_before_response_; |
172 } | 174 } |
173 bool request_failed() const { return request_failed_; } | 175 bool request_failed() const { return request_failed_; } |
174 | 176 |
175 private: | 177 private: |
| 178 static const int kBufferSize = 4096; |
176 // options for controlling behavior | 179 // options for controlling behavior |
177 bool cancel_in_rr_; | 180 bool cancel_in_rr_; |
178 bool cancel_in_rs_; | 181 bool cancel_in_rs_; |
179 bool cancel_in_rd_; | 182 bool cancel_in_rd_; |
180 bool cancel_in_rd_pending_; | 183 bool cancel_in_rd_pending_; |
181 bool quit_on_complete_; | 184 bool quit_on_complete_; |
182 | 185 |
183 std::wstring username_; | 186 std::wstring username_; |
184 std::wstring password_; | 187 std::wstring password_; |
185 | 188 |
186 // tracks status of callbacks | 189 // tracks status of callbacks |
187 int response_started_count_; | 190 int response_started_count_; |
188 int received_bytes_count_; | 191 int received_bytes_count_; |
189 int received_redirect_count_; | 192 int received_redirect_count_; |
190 bool received_data_before_response_; | 193 bool received_data_before_response_; |
191 bool request_failed_; | 194 bool request_failed_; |
192 std::string data_received_; | 195 std::string data_received_; |
193 | 196 |
194 // our read buffer | 197 // our read buffer |
195 char buf_[4096]; | 198 scoped_refptr<net::IOBuffer> buf_; |
196 }; | 199 }; |
197 | 200 |
198 // This object bounds the lifetime of an external python-based HTTP/FTP server | 201 // This object bounds the lifetime of an external python-based HTTP/FTP server |
199 // that can provide various responses useful for testing. | 202 // that can provide various responses useful for testing. |
200 class BaseTestServer : public base::ProcessFilter, | 203 class BaseTestServer : public base::ProcessFilter, |
201 public base::RefCounted<BaseTestServer> { | 204 public base::RefCounted<BaseTestServer> { |
202 protected: | 205 protected: |
203 BaseTestServer() | 206 BaseTestServer() |
204 : process_handle_(NULL) { | 207 : process_handle_(NULL) { |
205 } | 208 } |
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 command_line->push_back("python"); | 682 command_line->push_back("python"); |
680 command_line->push_back(WideToUTF8(testserver_path)); | 683 command_line->push_back(WideToUTF8(testserver_path)); |
681 command_line->push_back(" -f "); | 684 command_line->push_back(" -f "); |
682 command_line->push_back("--data-dir=" + WideToUTF8(test_data_directory)); | 685 command_line->push_back("--data-dir=" + WideToUTF8(test_data_directory)); |
683 command_line->push_back("--port=" + port_str_); | 686 command_line->push_back("--port=" + port_str_); |
684 } | 687 } |
685 #endif | 688 #endif |
686 }; | 689 }; |
687 | 690 |
688 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | 691 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ |
OLD | NEW |