OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "google_apis/drive/base_requests.h" | 5 #include "google_apis/drive/base_requests.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 callback_.Run(GetErrorCode()); | 44 callback_.Run(GetErrorCode()); |
45 } | 45 } |
46 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE { | 46 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code) OVERRIDE { |
47 callback_.Run(code); | 47 callback_.Run(code); |
48 } | 48 } |
49 | 49 |
50 EntryActionCallback callback_; | 50 EntryActionCallback callback_; |
51 GURL url_; | 51 GURL url_; |
52 }; | 52 }; |
53 | 53 |
| 54 class FakeGetDataRequest : public GetDataRequest { |
| 55 public: |
| 56 explicit FakeGetDataRequest(RequestSender* sender, |
| 57 const GetDataCallback& callback, |
| 58 const GURL& url) |
| 59 : GetDataRequest(sender, callback), |
| 60 url_(url) { |
| 61 } |
| 62 |
| 63 virtual ~FakeGetDataRequest() { |
| 64 } |
| 65 |
| 66 protected: |
| 67 virtual GURL GetURL() const OVERRIDE { return url_; } |
| 68 |
| 69 GURL url_; |
| 70 }; |
| 71 |
54 } // namespace | 72 } // namespace |
55 | 73 |
56 class BaseRequestsTest : public testing::Test { | 74 class BaseRequestsTest : public testing::Test { |
57 public: | 75 public: |
58 BaseRequestsTest() : response_code_(net::HTTP_OK) {} | 76 BaseRequestsTest() : response_code_(net::HTTP_OK) {} |
59 | 77 |
60 virtual void SetUp() OVERRIDE { | 78 virtual void SetUp() OVERRIDE { |
61 request_context_getter_ = new net::TestURLRequestContextGetter( | 79 request_context_getter_ = new net::TestURLRequestContextGetter( |
62 message_loop_.message_loop_proxy()); | 80 message_loop_.message_loop_proxy()); |
63 | 81 |
(...skipping 20 matching lines...) Expand all Loading... |
84 base::MessageLoopForIO message_loop_; | 102 base::MessageLoopForIO message_loop_; |
85 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 103 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
86 scoped_ptr<RequestSender> sender_; | 104 scoped_ptr<RequestSender> sender_; |
87 net::test_server::EmbeddedTestServer test_server_; | 105 net::test_server::EmbeddedTestServer test_server_; |
88 | 106 |
89 net::HttpStatusCode response_code_; | 107 net::HttpStatusCode response_code_; |
90 std::string response_body_; | 108 std::string response_body_; |
91 }; | 109 }; |
92 | 110 |
93 TEST_F(BaseRequestsTest, ParseValidJson) { | 111 TEST_F(BaseRequestsTest, ParseValidJson) { |
94 scoped_ptr<base::Value> json(ParseJson(kValidJsonString)); | 112 scoped_ptr<base::Value> json; |
| 113 ParseJson(message_loop_.message_loop_proxy(), |
| 114 kValidJsonString, |
| 115 base::Bind(test_util::CreateCopyResultCallback(&json))); |
| 116 base::RunLoop().RunUntilIdle(); |
95 | 117 |
96 base::DictionaryValue* root_dict = NULL; | 118 base::DictionaryValue* root_dict = NULL; |
97 ASSERT_TRUE(json); | 119 ASSERT_TRUE(json); |
98 ASSERT_TRUE(json->GetAsDictionary(&root_dict)); | 120 ASSERT_TRUE(json->GetAsDictionary(&root_dict)); |
99 | 121 |
100 int int_value = 0; | 122 int int_value = 0; |
101 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); | 123 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); |
102 EXPECT_EQ(123, int_value); | 124 EXPECT_EQ(123, int_value); |
103 } | 125 } |
104 | 126 |
105 TEST_F(BaseRequestsTest, ParseInvalidJson) { | 127 TEST_F(BaseRequestsTest, ParseInvalidJson) { |
106 EXPECT_FALSE(ParseJson(kInvalidJsonString)); | 128 // Initialize with a valid pointer to verify that null is indeed assigned. |
| 129 scoped_ptr<base::Value> json(base::Value::CreateNullValue()); |
| 130 ParseJson(message_loop_.message_loop_proxy(), |
| 131 kInvalidJsonString, |
| 132 base::Bind(test_util::CreateCopyResultCallback(&json))); |
| 133 base::RunLoop().RunUntilIdle(); |
| 134 |
| 135 EXPECT_FALSE(json); |
107 } | 136 } |
108 | 137 |
109 TEST_F(BaseRequestsTest, UrlFetchRequestBaseResponseCodeOverride) { | 138 TEST_F(BaseRequestsTest, UrlFetchRequestBaseResponseCodeOverride) { |
110 response_code_ = net::HTTP_FORBIDDEN; | 139 response_code_ = net::HTTP_FORBIDDEN; |
111 response_body_ = | 140 response_body_ = |
112 "{\"error\": {\n" | 141 "{\"error\": {\n" |
113 " \"errors\": [\n" | 142 " \"errors\": [\n" |
114 " {\n" | 143 " {\n" |
115 " \"domain\": \"usageLimits\",\n" | 144 " \"domain\": \"usageLimits\",\n" |
116 " \"reason\": \"rateLimitExceeded\",\n" | 145 " \"reason\": \"rateLimitExceeded\",\n" |
(...skipping 12 matching lines...) Expand all Loading... |
129 sender_.get(), | 158 sender_.get(), |
130 test_util::CreateQuitCallback( | 159 test_util::CreateQuitCallback( |
131 &run_loop, test_util::CreateCopyResultCallback(&error)), | 160 &run_loop, test_util::CreateCopyResultCallback(&error)), |
132 test_server_.base_url())); | 161 test_server_.base_url())); |
133 run_loop.Run(); | 162 run_loop.Run(); |
134 | 163 |
135 // HTTP_FORBIDDEN (403) is overridden by the error reason. | 164 // HTTP_FORBIDDEN (403) is overridden by the error reason. |
136 EXPECT_EQ(HTTP_SERVICE_UNAVAILABLE, error); | 165 EXPECT_EQ(HTTP_SERVICE_UNAVAILABLE, error); |
137 } | 166 } |
138 | 167 |
| 168 TEST_F(BaseRequestsTest, GetDataRequestParseValidResponse) { |
| 169 response_body_ = kValidJsonString; |
| 170 |
| 171 GDataErrorCode error = GDATA_OTHER_ERROR; |
| 172 scoped_ptr<base::Value> value; |
| 173 base::RunLoop run_loop; |
| 174 sender_->StartRequestWithRetry( |
| 175 new FakeGetDataRequest( |
| 176 sender_.get(), |
| 177 test_util::CreateQuitCallback( |
| 178 &run_loop, test_util::CreateCopyResultCallback(&error, &value)), |
| 179 test_server_.base_url())); |
| 180 run_loop.Run(); |
| 181 |
| 182 EXPECT_EQ(HTTP_SUCCESS, error); |
| 183 EXPECT_TRUE(value); |
| 184 } |
| 185 |
| 186 TEST_F(BaseRequestsTest, GetDataRequestParseInvalidResponse) { |
| 187 response_body_ = kInvalidJsonString; |
| 188 |
| 189 GDataErrorCode error = GDATA_OTHER_ERROR; |
| 190 scoped_ptr<base::Value> value; |
| 191 base::RunLoop run_loop; |
| 192 sender_->StartRequestWithRetry( |
| 193 new FakeGetDataRequest( |
| 194 sender_.get(), |
| 195 test_util::CreateQuitCallback( |
| 196 &run_loop, test_util::CreateCopyResultCallback(&error, &value)), |
| 197 test_server_.base_url())); |
| 198 run_loop.Run(); |
| 199 |
| 200 EXPECT_EQ(GDATA_PARSE_ERROR, error); |
| 201 EXPECT_FALSE(value); |
| 202 } |
| 203 |
139 } // namespace google_apis | 204 } // namespace google_apis |
OLD | NEW |