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" | |
willchan no longer on Chromium
2014/08/08 01:05:59
Why is this here?
Charlie
2014/08/08 21:57:53
Outdated. Removed.
| |
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" | |
willchan no longer on Chromium
2014/08/08 01:05:59
This goes first, as per style guide.
Charlie
2014/08/08 21:57:53
Done.
| |
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, | |
38 const std::string& response, | |
39 HttpPost* completed_post) { | |
40 received_response_code_ = response_code; | |
41 received_response_ = response; | |
42 completed_post_ = completed_post; | |
43 } | |
44 | |
45 protected: | |
46 bool ResponsePassedThrough(int response_code, const std::string& response) { | |
47 HttpPost* post = new HttpPost( | |
48 context_getter_.get(), | |
49 std::string("http://") + kFakeServerHost, | |
50 kRPCName, | |
51 proto_, | |
52 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 return received_response_code_ == response_code && | |
60 received_response_ == response && | |
61 completed_post_ == post; | |
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 HttpPost* completed_post_; | |
72 }; | |
73 | |
74 TEST_F(HttpPostTest, OKResponse) { | |
75 // "Send" the proto to the "server". | |
76 HttpPost* post = new HttpPost(context_getter_.get(), | |
77 std::string("http://") + kFakeServerHost, | |
78 kRPCName, | |
79 proto_, | |
80 base::Bind(&HttpPostTest::TestResponseCallback, | |
81 base::Unretained(this))); | |
82 | |
83 // Verify that the right data got sent to the right place. | |
84 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( | |
85 HttpPost::kUrlFetcherId); | |
86 EXPECT_EQ(kFakeServerHost, fetcher->GetOriginalURL().host()); | |
87 EXPECT_EQ(std::string("/") + kRPCName, fetcher->GetOriginalURL().path()); | |
88 std::string upload_data; | |
89 DCHECK(proto_.SerializeToString(&upload_data)); | |
90 EXPECT_EQ(upload_data, fetcher->upload_data()); | |
91 | |
92 // Send a response and check that it's passed along correctly. | |
93 fetcher->set_response_code(net::HTTP_OK); | |
94 fetcher->SetResponseString("Hello World!"); | |
95 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
96 EXPECT_EQ(net::HTTP_OK, received_response_code_); | |
97 EXPECT_EQ("Hello World!", received_response_); | |
98 EXPECT_EQ(post, completed_post_); | |
99 } | |
100 | |
101 TEST_F(HttpPostTest, ErrorResponse) { | |
102 EXPECT_TRUE(ResponsePassedThrough( | |
103 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); | |
104 EXPECT_TRUE(ResponsePassedThrough( | |
105 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); | |
106 EXPECT_TRUE(ResponsePassedThrough(-1, "")); | |
107 } | |
108 | |
109 } // namespace copresence | |
OLD | NEW |