Home | History | Annotate | Download | only in rpc
      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 
     23 }  // namespace
     24 
     25 using google::protobuf::MessageLite;
     26 
     27 namespace copresence {
     28 
     29 class HttpPostTest : public testing::Test {
     30  public:
     31   HttpPostTest()
     32       : received_response_code_(0) {
     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   virtual ~HttpPostTest() {}
     39 
     40   // Record the response sent back to the client for verification.
     41   void TestResponseCallback(int response_code,
     42                             const std::string& response) {
     43     received_response_code_ = response_code;
     44     received_response_ = response;
     45   }
     46 
     47  protected:
     48   bool ResponsePassedThrough(int response_code, const std::string& response) {
     49     pending_post_ = new HttpPost(context_getter_.get(),
     50                                  std::string("http://") + kFakeServerHost,
     51                                  kRPCName,
     52                                  "",
     53                                  kApiKey,
     54                                  proto_);
     55     pending_post_->Start(base::Bind(&HttpPostTest::TestResponseCallback,
     56                                     base::Unretained(this)));
     57     net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(
     58         HttpPost::kUrlFetcherId);
     59     fetcher->set_response_code(response_code);
     60     fetcher->SetResponseString(response);
     61     fetcher->delegate()->OnURLFetchComplete(fetcher);
     62     delete pending_post_;
     63     return received_response_code_ == response_code &&
     64            received_response_ == response;
     65   }
     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::kTracingField,
     83                                &tracing_token_sent);
     84     return tracing_token_sent;
     85   }
     86 
     87   net::TestURLFetcherFactory fetcher_factory_;
     88   scoped_refptr<net::TestURLRequestContextGetter> context_getter_;
     89 
     90   ClientVersion proto_;
     91 
     92   int received_response_code_;
     93   std::string received_response_;
     94 
     95  private:
     96   HttpPost* pending_post_;
     97 };
     98 
     99 TEST_F(HttpPostTest, OKResponse) {
    100   // "Send" the proto to the "server".
    101   HttpPost* post = new HttpPost(context_getter_.get(),
    102                                 std::string("http://") + kFakeServerHost,
    103                                 kRPCName,
    104                                 kTracingToken,
    105                                 kApiKey,
    106                                 proto_);
    107   post->Start(base::Bind(&HttpPostTest::TestResponseCallback,
    108                          base::Unretained(this)));
    109 
    110   // Verify that the data was sent to the right place.
    111   GURL requested_url = GetFetcher()->GetOriginalURL();
    112   EXPECT_EQ(kFakeServerHost, requested_url.host());
    113   EXPECT_EQ(std::string("/") + kRPCName, requested_url.path());
    114 
    115   // Check query parameters.
    116   EXPECT_EQ(kApiKey, GetApiKeySent());
    117   EXPECT_EQ(std::string("token:") + kTracingToken, GetTracingTokenSent());
    118 
    119   // Verify that the right data was sent.
    120   std::string upload_data;
    121   ASSERT_TRUE(proto_.SerializeToString(&upload_data));
    122   EXPECT_EQ(upload_data, GetFetcher()->upload_data());
    123 
    124   // Send a response and check that it's passed along correctly.
    125   GetFetcher()->set_response_code(net::HTTP_OK);
    126   GetFetcher()->SetResponseString("Hello World!");
    127   GetFetcher()->delegate()->OnURLFetchComplete(GetFetcher());
    128   EXPECT_EQ(net::HTTP_OK, received_response_code_);
    129   EXPECT_EQ("Hello World!", received_response_);
    130   delete post;
    131 }
    132 
    133 TEST_F(HttpPostTest, ErrorResponse) {
    134   EXPECT_TRUE(ResponsePassedThrough(
    135       net::HTTP_BAD_REQUEST, "Bad client. Shame on you."));
    136   EXPECT_TRUE(ResponsePassedThrough(
    137       net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me."));
    138   EXPECT_TRUE(ResponsePassedThrough(-1, ""));
    139 }
    140 
    141 }  // namespace copresence
    142