| 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 | |
| 72 } // namespace | 54 } // namespace |
| 73 | 55 |
| 74 class BaseRequestsTest : public testing::Test { | 56 class BaseRequestsTest : public testing::Test { |
| 75 public: | 57 public: |
| 76 BaseRequestsTest() : response_code_(net::HTTP_OK) {} | 58 BaseRequestsTest() : response_code_(net::HTTP_OK) {} |
| 77 | 59 |
| 78 virtual void SetUp() OVERRIDE { | 60 virtual void SetUp() OVERRIDE { |
| 79 request_context_getter_ = new net::TestURLRequestContextGetter( | 61 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 80 message_loop_.message_loop_proxy()); | 62 message_loop_.message_loop_proxy()); |
| 81 | 63 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 102 base::MessageLoopForIO message_loop_; | 84 base::MessageLoopForIO message_loop_; |
| 103 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 85 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 104 scoped_ptr<RequestSender> sender_; | 86 scoped_ptr<RequestSender> sender_; |
| 105 net::test_server::EmbeddedTestServer test_server_; | 87 net::test_server::EmbeddedTestServer test_server_; |
| 106 | 88 |
| 107 net::HttpStatusCode response_code_; | 89 net::HttpStatusCode response_code_; |
| 108 std::string response_body_; | 90 std::string response_body_; |
| 109 }; | 91 }; |
| 110 | 92 |
| 111 TEST_F(BaseRequestsTest, ParseValidJson) { | 93 TEST_F(BaseRequestsTest, ParseValidJson) { |
| 112 scoped_ptr<base::Value> json; | 94 scoped_ptr<base::Value> json(ParseJson(kValidJsonString)); |
| 113 ParseJson(message_loop_.message_loop_proxy(), | |
| 114 kValidJsonString, | |
| 115 base::Bind(test_util::CreateCopyResultCallback(&json))); | |
| 116 base::RunLoop().RunUntilIdle(); | |
| 117 | 95 |
| 118 base::DictionaryValue* root_dict = NULL; | 96 base::DictionaryValue* root_dict = NULL; |
| 119 ASSERT_TRUE(json); | 97 ASSERT_TRUE(json); |
| 120 ASSERT_TRUE(json->GetAsDictionary(&root_dict)); | 98 ASSERT_TRUE(json->GetAsDictionary(&root_dict)); |
| 121 | 99 |
| 122 int int_value = 0; | 100 int int_value = 0; |
| 123 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); | 101 ASSERT_TRUE(root_dict->GetInteger("test", &int_value)); |
| 124 EXPECT_EQ(123, int_value); | 102 EXPECT_EQ(123, int_value); |
| 125 } | 103 } |
| 126 | 104 |
| 127 TEST_F(BaseRequestsTest, ParseInvalidJson) { | 105 TEST_F(BaseRequestsTest, ParseInvalidJson) { |
| 128 // Initialize with a valid pointer to verify that null is indeed assigned. | 106 EXPECT_FALSE(ParseJson(kInvalidJsonString)); |
| 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); | |
| 136 } | 107 } |
| 137 | 108 |
| 138 TEST_F(BaseRequestsTest, UrlFetchRequestBaseResponseCodeOverride) { | 109 TEST_F(BaseRequestsTest, UrlFetchRequestBaseResponseCodeOverride) { |
| 139 response_code_ = net::HTTP_FORBIDDEN; | 110 response_code_ = net::HTTP_FORBIDDEN; |
| 140 response_body_ = | 111 response_body_ = |
| 141 "{\"error\": {\n" | 112 "{\"error\": {\n" |
| 142 " \"errors\": [\n" | 113 " \"errors\": [\n" |
| 143 " {\n" | 114 " {\n" |
| 144 " \"domain\": \"usageLimits\",\n" | 115 " \"domain\": \"usageLimits\",\n" |
| 145 " \"reason\": \"rateLimitExceeded\",\n" | 116 " \"reason\": \"rateLimitExceeded\",\n" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 158 sender_.get(), | 129 sender_.get(), |
| 159 test_util::CreateQuitCallback( | 130 test_util::CreateQuitCallback( |
| 160 &run_loop, test_util::CreateCopyResultCallback(&error)), | 131 &run_loop, test_util::CreateCopyResultCallback(&error)), |
| 161 test_server_.base_url())); | 132 test_server_.base_url())); |
| 162 run_loop.Run(); | 133 run_loop.Run(); |
| 163 | 134 |
| 164 // HTTP_FORBIDDEN (403) is overridden by the error reason. | 135 // HTTP_FORBIDDEN (403) is overridden by the error reason. |
| 165 EXPECT_EQ(HTTP_SERVICE_UNAVAILABLE, error); | 136 EXPECT_EQ(HTTP_SERVICE_UNAVAILABLE, error); |
| 166 } | 137 } |
| 167 | 138 |
| 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 | |
| 204 } // namespace google_apis | 139 } // namespace google_apis |
| OLD | NEW |