| 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 | 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() |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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 "", | 52 "", |
| 53 kApiKey, |
| 51 proto_); | 54 proto_); |
| 52 pending_post_->Start(base::Bind(&HttpPostTest::TestResponseCallback, | 55 pending_post_->Start(base::Bind(&HttpPostTest::TestResponseCallback, |
| 53 base::Unretained(this))); | 56 base::Unretained(this))); |
| 54 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( | 57 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( |
| 55 HttpPost::kUrlFetcherId); | 58 HttpPost::kUrlFetcherId); |
| 56 fetcher->set_response_code(response_code); | 59 fetcher->set_response_code(response_code); |
| 57 fetcher->SetResponseString(response); | 60 fetcher->SetResponseString(response); |
| 58 fetcher->delegate()->OnURLFetchComplete(fetcher); | 61 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 59 delete pending_post_; | 62 delete pending_post_; |
| 60 return received_response_code_ == response_code && | 63 return received_response_code_ == response_code && |
| 61 received_response_ == response; | 64 received_response_ == response; |
| 62 } | 65 } |
| 63 | 66 |
| 67 net::TestURLFetcher* GetFetcher() { |
| 68 return fetcher_factory_.GetFetcherByID(HttpPost::kUrlFetcherId); |
| 69 } |
| 70 |
| 71 const std::string GetApiKeySent() { |
| 72 std::string api_key_sent; |
| 73 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), |
| 74 HttpPost::kApiKeyField, |
| 75 &api_key_sent); |
| 76 return api_key_sent; |
| 77 } |
| 78 |
| 79 const std::string GetTracingTokenSent() { |
| 80 std::string tracing_token_sent; |
| 81 net::GetValueForKeyInQuery(GetFetcher()->GetOriginalURL(), |
| 82 HttpPost::kTracingTokenField, |
| 83 &tracing_token_sent); |
| 84 return tracing_token_sent; |
| 85 } |
| 86 |
| 64 net::TestURLFetcherFactory fetcher_factory_; | 87 net::TestURLFetcherFactory fetcher_factory_; |
| 65 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; | 88 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; |
| 66 | 89 |
| 67 ClientVersion proto_; | 90 ClientVersion proto_; |
| 68 | 91 |
| 69 int received_response_code_; | 92 int received_response_code_; |
| 70 std::string received_response_; | 93 std::string received_response_; |
| 71 | 94 |
| 72 private: | 95 private: |
| 73 HttpPost* pending_post_; | 96 HttpPost* pending_post_; |
| 74 }; | 97 }; |
| 75 | 98 |
| 76 TEST_F(HttpPostTest, OKResponse) { | 99 TEST_F(HttpPostTest, OKResponse) { |
| 77 // "Send" the proto to the "server". | 100 // "Send" the proto to the "server". |
| 78 HttpPost* post = new HttpPost(context_getter_.get(), | 101 HttpPost* post = new HttpPost(context_getter_.get(), |
| 79 std::string("http://") + kFakeServerHost, | 102 std::string("http://") + kFakeServerHost, |
| 80 kRPCName, | 103 kRPCName, |
| 81 kTracingToken, | 104 kTracingToken, |
| 105 kApiKey, |
| 82 proto_); | 106 proto_); |
| 83 post->Start(base::Bind(&HttpPostTest::TestResponseCallback, | 107 post->Start(base::Bind(&HttpPostTest::TestResponseCallback, |
| 84 base::Unretained(this))); | 108 base::Unretained(this))); |
| 85 | 109 |
| 86 // Verify that the right data got sent to the right place. | 110 // Verify that the data was sent to the right place. |
| 87 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( | 111 GURL requested_url = GetFetcher()->GetOriginalURL(); |
| 88 HttpPost::kUrlFetcherId); | 112 EXPECT_EQ(kFakeServerHost, requested_url.host()); |
| 89 EXPECT_EQ(kFakeServerHost, fetcher->GetOriginalURL().host()); | 113 EXPECT_EQ(std::string("/") + kRPCName, requested_url.path()); |
| 90 EXPECT_EQ(std::string("/") + kRPCName, fetcher->GetOriginalURL().path()); | 114 |
| 91 std::string tracing_token_sent; | 115 // Check query parameters. |
| 92 EXPECT_TRUE(net::GetValueForKeyInQuery(fetcher->GetOriginalURL(), | 116 EXPECT_EQ(kApiKey, GetApiKeySent()); |
| 93 HttpPost::kTracingTokenField, | 117 EXPECT_EQ(std::string("token:") + kTracingToken, GetTracingTokenSent()); |
| 94 &tracing_token_sent)); | 118 |
| 95 EXPECT_EQ(std::string("token:") + kTracingToken, tracing_token_sent); | 119 // Verify that the right data was sent. |
| 96 std::string upload_data; | 120 std::string upload_data; |
| 97 ASSERT_TRUE(proto_.SerializeToString(&upload_data)); | 121 ASSERT_TRUE(proto_.SerializeToString(&upload_data)); |
| 98 EXPECT_EQ(upload_data, fetcher->upload_data()); | 122 EXPECT_EQ(upload_data, GetFetcher()->upload_data()); |
| 99 | 123 |
| 100 // Send a response and check that it's passed along correctly. | 124 // Send a response and check that it's passed along correctly. |
| 101 fetcher->set_response_code(net::HTTP_OK); | 125 GetFetcher()->set_response_code(net::HTTP_OK); |
| 102 fetcher->SetResponseString("Hello World!"); | 126 GetFetcher()->SetResponseString("Hello World!"); |
| 103 fetcher->delegate()->OnURLFetchComplete(fetcher); | 127 GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher()); |
| 104 EXPECT_EQ(net::HTTP_OK, received_response_code_); | 128 EXPECT_EQ(net::HTTP_OK, received_response_code_); |
| 105 EXPECT_EQ("Hello World!", received_response_); | 129 EXPECT_EQ("Hello World!", received_response_); |
| 106 delete post; | 130 delete post; |
| 107 } | 131 } |
| 108 | 132 |
| 109 TEST_F(HttpPostTest, ErrorResponse) { | 133 TEST_F(HttpPostTest, ErrorResponse) { |
| 110 EXPECT_TRUE(ResponsePassedThrough( | 134 EXPECT_TRUE(ResponsePassedThrough( |
| 111 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); | 135 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); |
| 112 EXPECT_TRUE(ResponsePassedThrough( | 136 EXPECT_TRUE(ResponsePassedThrough( |
| 113 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); | 137 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); |
| 114 EXPECT_TRUE(ResponsePassedThrough(-1, "")); | 138 EXPECT_TRUE(ResponsePassedThrough(-1, "")); |
| 115 } | 139 } |
| 116 | 140 |
| 117 } // namespace copresence | 141 } // namespace copresence |
| OLD | NEW |