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";
22 const char kAuthToken
[] = "oogabooga";
26 using google::protobuf::MessageLite
;
28 namespace copresence
{
30 class HttpPostTest
: public testing::Test
{
33 : received_response_code_(0) {
34 context_getter_
= new net::TestURLRequestContextGetter(
35 make_scoped_refptr(new base::TestSimpleTaskRunner
));
36 proto_
.set_client("test_client");
37 proto_
.set_version_code(123);
39 ~HttpPostTest() override
{}
41 // Record the response sent back to the client for verification.
42 void TestResponseCallback(int response_code
,
43 const std::string
& response
) {
44 received_response_code_
= response_code
;
45 received_response_
= response
;
49 bool ResponsePassedThrough(int response_code
, const std::string
& response
) {
50 scoped_ptr
<HttpPost
> post(
51 new HttpPost(context_getter_
.get(),
52 std::string("http://") + kFakeServerHost
,
58 post
->Start(base::Bind(&HttpPostTest::TestResponseCallback
,
59 base::Unretained(this)));
60 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(
61 HttpPost::kUrlFetcherId
);
62 fetcher
->set_response_code(response_code
);
63 fetcher
->SetResponseString(response
);
64 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
65 return received_response_code_
== response_code
&&
66 received_response_
== response
&&
67 GetApiKeySent() == kApiKey
&&
68 GetAuthHeaderSent().empty();
71 net::TestURLFetcher
* GetFetcher() {
72 return fetcher_factory_
.GetFetcherByID(HttpPost::kUrlFetcherId
);
75 const std::string
GetApiKeySent() {
76 std::string api_key_sent
;
77 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(),
78 HttpPost::kApiKeyField
,
83 const std::string
GetAuthHeaderSent() {
84 net::HttpRequestHeaders headers
;
86 GetFetcher()->GetExtraRequestHeaders(&headers
);
87 return headers
.GetHeader("Authorization", &header
) ? header
: "";
90 const std::string
GetTracingTokenSent() {
91 std::string tracing_token_sent
;
92 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(),
93 HttpPost::kTracingField
,
95 return tracing_token_sent
;
98 net::TestURLFetcherFactory fetcher_factory_
;
99 scoped_refptr
<net::TestURLRequestContextGetter
> context_getter_
;
101 ClientVersion proto_
;
103 int received_response_code_
;
104 std::string received_response_
;
107 TEST_F(HttpPostTest
, OKResponse
) {
108 // "Send" the proto to the "server".
109 HttpPost
* post
= new HttpPost(context_getter_
.get(),
110 std::string("http://") + kFakeServerHost
,
116 post
->Start(base::Bind(&HttpPostTest::TestResponseCallback
,
117 base::Unretained(this)));
119 // Verify that the data was sent to the right place.
120 GURL requested_url
= GetFetcher()->GetOriginalURL();
121 EXPECT_EQ(kFakeServerHost
, requested_url
.host());
122 EXPECT_EQ(std::string("/") + kRPCName
, requested_url
.path());
125 EXPECT_EQ("", GetApiKeySent()); // No API key when using an auth token.
126 EXPECT_EQ(std::string("Bearer ") + kAuthToken
, GetAuthHeaderSent());
127 EXPECT_EQ(std::string("token:") + kTracingToken
, GetTracingTokenSent());
129 // Verify that the right data was sent.
130 std::string upload_data
;
131 ASSERT_TRUE(proto_
.SerializeToString(&upload_data
));
132 EXPECT_EQ(upload_data
, GetFetcher()->upload_data());
134 // Send a response and check that it's passed along correctly.
135 GetFetcher()->set_response_code(net::HTTP_OK
);
136 GetFetcher()->SetResponseString("Hello World!");
137 GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher());
138 EXPECT_EQ(net::HTTP_OK
, received_response_code_
);
139 EXPECT_EQ("Hello World!", received_response_
);
143 TEST_F(HttpPostTest
, ErrorResponse
) {
144 EXPECT_TRUE(ResponsePassedThrough(
145 net::HTTP_BAD_REQUEST
, "Bad client. Shame on you."));
146 EXPECT_TRUE(ResponsePassedThrough(
147 net::HTTP_INTERNAL_SERVER_ERROR
, "I'm dying. Forgive me."));
148 EXPECT_TRUE(ResponsePassedThrough(-1, ""));
151 } // namespace copresence