| 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() |
| 32 : received_response_code_(0) { | 33 : received_response_code_(0) { |
| 33 context_getter_ = new net::TestURLRequestContextGetter( | 34 context_getter_ = new net::TestURLRequestContextGetter( |
| 34 make_scoped_refptr(new base::TestSimpleTaskRunner)); | 35 make_scoped_refptr(new base::TestSimpleTaskRunner)); |
| 35 proto_.set_client("test_client"); | 36 proto_.set_client("test_client"); |
| 36 proto_.set_version_code(123); | 37 proto_.set_version_code(123); |
| 37 } | 38 } |
| 38 virtual ~HttpPostTest() {} | 39 virtual ~HttpPostTest() {} |
| 39 | 40 |
| 40 // Record the response sent back to the client for verification. | 41 // Record the response sent back to the client for verification. |
| 41 void TestResponseCallback(int response_code, | 42 void TestResponseCallback(int response_code, |
| 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 scoped_ptr<HttpPost> post( |
| 50 std::string("http://") + kFakeServerHost, | 51 new HttpPost(context_getter_.get(), |
| 51 kRPCName, | 52 std::string("http://") + kFakeServerHost, |
| 52 "", | 53 kRPCName, |
| 53 kApiKey, | 54 kApiKey, |
| 54 proto_); | 55 "", // auth token |
| 55 pending_post_->Start(base::Bind(&HttpPostTest::TestResponseCallback, | 56 "", // tracing token |
| 56 base::Unretained(this))); | 57 proto_)); |
| 58 post->Start(base::Bind(&HttpPostTest::TestResponseCallback, |
| 59 base::Unretained(this))); |
| 57 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( | 60 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( |
| 58 HttpPost::kUrlFetcherId); | 61 HttpPost::kUrlFetcherId); |
| 59 fetcher->set_response_code(response_code); | 62 fetcher->set_response_code(response_code); |
| 60 fetcher->SetResponseString(response); | 63 fetcher->SetResponseString(response); |
| 61 fetcher->delegate()->OnURLFetchComplete(fetcher); | 64 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 62 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 GetApiKeySent() == kApiKey && |
| 68 GetAuthHeaderSent().empty(); |
| 65 } | 69 } |
| 66 | 70 |
| 67 net::TestURLFetcher* GetFetcher() { | 71 net::TestURLFetcher* GetFetcher() { |
| 68 return fetcher_factory_.GetFetcherByID(HttpPost::kUrlFetcherId); | 72 return fetcher_factory_.GetFetcherByID(HttpPost::kUrlFetcherId); |
| 69 } | 73 } |
| 70 | 74 |
| 71 const std::string GetApiKeySent() { | 75 const std::string GetApiKeySent() { |
| 72 std::string api_key_sent; | 76 std::string api_key_sent; |
| 73 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), | 77 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), |
| 74 HttpPost::kApiKeyField, | 78 HttpPost::kApiKeyField, |
| 75 &api_key_sent); | 79 &api_key_sent); |
| 76 return api_key_sent; | 80 return api_key_sent; |
| 77 } | 81 } |
| 78 | 82 |
| 83 const std::string GetAuthHeaderSent() { |
| 84 net::HttpRequestHeaders headers; |
| 85 std::string header; |
| 86 GetFetcher()->GetExtraRequestHeaders(&headers); |
| 87 return headers.GetHeader("Authorization", &header) ? header : ""; |
| 88 } |
| 89 |
| 79 const std::string GetTracingTokenSent() { | 90 const std::string GetTracingTokenSent() { |
| 80 std::string tracing_token_sent; | 91 std::string tracing_token_sent; |
| 81 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), | 92 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), |
| 82 HttpPost::kTracingField, | 93 HttpPost::kTracingField, |
| 83 &tracing_token_sent); | 94 &tracing_token_sent); |
| 84 return tracing_token_sent; | 95 return tracing_token_sent; |
| 85 } | 96 } |
| 86 | 97 |
| 87 net::TestURLFetcherFactory fetcher_factory_; | 98 net::TestURLFetcherFactory fetcher_factory_; |
| 88 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; | 99 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; |
| 89 | 100 |
| 90 ClientVersion proto_; | 101 ClientVersion proto_; |
| 91 | 102 |
| 92 int received_response_code_; | 103 int received_response_code_; |
| 93 std::string received_response_; | 104 std::string received_response_; |
| 94 | |
| 95 private: | |
| 96 HttpPost* pending_post_; | |
| 97 }; | 105 }; |
| 98 | 106 |
| 99 TEST_F(HttpPostTest, OKResponse) { | 107 TEST_F(HttpPostTest, OKResponse) { |
| 100 // "Send" the proto to the "server". | 108 // "Send" the proto to the "server". |
| 101 HttpPost* post = new HttpPost(context_getter_.get(), | 109 HttpPost* post = new HttpPost(context_getter_.get(), |
| 102 std::string("http://") + kFakeServerHost, | 110 std::string("http://") + kFakeServerHost, |
| 103 kRPCName, | 111 kRPCName, |
| 112 kApiKey, |
| 113 kAuthToken, |
| 104 kTracingToken, | 114 kTracingToken, |
| 105 kApiKey, | |
| 106 proto_); | 115 proto_); |
| 107 post->Start(base::Bind(&HttpPostTest::TestResponseCallback, | 116 post->Start(base::Bind(&HttpPostTest::TestResponseCallback, |
| 108 base::Unretained(this))); | 117 base::Unretained(this))); |
| 109 | 118 |
| 110 // Verify that the data was sent to the right place. | 119 // Verify that the data was sent to the right place. |
| 111 GURL requested_url = GetFetcher()->GetOriginalURL(); | 120 GURL requested_url = GetFetcher()->GetOriginalURL(); |
| 112 EXPECT_EQ(kFakeServerHost, requested_url.host()); | 121 EXPECT_EQ(kFakeServerHost, requested_url.host()); |
| 113 EXPECT_EQ(std::string("/") + kRPCName, requested_url.path()); | 122 EXPECT_EQ(std::string("/") + kRPCName, requested_url.path()); |
| 114 | 123 |
| 115 // Check query parameters. | 124 // Check parameters. |
| 116 EXPECT_EQ(kApiKey, GetApiKeySent()); | 125 EXPECT_EQ("", GetApiKeySent()); // No API key when using an auth token. |
| 126 EXPECT_EQ(std::string("Bearer ") + kAuthToken, GetAuthHeaderSent()); |
| 117 EXPECT_EQ(std::string("token:") + kTracingToken, GetTracingTokenSent()); | 127 EXPECT_EQ(std::string("token:") + kTracingToken, GetTracingTokenSent()); |
| 118 | 128 |
| 119 // Verify that the right data was sent. | 129 // Verify that the right data was sent. |
| 120 std::string upload_data; | 130 std::string upload_data; |
| 121 ASSERT_TRUE(proto_.SerializeToString(&upload_data)); | 131 ASSERT_TRUE(proto_.SerializeToString(&upload_data)); |
| 122 EXPECT_EQ(upload_data, GetFetcher()->upload_data()); | 132 EXPECT_EQ(upload_data, GetFetcher()->upload_data()); |
| 123 | 133 |
| 124 // Send a response and check that it's passed along correctly. | 134 // Send a response and check that it's passed along correctly. |
| 125 GetFetcher()->set_response_code(net::HTTP_OK); | 135 GetFetcher()->set_response_code(net::HTTP_OK); |
| 126 GetFetcher()->SetResponseString("Hello World!"); | 136 GetFetcher()->SetResponseString("Hello World!"); |
| 127 GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher()); | 137 GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher()); |
| 128 EXPECT_EQ(net::HTTP_OK, received_response_code_); | 138 EXPECT_EQ(net::HTTP_OK, received_response_code_); |
| 129 EXPECT_EQ("Hello World!", received_response_); | 139 EXPECT_EQ("Hello World!", received_response_); |
| 130 delete post; | 140 delete post; |
| 131 } | 141 } |
| 132 | 142 |
| 133 TEST_F(HttpPostTest, ErrorResponse) { | 143 TEST_F(HttpPostTest, ErrorResponse) { |
| 134 EXPECT_TRUE(ResponsePassedThrough( | 144 EXPECT_TRUE(ResponsePassedThrough( |
| 135 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); | 145 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); |
| 136 EXPECT_TRUE(ResponsePassedThrough( | 146 EXPECT_TRUE(ResponsePassedThrough( |
| 137 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); | 147 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); |
| 138 EXPECT_TRUE(ResponsePassedThrough(-1, "")); | 148 EXPECT_TRUE(ResponsePassedThrough(-1, "")); |
| 139 } | 149 } |
| 140 | 150 |
| 141 } // namespace copresence | 151 } // namespace copresence |
| OLD | NEW |