OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/copresence/rpc/http_post.h" | 5 #include "components/copresence/rpc/http_post.h" |
6 | 6 |
7 #include "base/test/test_simple_task_runner.h" | 7 #include "base/test/test_simple_task_runner.h" |
8 #include "components/copresence/proto/data.pb.h" | 8 #include "components/copresence/proto/data.pb.h" |
9 #include "net/base/url_util.h" | 9 #include "net/base/url_util.h" |
10 #include "net/http/http_status_code.h" | 10 #include "net/http/http_status_code.h" |
11 #include "net/url_request/test_url_fetcher_factory.h" | 11 #include "net/url_request/test_url_fetcher_factory.h" |
12 #include "net/url_request/url_request_test_util.h" | 12 #include "net/url_request/url_request_test_util.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const char kFakeServerHost[] = "test.server.google.com"; | 18 const char kFakeServerHost[] = "test.server.google.com"; |
19 const char kRPCName[] = "testRpc"; | 19 const char kRPCName[] = "testRpc"; |
20 const char kTracingToken[] = "trace me!"; | 20 const char kTracingToken[] = "trace me!"; |
21 const char kApiKey[] = "unlock ALL the APIz"; | 21 const char kApiKey[] = "unlock ALL the APIz"; |
| 22 const char kAuthToken[] = "oogabooga"; |
22 | 23 |
23 } // namespace | 24 } // namespace |
24 | 25 |
25 using google::protobuf::MessageLite; | 26 using google::protobuf::MessageLite; |
26 | 27 |
27 namespace copresence { | 28 namespace copresence { |
28 | 29 |
29 class HttpPostTest : public testing::Test { | 30 class HttpPostTest : public testing::Test { |
30 public: | 31 public: |
31 HttpPostTest() | 32 HttpPostTest() |
(...skipping 10 matching lines...) Expand all Loading... |
42 const std::string& response) { | 43 const std::string& response) { |
43 received_response_code_ = response_code; | 44 received_response_code_ = response_code; |
44 received_response_ = response; | 45 received_response_ = response; |
45 } | 46 } |
46 | 47 |
47 protected: | 48 protected: |
48 bool ResponsePassedThrough(int response_code, const std::string& response) { | 49 bool ResponsePassedThrough(int response_code, const std::string& response) { |
49 pending_post_ = new HttpPost(context_getter_.get(), | 50 pending_post_ = new HttpPost(context_getter_.get(), |
50 std::string("http://") + kFakeServerHost, | 51 std::string("http://") + kFakeServerHost, |
51 kRPCName, | 52 kRPCName, |
52 "", | |
53 kApiKey, | 53 kApiKey, |
| 54 "", // auth token |
| 55 "", // tracing token |
54 proto_); | 56 proto_); |
55 pending_post_->Start(base::Bind(&HttpPostTest::TestResponseCallback, | 57 pending_post_->Start(base::Bind(&HttpPostTest::TestResponseCallback, |
56 base::Unretained(this))); | 58 base::Unretained(this))); |
57 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( | 59 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( |
58 HttpPost::kUrlFetcherId); | 60 HttpPost::kUrlFetcherId); |
59 fetcher->set_response_code(response_code); | 61 fetcher->set_response_code(response_code); |
60 fetcher->SetResponseString(response); | 62 fetcher->SetResponseString(response); |
61 fetcher->delegate()->OnURLFetchComplete(fetcher); | 63 fetcher->delegate()->OnURLFetchComplete(fetcher); |
62 delete pending_post_; | 64 delete pending_post_; |
63 return received_response_code_ == response_code && | 65 return received_response_code_ == response_code && |
64 received_response_ == response; | 66 received_response_ == response && |
| 67 GetAuthHeaderSent().empty(); |
65 } | 68 } |
66 | 69 |
67 net::TestURLFetcher* GetFetcher() { | 70 net::TestURLFetcher* GetFetcher() { |
68 return fetcher_factory_.GetFetcherByID(HttpPost::kUrlFetcherId); | 71 return fetcher_factory_.GetFetcherByID(HttpPost::kUrlFetcherId); |
69 } | 72 } |
70 | 73 |
71 const std::string GetApiKeySent() { | 74 const std::string GetApiKeySent() { |
72 std::string api_key_sent; | 75 std::string api_key_sent; |
73 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), | 76 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), |
74 HttpPost::kApiKeyField, | 77 HttpPost::kApiKeyField, |
75 &api_key_sent); | 78 &api_key_sent); |
76 return api_key_sent; | 79 return api_key_sent; |
77 } | 80 } |
78 | 81 |
| 82 const std::string GetAuthHeaderSent() { |
| 83 net::HttpRequestHeaders headers; |
| 84 std::string header; |
| 85 GetFetcher()->GetExtraRequestHeaders(&headers); |
| 86 return headers.GetHeader("Authorization", &header) ? header : ""; |
| 87 } |
| 88 |
79 const std::string GetTracingTokenSent() { | 89 const std::string GetTracingTokenSent() { |
80 std::string tracing_token_sent; | 90 std::string tracing_token_sent; |
81 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), | 91 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), |
82 HttpPost::kTracingField, | 92 HttpPost::kTracingField, |
83 &tracing_token_sent); | 93 &tracing_token_sent); |
84 return tracing_token_sent; | 94 return tracing_token_sent; |
85 } | 95 } |
86 | 96 |
87 net::TestURLFetcherFactory fetcher_factory_; | 97 net::TestURLFetcherFactory fetcher_factory_; |
88 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; | 98 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; |
89 | 99 |
90 ClientVersion proto_; | 100 ClientVersion proto_; |
91 | 101 |
92 int received_response_code_; | 102 int received_response_code_; |
93 std::string received_response_; | 103 std::string received_response_; |
94 | 104 |
95 private: | 105 private: |
96 HttpPost* pending_post_; | 106 HttpPost* pending_post_; |
97 }; | 107 }; |
98 | 108 |
99 TEST_F(HttpPostTest, OKResponse) { | 109 TEST_F(HttpPostTest, OKResponse) { |
100 // "Send" the proto to the "server". | 110 // "Send" the proto to the "server". |
101 HttpPost* post = new HttpPost(context_getter_.get(), | 111 HttpPost* post = new HttpPost(context_getter_.get(), |
102 std::string("http://") + kFakeServerHost, | 112 std::string("http://") + kFakeServerHost, |
103 kRPCName, | 113 kRPCName, |
| 114 kApiKey, |
| 115 kAuthToken, |
104 kTracingToken, | 116 kTracingToken, |
105 kApiKey, | |
106 proto_); | 117 proto_); |
107 post->Start(base::Bind(&HttpPostTest::TestResponseCallback, | 118 post->Start(base::Bind(&HttpPostTest::TestResponseCallback, |
108 base::Unretained(this))); | 119 base::Unretained(this))); |
109 | 120 |
110 // Verify that the data was sent to the right place. | 121 // Verify that the data was sent to the right place. |
111 GURL requested_url = GetFetcher()->GetOriginalURL(); | 122 GURL requested_url = GetFetcher()->GetOriginalURL(); |
112 EXPECT_EQ(kFakeServerHost, requested_url.host()); | 123 EXPECT_EQ(kFakeServerHost, requested_url.host()); |
113 EXPECT_EQ(std::string("/") + kRPCName, requested_url.path()); | 124 EXPECT_EQ(std::string("/") + kRPCName, requested_url.path()); |
114 | 125 |
115 // Check query parameters. | 126 // Check parameters. |
116 EXPECT_EQ(kApiKey, GetApiKeySent()); | 127 EXPECT_EQ(kApiKey, GetApiKeySent()); |
| 128 EXPECT_EQ(std::string("Bearer ") + kAuthToken, GetAuthHeaderSent()); |
117 EXPECT_EQ(std::string("token:") + kTracingToken, GetTracingTokenSent()); | 129 EXPECT_EQ(std::string("token:") + kTracingToken, GetTracingTokenSent()); |
118 | 130 |
119 // Verify that the right data was sent. | 131 // Verify that the right data was sent. |
120 std::string upload_data; | 132 std::string upload_data; |
121 ASSERT_TRUE(proto_.SerializeToString(&upload_data)); | 133 ASSERT_TRUE(proto_.SerializeToString(&upload_data)); |
122 EXPECT_EQ(upload_data, GetFetcher()->upload_data()); | 134 EXPECT_EQ(upload_data, GetFetcher()->upload_data()); |
123 | 135 |
124 // Send a response and check that it's passed along correctly. | 136 // Send a response and check that it's passed along correctly. |
125 GetFetcher()->set_response_code(net::HTTP_OK); | 137 GetFetcher()->set_response_code(net::HTTP_OK); |
126 GetFetcher()->SetResponseString("Hello World!"); | 138 GetFetcher()->SetResponseString("Hello World!"); |
127 GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher()); | 139 GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher()); |
128 EXPECT_EQ(net::HTTP_OK, received_response_code_); | 140 EXPECT_EQ(net::HTTP_OK, received_response_code_); |
129 EXPECT_EQ("Hello World!", received_response_); | 141 EXPECT_EQ("Hello World!", received_response_); |
130 delete post; | 142 delete post; |
131 } | 143 } |
132 | 144 |
133 TEST_F(HttpPostTest, ErrorResponse) { | 145 TEST_F(HttpPostTest, ErrorResponse) { |
134 EXPECT_TRUE(ResponsePassedThrough( | 146 EXPECT_TRUE(ResponsePassedThrough( |
135 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); | 147 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); |
136 EXPECT_TRUE(ResponsePassedThrough( | 148 EXPECT_TRUE(ResponsePassedThrough( |
137 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); | 149 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); |
138 EXPECT_TRUE(ResponsePassedThrough(-1, "")); | 150 EXPECT_TRUE(ResponsePassedThrough(-1, "")); |
139 } | 151 } |
140 | 152 |
141 } // namespace copresence | 153 } // namespace copresence |
OLD | NEW |