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
3 // found in the LICENSE file.
5 #include "google_apis/drive/base_requests.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/values.h"
12 #include "google_apis/drive/dummy_auth_service.h"
13 #include "google_apis/drive/request_sender.h"
14 #include "google_apis/drive/test_util.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h"
16 #include "net/test/embedded_test_server/http_request.h"
17 #include "net/test/embedded_test_server/http_response.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace google_apis
{
25 const char kValidJsonString
[] = "{ \"test\": 123 }";
26 const char kInvalidJsonString
[] = "$$$";
28 class FakeUrlFetchRequest
: public UrlFetchRequestBase
{
30 FakeUrlFetchRequest(RequestSender
* sender
,
31 const EntryActionCallback
& callback
,
33 : UrlFetchRequestBase(sender
),
38 virtual ~FakeUrlFetchRequest() {
42 virtual GURL
GetURL() const OVERRIDE
{ return url_
; }
43 virtual void ProcessURLFetchResults(const net::URLFetcher
* source
) OVERRIDE
{
44 callback_
.Run(GetErrorCode());
46 virtual void RunCallbackOnPrematureFailure(GDataErrorCode code
) OVERRIDE
{
50 EntryActionCallback callback_
;
56 class BaseRequestsTest
: public testing::Test
{
58 BaseRequestsTest() : response_code_(net::HTTP_OK
) {}
60 virtual void SetUp() OVERRIDE
{
61 request_context_getter_
= new net::TestURLRequestContextGetter(
62 message_loop_
.message_loop_proxy());
64 sender_
.reset(new RequestSender(new DummyAuthService
,
65 request_context_getter_
.get(),
66 message_loop_
.message_loop_proxy(),
67 std::string() /* custom user agent */));
69 ASSERT_TRUE(test_server_
.InitializeAndWaitUntilReady());
70 test_server_
.RegisterRequestHandler(
71 base::Bind(&BaseRequestsTest::HandleRequest
, base::Unretained(this)));
74 scoped_ptr
<net::test_server::HttpResponse
> HandleRequest(
75 const net::test_server::HttpRequest
& request
) {
76 scoped_ptr
<net::test_server::BasicHttpResponse
> response(
77 new net::test_server::BasicHttpResponse
);
78 response
->set_code(response_code_
);
79 response
->set_content(response_body_
);
80 response
->set_content_type("application/json");
81 return response
.PassAs
<net::test_server::HttpResponse
>();
84 base::MessageLoopForIO message_loop_
;
85 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
86 scoped_ptr
<RequestSender
> sender_
;
87 net::test_server::EmbeddedTestServer test_server_
;
89 net::HttpStatusCode response_code_
;
90 std::string response_body_
;
93 TEST_F(BaseRequestsTest
, ParseValidJson
) {
94 scoped_ptr
<base::Value
> json(ParseJson(kValidJsonString
));
96 base::DictionaryValue
* root_dict
= NULL
;
98 ASSERT_TRUE(json
->GetAsDictionary(&root_dict
));
101 ASSERT_TRUE(root_dict
->GetInteger("test", &int_value
));
102 EXPECT_EQ(123, int_value
);
105 TEST_F(BaseRequestsTest
, ParseInvalidJson
) {
106 EXPECT_FALSE(ParseJson(kInvalidJsonString
));
109 TEST_F(BaseRequestsTest
, UrlFetchRequestBaseResponseCodeOverride
) {
110 response_code_
= net::HTTP_FORBIDDEN
;
115 " \"domain\": \"usageLimits\",\n"
116 " \"reason\": \"rateLimitExceeded\",\n"
117 " \"message\": \"Rate Limit Exceeded\"\n"
121 " \"message\": \"Rate Limit Exceeded\"\n"
125 GDataErrorCode error
= GDATA_OTHER_ERROR
;
126 base::RunLoop run_loop
;
127 sender_
->StartRequestWithRetry(
128 new FakeUrlFetchRequest(
130 test_util::CreateQuitCallback(
131 &run_loop
, test_util::CreateCopyResultCallback(&error
)),
132 test_server_
.base_url()));
135 // HTTP_FORBIDDEN (403) is overridden by the error reason.
136 EXPECT_EQ(HTTP_SERVICE_UNAVAILABLE
, error
);
139 } // namespace google_apis