OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <string.h> | 5 #include <string.h> |
6 #include <algorithm> | 6 #include <algorithm> |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 sizeof(kCompressed)); | 44 sizeof(kCompressed)); |
45 } else { | 45 } else { |
46 return false; | 46 return false; |
47 } | 47 } |
48 | 48 |
49 return true; | 49 return true; |
50 } | 50 } |
51 | 51 |
52 class MockJobObserver : public URLRequestJobTracker::JobObserver { | 52 class MockJobObserver : public URLRequestJobTracker::JobObserver { |
53 public: | 53 public: |
54 MOCK_METHOD1(OnJobAdded, void(URLRequestJob* job)); | 54 MOCK_METHOD1(OnJobAdded, void(net::URLRequestJob* job)); |
55 MOCK_METHOD1(OnJobRemoved, void(URLRequestJob* job)); | 55 MOCK_METHOD1(OnJobRemoved, void(net::URLRequestJob* job)); |
56 MOCK_METHOD2(OnJobDone, void(URLRequestJob* job, | 56 MOCK_METHOD2(OnJobDone, void(net::URLRequestJob* job, |
57 const URLRequestStatus& status)); | 57 const URLRequestStatus& status)); |
58 MOCK_METHOD3(OnJobRedirect, void(URLRequestJob* job, | 58 MOCK_METHOD3(OnJobRedirect, void(net::URLRequestJob* job, |
59 const GURL& location, | 59 const GURL& location, |
60 int status_code)); | 60 int status_code)); |
61 MOCK_METHOD3(OnBytesRead, void(URLRequestJob* job, | 61 MOCK_METHOD3(OnBytesRead, void(net::URLRequestJob* job, |
62 const char* buf, | 62 const char* buf, |
63 int byte_count)); | 63 int byte_count)); |
64 }; | 64 }; |
65 | 65 |
66 // A URLRequestJob that returns static content for given URLs. We do | 66 // A net::URLRequestJob that returns static content for given URLs. We do |
67 // not use URLRequestTestJob here because URLRequestTestJob fakes | 67 // not use URLRequestTestJob here because URLRequestTestJob fakes |
68 // async operations by calling ReadRawData synchronously in an async | 68 // async operations by calling ReadRawData synchronously in an async |
69 // callback. This test requires a URLRequestJob that returns false for | 69 // callback. This test requires a net::URLRequestJob that returns false for |
70 // async reads, in order to exercise the real async read codepath. | 70 // async reads, in order to exercise the real async read codepath. |
71 class URLRequestJobTrackerTestJob : public URLRequestJob { | 71 class URLRequestJobTrackerTestJob : public net::URLRequestJob { |
72 public: | 72 public: |
73 URLRequestJobTrackerTestJob(net::URLRequest* request, bool async_reads) | 73 URLRequestJobTrackerTestJob(net::URLRequest* request, bool async_reads) |
74 : URLRequestJob(request), async_reads_(async_reads) {} | 74 : net::URLRequestJob(request), async_reads_(async_reads) {} |
75 | 75 |
76 void Start() { | 76 void Start() { |
77 ASSERT_TRUE(GetResponseBody(request_->url(), &response_data_)); | 77 ASSERT_TRUE(GetResponseBody(request_->url(), &response_data_)); |
78 | 78 |
79 // Start reading asynchronously so that all error reporting and data | 79 // Start reading asynchronously so that all error reporting and data |
80 // callbacks happen as they would for network requests. | 80 // callbacks happen as they would for network requests. |
81 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 81 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
82 this, &URLRequestJobTrackerTestJob::NotifyHeadersComplete)); | 82 this, &URLRequestJobTrackerTestJob::NotifyHeadersComplete)); |
83 } | 83 } |
84 | 84 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 118 } |
119 | 119 |
120 bool GetContentEncodings( | 120 bool GetContentEncodings( |
121 std::vector<Filter::FilterType>* encoding_types) { | 121 std::vector<Filter::FilterType>* encoding_types) { |
122 if (request_->url().spec() == "test:basic") { | 122 if (request_->url().spec() == "test:basic") { |
123 return false; | 123 return false; |
124 } else if (request_->url().spec() == "test:compressed") { | 124 } else if (request_->url().spec() == "test:compressed") { |
125 encoding_types->push_back(Filter::FILTER_TYPE_GZIP); | 125 encoding_types->push_back(Filter::FILTER_TYPE_GZIP); |
126 return true; | 126 return true; |
127 } else { | 127 } else { |
128 return URLRequestJob::GetContentEncodings(encoding_types); | 128 return net::URLRequestJob::GetContentEncodings(encoding_types); |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
132 // The data to send, will be set in Start(). | 132 // The data to send, will be set in Start(). |
133 std::string response_data_; | 133 std::string response_data_; |
134 | 134 |
135 // Should reads be synchronous or asynchronous? | 135 // Should reads be synchronous or asynchronous? |
136 const bool async_reads_; | 136 const bool async_reads_; |
137 }; | 137 }; |
138 | 138 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 EXPECT_EQ(1, d.response_started_count()); | 194 EXPECT_EQ(1, d.response_started_count()); |
195 EXPECT_FALSE(d.received_data_before_response()); | 195 EXPECT_FALSE(d.received_data_before_response()); |
196 EXPECT_STREQ(kBasic, d.data_received().c_str()); | 196 EXPECT_STREQ(kBasic, d.data_received().c_str()); |
197 } | 197 } |
198 | 198 |
199 static net::URLRequest::ProtocolFactory Factory; | 199 static net::URLRequest::ProtocolFactory Factory; |
200 static bool g_async_reads; | 200 static bool g_async_reads; |
201 }; | 201 }; |
202 | 202 |
203 // static | 203 // static |
204 URLRequestJob* URLRequestJobTrackerTest::Factory(net::URLRequest* request, | 204 net::URLRequestJob* URLRequestJobTrackerTest::Factory( |
205 const std::string& scheme) { | 205 net::URLRequest* request, |
| 206 const std::string& scheme) { |
206 return new URLRequestJobTrackerTestJob(request, g_async_reads); | 207 return new URLRequestJobTrackerTestJob(request, g_async_reads); |
207 } | 208 } |
208 | 209 |
209 // static | 210 // static |
210 bool URLRequestJobTrackerTest::g_async_reads = true; | 211 bool URLRequestJobTrackerTest::g_async_reads = true; |
211 | 212 |
212 TEST_F(URLRequestJobTrackerTest, BasicAsync) { | 213 TEST_F(URLRequestJobTrackerTest, BasicAsync) { |
213 g_async_reads = true; | 214 g_async_reads = true; |
214 AssertJobTrackerCallbacks("test:basic"); | 215 AssertJobTrackerCallbacks("test:basic"); |
215 } | 216 } |
216 | 217 |
217 TEST_F(URLRequestJobTrackerTest, BasicSync) { | 218 TEST_F(URLRequestJobTrackerTest, BasicSync) { |
218 g_async_reads = false; | 219 g_async_reads = false; |
219 AssertJobTrackerCallbacks("test:basic"); | 220 AssertJobTrackerCallbacks("test:basic"); |
220 } | 221 } |
221 | 222 |
222 TEST_F(URLRequestJobTrackerTest, CompressedAsync) { | 223 TEST_F(URLRequestJobTrackerTest, CompressedAsync) { |
223 g_async_reads = true; | 224 g_async_reads = true; |
224 AssertJobTrackerCallbacks("test:compressed"); | 225 AssertJobTrackerCallbacks("test:compressed"); |
225 } | 226 } |
226 | 227 |
227 TEST_F(URLRequestJobTrackerTest, CompressedSync) { | 228 TEST_F(URLRequestJobTrackerTest, CompressedSync) { |
228 g_async_reads = false; | 229 g_async_reads = false; |
229 AssertJobTrackerCallbacks("test:compressed"); | 230 AssertJobTrackerCallbacks("test:compressed"); |
230 } | 231 } |
231 | 232 |
232 } // namespace | 233 } // namespace |
OLD | NEW |