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

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

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

Powered by Google App Engine
This is Rietveld 408576698