Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: chrome/browser/local_discovery/privet_url_fetcher_unittest.cc

Issue 156143002: Add ability to get raw data and range requests to to PrivetURLFetcher (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/local_discovery/privet_url_fetcher.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "chrome/browser/local_discovery/privet_url_fetcher.h" 5 #include "chrome/browser/local_discovery/privet_url_fetcher.h"
6 #include "net/url_request/test_url_fetcher_factory.h" 6 #include "net/url_request/test_url_fetcher_factory.h"
7 #include "net/url_request/url_request_test_util.h" 7 #include "net/url_request/url_request_test_util.h"
8 #include "testing/gmock/include/gmock/gmock.h" 8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 using testing::StrictMock; 11 using testing::StrictMock;
12 12
13 namespace local_discovery { 13 namespace local_discovery {
14 14
15 namespace { 15 namespace {
16 16
17 const char kSamplePrivetURL[] = 17 const char kSamplePrivetURL[] =
18 "http://10.0.0.8:7676/privet/register?action=start"; 18 "http://10.0.0.8:7676/privet/register?action=start";
19 const char kSamplePrivetToken[] = "MyToken"; 19 const char kSamplePrivetToken[] = "MyToken";
20 const char kEmptyPrivetToken[] = "\"\""; 20 const char kEmptyPrivetToken[] = "\"\"";
21 21
22 const char kSampleParsableJSON[] = "{ \"hello\" : 2 }"; 22 const char kSampleParsableJSON[] = "{ \"hello\" : 2 }";
23 const char kSampleUnparsableJSON[] = "{ \"hello\" : }"; 23 const char kSampleUnparsableJSON[] = "{ \"hello\" : }";
24 const char kSampleJSONWithError[] = "{ \"error\" : \"unittest_example\" }"; 24 const char kSampleJSONWithError[] = "{ \"error\" : \"unittest_example\" }";
25 25
26 class MockPrivetURLFetcherDelegate : public PrivetURLFetcher::Delegate { 26 class MockPrivetURLFetcherDelegate : public PrivetURLFetcher::Delegate {
27 public: 27 public:
28 MockPrivetURLFetcherDelegate() : raw_mode_(false) {
29 }
30
31 virtual ~MockPrivetURLFetcherDelegate() {
32 }
33
28 virtual void OnError(PrivetURLFetcher* fetcher, 34 virtual void OnError(PrivetURLFetcher* fetcher,
29 PrivetURLFetcher::ErrorType error) OVERRIDE { 35 PrivetURLFetcher::ErrorType error) OVERRIDE {
30 OnErrorInternal(error); 36 OnErrorInternal(error);
31 } 37 }
32 38
33 MOCK_METHOD1(OnErrorInternal, void(PrivetURLFetcher::ErrorType error)); 39 MOCK_METHOD1(OnErrorInternal, void(PrivetURLFetcher::ErrorType error));
34 40
35 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 41 virtual void OnParsedJson(PrivetURLFetcher* fetcher,
36 const base::DictionaryValue* value, 42 const base::DictionaryValue* value,
37 bool has_error) OVERRIDE { 43 bool has_error) OVERRIDE {
38 saved_value_.reset(value->DeepCopy()); 44 saved_value_.reset(value->DeepCopy());
39 OnParsedJsonInternal(has_error); 45 OnParsedJsonInternal(has_error);
40 } 46 }
41 47
42 MOCK_METHOD1(OnParsedJsonInternal, void(bool has_error)); 48 MOCK_METHOD1(OnParsedJsonInternal, void(bool has_error));
43 49
44 virtual void OnNeedPrivetToken( 50 virtual void OnNeedPrivetToken(
45 PrivetURLFetcher* fetcher, 51 PrivetURLFetcher* fetcher,
46 const PrivetURLFetcher::TokenCallback& callback) { 52 const PrivetURLFetcher::TokenCallback& callback) {
47 } 53 }
48 54
55 bool OnRawData(PrivetURLFetcher* fetcher,
56 bool response_is_file,
57 const std::string& data,
58 const base::FilePath& response_file) {
59 if (!raw_mode_) return false;
60
61 if (response_is_file) {
62 EXPECT_TRUE(response_file != base::FilePath());
63 OnFileInternal();
64 } else {
65 OnRawDataInternal(data);
66 }
67
68 return true;
69 }
70
71 MOCK_METHOD1(OnRawDataInternal, void(std::string data));
72
73 MOCK_METHOD0(OnFileInternal, void());
74
49 const base::DictionaryValue* saved_value() { return saved_value_.get(); } 75 const base::DictionaryValue* saved_value() { return saved_value_.get(); }
50 76
77 void SetRawMode(bool raw_mode) {
78 raw_mode_ = raw_mode;
79 }
80
51 private: 81 private:
52 scoped_ptr<base::DictionaryValue> saved_value_; 82 scoped_ptr<base::DictionaryValue> saved_value_;
83 bool raw_mode_;
53 }; 84 };
54 85
55 class PrivetURLFetcherTest : public ::testing::Test { 86 class PrivetURLFetcherTest : public ::testing::Test {
56 public: 87 public:
57 PrivetURLFetcherTest() { 88 PrivetURLFetcherTest() {
58 request_context_= new net::TestURLRequestContextGetter( 89 request_context_= new net::TestURLRequestContextGetter(
59 base::MessageLoopProxy::current()); 90 base::MessageLoopProxy::current());
60 privet_urlfetcher_.reset(new PrivetURLFetcher( 91 privet_urlfetcher_.reset(new PrivetURLFetcher(
61 kSamplePrivetToken, 92 kSamplePrivetToken,
62 GURL(kSamplePrivetURL), 93 GURL(kSamplePrivetURL),
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 ASSERT_TRUE(fetcher != NULL); 230 ASSERT_TRUE(fetcher != NULL);
200 fetcher->SetResponseString(kSampleJSONWithError); 231 fetcher->SetResponseString(kSampleJSONWithError);
201 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 232 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
202 net::OK)); 233 net::OK));
203 fetcher->set_response_code(200); 234 fetcher->set_response_code(200);
204 235
205 EXPECT_CALL(delegate_, OnParsedJsonInternal(true)); 236 EXPECT_CALL(delegate_, OnParsedJsonInternal(true));
206 fetcher->delegate()->OnURLFetchComplete(fetcher); 237 fetcher->delegate()->OnURLFetchComplete(fetcher);
207 } 238 }
208 239
240 TEST_F(PrivetURLFetcherTest, FetcherRawData) {
241 delegate_.SetRawMode(true);
242 privet_urlfetcher_->Start();
243 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
244 ASSERT_TRUE(fetcher != NULL);
245 fetcher->SetResponseString(kSampleJSONWithError);
246 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
247 net::OK));
248 fetcher->set_response_code(200);
249
250 EXPECT_CALL(delegate_, OnRawDataInternal(kSampleJSONWithError));
251 fetcher->delegate()->OnURLFetchComplete(fetcher);
252 }
253
254 TEST_F(PrivetURLFetcherTest, RangeRequest) {
255 delegate_.SetRawMode(true);
256 privet_urlfetcher_->SetByteRange(200, 300);
257 privet_urlfetcher_->Start();
258 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
259 ASSERT_TRUE(fetcher != NULL);
260 net::HttpRequestHeaders headers;
261 fetcher->GetExtraRequestHeaders(&headers);
262
263 std::string header_range;
264 ASSERT_TRUE(headers.GetHeader("Range", &header_range));
265 EXPECT_EQ("bytes=200-300", header_range);
266 }
267
268 TEST_F(PrivetURLFetcherTest, FetcherToFile) {
269 delegate_.SetRawMode(true);
270 privet_urlfetcher_->SaveResponseToFile();
271 privet_urlfetcher_->Start();
272 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
273 ASSERT_TRUE(fetcher != NULL);
274 fetcher->SetResponseFilePath(
275 base::FilePath(FILE_PATH_LITERAL("sample/file")));
276 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
277 net::OK));
278 fetcher->set_response_code(200);
279
280 EXPECT_CALL(delegate_, OnFileInternal());
281 fetcher->delegate()->OnURLFetchComplete(fetcher);
282 }
283
209 } // namespace 284 } // namespace
210 285
211 } // namespace local_discovery 286 } // namespace local_discovery
OLDNEW
« no previous file with comments | « chrome/browser/local_discovery/privet_url_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698