Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: components/copresence/rpc/http_post_unittest.cc

Issue 2130803002: Deleting the copresence API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/copresence/rpc/http_post.cc ('k') | components/copresence/rpc/rpc_handler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "components/copresence/rpc/http_post.h"
6
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"
14 #include "url/gurl.h"
15
16 namespace {
17
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";
23
24 } // namespace
25
26 using google::protobuf::MessageLite;
27
28 namespace copresence {
29
30 class HttpPostTest : public testing::Test {
31 public:
32 HttpPostTest() {
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);
37 }
38 ~HttpPostTest() override {}
39
40 // Check that the correct response was sent.
41 void TestResponseCallback(int expected_response_code,
42 const std::string& expected_response,
43 int actual_response_code,
44 const std::string& actual_response) {
45 CHECK_EQ(expected_response_code, actual_response_code);
46 CHECK_EQ(expected_response, actual_response);
47 }
48
49 protected:
50 void CheckPassthrough(int response_code, const std::string& response) {
51 HttpPost* post = new HttpPost(
52 context_getter_.get(), std::string("http://") + kFakeServerHost,
53 kRPCName, kApiKey,
54 "", // auth token
55 "", // tracing token
56 proto_);
57 post->Start(base::Bind(&HttpPostTest::TestResponseCallback,
58 base::Unretained(this),
59 response_code,
60 response));
61
62 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(
63 HttpPost::kUrlFetcherId);
64 fetcher->set_response_code(response_code);
65 fetcher->SetResponseString(response);
66 fetcher->delegate()->OnURLFetchComplete(fetcher);
67 }
68
69 net::TestURLFetcher* GetFetcher() {
70 return fetcher_factory_.GetFetcherByID(HttpPost::kUrlFetcherId);
71 }
72
73 const std::string GetApiKeySent() {
74 std::string api_key_sent;
75 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(),
76 HttpPost::kApiKeyField,
77 &api_key_sent);
78 return api_key_sent;
79 }
80
81 const std::string GetAuthHeaderSent() {
82 net::HttpRequestHeaders headers;
83 std::string header;
84 GetFetcher()->GetExtraRequestHeaders(&headers);
85 return headers.GetHeader("Authorization", &header) ? header : "";
86 }
87
88 const std::string GetTracingTokenSent() {
89 std::string tracing_token_sent;
90 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(),
91 HttpPost::kTracingField,
92 &tracing_token_sent);
93 return tracing_token_sent;
94 }
95
96 net::TestURLFetcherFactory fetcher_factory_;
97 scoped_refptr<net::TestURLRequestContextGetter> context_getter_;
98
99 ClientVersion proto_;
100 };
101
102 TEST_F(HttpPostTest, OKResponse) {
103 // "Send" the proto to the "server".
104 HttpPost* post = new HttpPost(context_getter_.get(),
105 std::string("http://") + kFakeServerHost,
106 kRPCName,
107 kApiKey,
108 kAuthToken,
109 kTracingToken,
110 proto_);
111 post->Start(base::Bind(&HttpPostTest::TestResponseCallback,
112 base::Unretained(this),
113 net::HTTP_OK,
114 "Hello World!"));
115
116 // Verify that the data was sent to the right place.
117 GURL requested_url = GetFetcher()->GetOriginalURL();
118 EXPECT_EQ(kFakeServerHost, requested_url.host());
119 EXPECT_EQ(std::string("/") + kRPCName, requested_url.path());
120
121 // Check parameters.
122 EXPECT_EQ("", GetApiKeySent()); // No API key when using an auth token.
123 EXPECT_EQ(std::string("Bearer ") + kAuthToken, GetAuthHeaderSent());
124 EXPECT_EQ(std::string("token:") + kTracingToken, GetTracingTokenSent());
125
126 // Verify that the right data was sent.
127 std::string upload_data;
128 ASSERT_TRUE(proto_.SerializeToString(&upload_data));
129 EXPECT_EQ(upload_data, GetFetcher()->upload_data());
130
131 // Send a response and check that it's passed along correctly.
132 GetFetcher()->set_response_code(net::HTTP_OK);
133 GetFetcher()->SetResponseString("Hello World!");
134 GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher());
135 }
136
137 TEST_F(HttpPostTest, ErrorResponse) {
138 CheckPassthrough(net::HTTP_BAD_REQUEST, "Bad client. Shame on you.");
139 CheckPassthrough(net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.");
140 CheckPassthrough(-1, "");
141 }
142
143 } // namespace copresence
OLDNEW
« no previous file with comments | « components/copresence/rpc/http_post.cc ('k') | components/copresence/rpc/rpc_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698