OLD | NEW |
(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 "base/command_line.h" |
| 6 #include "base/test/test_simple_task_runner.h" |
| 7 #include "components/copresence/proto/data.pb.h" |
| 8 #include "components/copresence/rpc/http_post.h" |
| 9 #include "net/http/http_status_code.h" |
| 10 #include "net/url_request/test_url_fetcher_factory.h" |
| 11 #include "net/url_request/url_request_test_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 const char kFakeServerHost[] = "test.server.google.com"; |
| 17 const char kRPCName[] = "testRpc"; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 using google::protobuf::MessageLite; |
| 22 |
| 23 namespace copresence { |
| 24 |
| 25 class HttpPostTest : public testing::Test { |
| 26 public: |
| 27 HttpPostTest() |
| 28 : received_response_code_(0) { |
| 29 context_getter_ = new net::TestURLRequestContextGetter( |
| 30 make_scoped_refptr(new base::TestSimpleTaskRunner)); |
| 31 proto_.set_client("test_client"); |
| 32 proto_.set_version_code(123); |
| 33 } |
| 34 virtual ~HttpPostTest() {} |
| 35 |
| 36 // Record the response sent back to the client for verification. |
| 37 void TestResponseCallback(int response_code, const std::string& response) { |
| 38 received_response_code_ = response_code; |
| 39 received_response_ = response; |
| 40 } |
| 41 |
| 42 protected: |
| 43 bool ResponsePassedThrough(int response_code, const std::string& response) { |
| 44 new HttpPost(context_getter_.get(), |
| 45 std::string("http://") + kFakeServerHost, |
| 46 kRPCName, |
| 47 proto_, |
| 48 base::Bind(&HttpPostTest::TestResponseCallback, |
| 49 base::Unretained(this))); |
| 50 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( |
| 51 HttpPost::kUrlFetcherId); |
| 52 fetcher->set_response_code(response_code); |
| 53 fetcher->SetResponseString(response); |
| 54 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 55 return received_response_code_ == response_code && |
| 56 received_response_ == response; |
| 57 } |
| 58 |
| 59 net::TestURLFetcherFactory fetcher_factory_; |
| 60 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; |
| 61 |
| 62 ClientVersion proto_; |
| 63 |
| 64 int received_response_code_; |
| 65 std::string received_response_; |
| 66 }; |
| 67 |
| 68 TEST_F(HttpPostTest, OKResponse) { |
| 69 // "Send" the proto to the "server". |
| 70 new HttpPost(context_getter_.get(), |
| 71 std::string("http://") + kFakeServerHost, |
| 72 kRPCName, |
| 73 proto_, |
| 74 base::Bind(&HttpPostTest::TestResponseCallback, |
| 75 base::Unretained(this))); |
| 76 |
| 77 // Verify that the right data got sent to the right place. |
| 78 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( |
| 79 HttpPost::kUrlFetcherId); |
| 80 EXPECT_EQ(kFakeServerHost, fetcher->GetOriginalURL().host()); |
| 81 EXPECT_EQ(std::string("/") + kRPCName, fetcher->GetOriginalURL().path()); |
| 82 std::string upload_data; |
| 83 DCHECK(proto_.SerializeToString(&upload_data)); |
| 84 EXPECT_EQ(upload_data, fetcher->upload_data()); |
| 85 |
| 86 // Send a response and check that it's passed along correctly. |
| 87 fetcher->set_response_code(net::HTTP_OK); |
| 88 fetcher->SetResponseString("Hello World!"); |
| 89 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 90 EXPECT_EQ(net::HTTP_OK, received_response_code_); |
| 91 EXPECT_EQ("Hello World!", received_response_); |
| 92 } |
| 93 |
| 94 TEST_F(HttpPostTest, ErrorResponse) { |
| 95 EXPECT_TRUE(ResponsePassedThrough( |
| 96 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); |
| 97 EXPECT_TRUE(ResponsePassedThrough( |
| 98 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); |
| 99 EXPECT_TRUE(ResponsePassedThrough(-1, "")); |
| 100 } |
| 101 |
| 102 } // namespace copresence |
OLD | NEW |