1 // Copyright 2014 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 "components/copresence/rpc/http_post.h"
7 #include "base/test/test_simple_task_runner.h"
8 #include "components/copresence/proto/data.pb.h"
9 #include "net/base/url_util.h"
10 #include "net/http/http_status_code.h"
11 #include "net/url_request/test_url_fetcher_factory.h"
12 #include "net/url_request/url_request_test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 const char kFakeServerHost
[] = "test.server.google.com";
19 const char kRPCName
[] = "testRpc";
20 const char kTracingToken
[] = "trace me!";
21 const char kApiKey
[] = "unlock ALL the APIz";
25 using google::protobuf::MessageLite
;
27 namespace copresence
{
29 class HttpPostTest
: public testing::Test
{
32 : received_response_code_(0) {
33 context_getter_
= new net::TestURLRequestContextGetter(
34 make_scoped_refptr(new base::TestSimpleTaskRunner
));
35 proto_
.set_client("test_client");
36 proto_
.set_version_code(123);
38 virtual ~HttpPostTest() {}
40 // Record the response sent back to the client for verification.
41 void TestResponseCallback(int response_code
,
42 const std::string
& response
) {
43 received_response_code_
= response_code
;
44 received_response_
= response
;
48 bool ResponsePassedThrough(int response_code
, const std::string
& response
) {
49 pending_post_
= new HttpPost(context_getter_
.get(),
50 std::string("http://") + kFakeServerHost
,
55 pending_post_
->Start(base::Bind(&HttpPostTest::TestResponseCallback
,
56 base::Unretained(this)));
57 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(
58 HttpPost::kUrlFetcherId
);
59 fetcher
->set_response_code(response_code
);
60 fetcher
->SetResponseString(response
);
61 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
63 return received_response_code_
== response_code
&&
64 received_response_
== response
;
67 net::TestURLFetcher
* GetFetcher() {
68 return fetcher_factory_
.GetFetcherByID(HttpPost::kUrlFetcherId
);
71 const std::string
GetApiKeySent() {
72 std::string api_key_sent
;
73 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(),
74 HttpPost::kApiKeyField
,
79 const std::string
GetTracingTokenSent() {
80 std::string tracing_token_sent
;
81 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(),
82 HttpPost::kTracingField
,
84 return tracing_token_sent
;
87 net::TestURLFetcherFactory fetcher_factory_
;
88 scoped_refptr
<net::TestURLRequestContextGetter
> context_getter_
;
92 int received_response_code_
;
93 std::string received_response_
;
96 HttpPost
* pending_post_
;
99 TEST_F(HttpPostTest
, OKResponse
) {
100 // "Send" the proto to the "server".
101 HttpPost
* post
= new HttpPost(context_getter_
.get(),
102 std::string("http://") + kFakeServerHost
,
107 post
->Start(base::Bind(&HttpPostTest::TestResponseCallback
,
108 base::Unretained(this)));
110 // Verify that the data was sent to the right place.
111 GURL requested_url
= GetFetcher()->GetOriginalURL();
112 EXPECT_EQ(kFakeServerHost
, requested_url
.host());
113 EXPECT_EQ(std::string("/") + kRPCName
, requested_url
.path());
115 // Check query parameters.
116 EXPECT_EQ(kApiKey
, GetApiKeySent());
117 EXPECT_EQ(std::string("token:") + kTracingToken
, GetTracingTokenSent());
119 // Verify that the right data was sent.
120 std::string upload_data
;
121 ASSERT_TRUE(proto_
.SerializeToString(&upload_data
));
122 EXPECT_EQ(upload_data
, GetFetcher()->upload_data());
124 // Send a response and check that it's passed along correctly.
125 GetFetcher()->set_response_code(net::HTTP_OK
);
126 GetFetcher()->SetResponseString("Hello World!");
127 GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher());
128 EXPECT_EQ(net::HTTP_OK
, received_response_code_
);
129 EXPECT_EQ("Hello World!", received_response_
);
133 TEST_F(HttpPostTest
, ErrorResponse
) {
134 EXPECT_TRUE(ResponsePassedThrough(
135 net::HTTP_BAD_REQUEST
, "Bad client. Shame on you."));
136 EXPECT_TRUE(ResponsePassedThrough(
137 net::HTTP_INTERNAL_SERVER_ERROR
, "I'm dying. Forgive me."));
138 EXPECT_TRUE(ResponsePassedThrough(-1, ""));
141 } // namespace copresence