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

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

Issue 469883002: Using API key specified from js (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing review comments Created 6 years, 4 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
OLDNEW
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 15
15 namespace { 16 namespace {
16 17
17 const char kFakeServerHost[] = "test.server.google.com"; 18 const char kFakeServerHost[] = "test.server.google.com";
18 const char kRPCName[] = "testRpc"; 19 const char kRPCName[] = "testRpc";
19 const char kTracingToken[] = "trace me!"; 20 const char kTracingToken[] = "trace me!";
21 const char kApiKey[] = "unlock ALL the APIz";
20 22
21 } // namespace 23 } // namespace
22 24
23 using google::protobuf::MessageLite; 25 using google::protobuf::MessageLite;
24 26
25 namespace copresence { 27 namespace copresence {
26 28
27 class HttpPostTest : public testing::Test { 29 class HttpPostTest : public testing::Test {
28 public: 30 public:
29 HttpPostTest() 31 HttpPostTest()
30 : received_response_code_(0) { 32 : received_response_code_(0) {
31 context_getter_ = new net::TestURLRequestContextGetter( 33 context_getter_ = new net::TestURLRequestContextGetter(
32 make_scoped_refptr(new base::TestSimpleTaskRunner)); 34 make_scoped_refptr(new base::TestSimpleTaskRunner));
33 proto_.set_client("test_client"); 35 proto_.set_client("test_client");
34 proto_.set_version_code(123); 36 proto_.set_version_code(123);
35 } 37 }
36 virtual ~HttpPostTest() {} 38 virtual ~HttpPostTest() {}
37 39
38 // Record the response sent back to the client for verification. 40 // Record the response sent back to the client for verification.
39 void TestResponseCallback(int response_code, 41 void TestResponseCallback(int response_code,
40 const std::string& response) { 42 const std::string& response) {
41 received_response_code_ = response_code; 43 received_response_code_ = response_code;
42 received_response_ = response; 44 received_response_ = response;
43 } 45 }
44 46
45 protected: 47 protected:
46 bool ResponsePassedThrough(int response_code, const std::string& response) { 48 bool ResponsePassedThrough(int response_code, const std::string& response) {
47 pending_post_ = new HttpPost(context_getter_.get(), 49 pending_post_ = new HttpPost(context_getter_.get(),
48 std::string("http://") + kFakeServerHost, 50 std::string("http://") + kFakeServerHost,
49 kRPCName, 51 kRPCName);
50 "",
51 proto_);
52 pending_post_->Start(base::Bind(&HttpPostTest::TestResponseCallback, 52 pending_post_->Start(base::Bind(&HttpPostTest::TestResponseCallback,
53 base::Unretained(this))); 53 base::Unretained(this)),
54 proto_);
54 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( 55 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(
55 HttpPost::kUrlFetcherId); 56 HttpPost::kUrlFetcherId);
56 fetcher->set_response_code(response_code); 57 fetcher->set_response_code(response_code);
57 fetcher->SetResponseString(response); 58 fetcher->SetResponseString(response);
58 fetcher->delegate()->OnURLFetchComplete(fetcher); 59 fetcher->delegate()->OnURLFetchComplete(fetcher);
59 delete pending_post_; 60 delete pending_post_;
60 return received_response_code_ == response_code && 61 return received_response_code_ == response_code &&
61 received_response_ == response; 62 received_response_ == response;
62 } 63 }
63 64
65 net::TestURLFetcher* GetFetcher() {
66 return fetcher_factory_.GetFetcherByID(HttpPost::kUrlFetcherId);
67 }
68
69 const std::string GetApiKeySent() {
70 std::string api_key_sent;
71 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(),
72 HttpPost::kApiKeyField,
73 &api_key_sent);
74 return api_key_sent;
75 }
76
77 const std::string GetTracingTokenSent() {
78 std::string tracing_token_sent;
79 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(),
80 HttpPost::kTracingTokenField,
81 &tracing_token_sent);
82 return tracing_token_sent;
83 }
84
64 net::TestURLFetcherFactory fetcher_factory_; 85 net::TestURLFetcherFactory fetcher_factory_;
65 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; 86 scoped_refptr<net::TestURLRequestContextGetter> context_getter_;
66 87
67 ClientVersion proto_; 88 ClientVersion proto_;
68 89
69 int received_response_code_; 90 int received_response_code_;
70 std::string received_response_; 91 std::string received_response_;
71 92
72 private: 93 private:
73 HttpPost* pending_post_; 94 HttpPost* pending_post_;
74 }; 95 };
75 96
76 TEST_F(HttpPostTest, OKResponse) { 97 TEST_F(HttpPostTest, OKResponse) {
77 // "Send" the proto to the "server". 98 // "Send" the proto to the "server".
78 HttpPost* post = new HttpPost(context_getter_.get(), 99 HttpPost* post = new HttpPost(context_getter_.get(),
79 std::string("http://") + kFakeServerHost, 100 std::string("http://") + kFakeServerHost,
80 kRPCName, 101 kRPCName);
81 kTracingToken, 102 post->set_api_key(kApiKey);
82 proto_); 103 post->set_tracing_token(kTracingToken);
83 post->Start(base::Bind(&HttpPostTest::TestResponseCallback, 104 post->Start(base::Bind(&HttpPostTest::TestResponseCallback,
84 base::Unretained(this))); 105 base::Unretained(this)),
106 proto_);
85 107
86 // Verify that the right data got sent to the right place. 108 // Verify that the data was sent to the right place.
87 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( 109 GURL requested_url = GetFetcher()->GetOriginalURL();
88 HttpPost::kUrlFetcherId); 110 EXPECT_EQ(kFakeServerHost, requested_url.host());
89 EXPECT_EQ(kFakeServerHost, fetcher->GetOriginalURL().host()); 111 EXPECT_EQ(std::string("/") + kRPCName, requested_url.path());
90 EXPECT_EQ(std::string("/") + kRPCName, fetcher->GetOriginalURL().path()); 112
91 std::string tracing_token_sent; 113 // Check query parameters.
92 EXPECT_TRUE(net::GetValueForKeyInQuery(fetcher->GetOriginalURL(), 114 EXPECT_EQ(kApiKey, GetApiKeySent());
93 HttpPost::kTracingTokenField, 115 EXPECT_EQ(std::string("token:") + kTracingToken, GetTracingTokenSent());
94 &tracing_token_sent)); 116
95 EXPECT_EQ(std::string("token:") + kTracingToken, tracing_token_sent); 117 // Verify that the right data was sent.
96 std::string upload_data; 118 std::string upload_data;
97 ASSERT_TRUE(proto_.SerializeToString(&upload_data)); 119 ASSERT_TRUE(proto_.SerializeToString(&upload_data));
98 EXPECT_EQ(upload_data, fetcher->upload_data()); 120 EXPECT_EQ(upload_data, GetFetcher()->upload_data());
99 121
100 // Send a response and check that it's passed along correctly. 122 // Send a response and check that it's passed along correctly.
101 fetcher->set_response_code(net::HTTP_OK); 123 GetFetcher()->set_response_code(net::HTTP_OK);
102 fetcher->SetResponseString("Hello World!"); 124 GetFetcher()->SetResponseString("Hello World!");
103 fetcher->delegate()->OnURLFetchComplete(fetcher); 125 GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher());
104 EXPECT_EQ(net::HTTP_OK, received_response_code_); 126 EXPECT_EQ(net::HTTP_OK, received_response_code_);
105 EXPECT_EQ("Hello World!", received_response_); 127 EXPECT_EQ("Hello World!", received_response_);
106 delete post; 128 delete post;
107 } 129 }
108 130
109 TEST_F(HttpPostTest, ErrorResponse) { 131 TEST_F(HttpPostTest, ErrorResponse) {
110 EXPECT_TRUE(ResponsePassedThrough( 132 EXPECT_TRUE(ResponsePassedThrough(
111 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); 133 net::HTTP_BAD_REQUEST, "Bad client. Shame on you."));
112 EXPECT_TRUE(ResponsePassedThrough( 134 EXPECT_TRUE(ResponsePassedThrough(
113 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); 135 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me."));
114 EXPECT_TRUE(ResponsePassedThrough(-1, "")); 136 EXPECT_TRUE(ResponsePassedThrough(-1, ""));
115 } 137 }
116 138
117 } // namespace copresence 139 } // namespace copresence
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698