Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
|
Daniel Erat
2014/08/06 00:44:47
we don't use the (c) anymore
Charlie
2014/08/06 19:32:18
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "base/command_line.h" | |
| 6 #include "base/test/test_simple_task_runner.h" | |
| 7 #include "chrome/common/chrome_switches.h" | |
|
Daniel Erat
2014/08/06 16:01:15
(mentioned earlier, but this shouldn't depend on c
Charlie
2014/08/06 19:32:18
Done.
| |
| 8 #include "components/copresence/proto/data.pb.h" | |
| 9 #include "components/copresence/rpc/http_post.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 | |
| 20 } // namespace | |
| 21 | |
| 22 using google::protobuf::MessageLite; | |
| 23 | |
| 24 namespace copresence { | |
| 25 | |
| 26 class HttpPostTest : public testing::Test { | |
| 27 public: | |
| 28 HttpPostTest() { | |
| 29 context_getter_ = new net::TestURLRequestContextGetter( | |
| 30 make_scoped_refptr(new base::TestSimpleTaskRunner)); | |
|
Daniel Erat
2014/08/06 16:01:15
indent four spaces beyond previous line, not two
Charlie
2014/08/06 19:32:18
Oops. Done.
On 2014/08/06 16:01:15, Daniel Erat w
| |
| 31 } | |
| 32 virtual ~HttpPostTest() {} | |
| 33 | |
| 34 // Record the response sent back to the client for verification. | |
| 35 void TestResponseCallback(int response_code, const std::string& response) { | |
| 36 received_response_code_ = response_code; | |
| 37 received_response_ = response; | |
| 38 } | |
| 39 | |
| 40 protected: | |
| 41 bool ResponsePassedThrough(int response_code, const std::string& response) { | |
| 42 new HttpPost(context_getter_.get(), | |
| 43 kRPCName, | |
| 44 make_scoped_ptr<MessageLite>(new ClientVersion(proto_)), | |
| 45 base::Bind(&HttpPostTest::TestResponseCallback, | |
| 46 base::Unretained(this))); | |
| 47 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( | |
| 48 HttpPost::kUrlFetcherId); | |
| 49 fetcher->set_response_code(response_code); | |
| 50 fetcher->SetResponseString(response); | |
| 51 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 52 return received_response_code_ == response_code && | |
| 53 received_response_ == response; | |
| 54 } | |
| 55 | |
| 56 virtual void SetUp() { | |
| 57 proto_.set_client("test_client"); | |
|
Daniel Erat
2014/08/06 16:01:15
just do this in the c'tor. you don't need SetUp()
Charlie
2014/08/06 19:32:18
Done.
| |
| 58 proto_.set_version_code(123); | |
| 59 } | |
| 60 | |
| 61 static void SetUpTestCase() { | |
| 62 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
|
Daniel Erat
2014/08/06 16:01:16
could this cause problems for other tests running
Charlie
2014/08/06 19:32:18
Makes sense. Done.
On 2014/08/06 16:01:16, Daniel
| |
| 63 switches::kCopresenceServer, std::string("http://") + kFakeServerHost); | |
| 64 } | |
| 65 | |
| 66 static void TearDownTestCase() {} | |
|
Daniel Erat
2014/08/06 16:01:16
don't define this if it's empty
Charlie
2014/08/06 19:32:18
For some reason I got errors when I had SetUpTestC
| |
| 67 | |
| 68 net::TestURLFetcherFactory fetcher_factory_; | |
| 69 scoped_refptr<net::TestURLRequestContextGetter> context_getter_; | |
| 70 | |
| 71 ClientVersion proto_; | |
| 72 | |
| 73 int received_response_code_; | |
|
Daniel Erat
2014/08/06 16:01:15
initialize this in the c'tor so it doesn't contain
Charlie
2014/08/06 19:32:18
Done. Sorry, writing Java code has made me sloppy
| |
| 74 std::string received_response_; | |
| 75 }; | |
| 76 | |
| 77 TEST_F(HttpPostTest, OKResponse) { | |
| 78 // "Send" the proto to the "server". | |
| 79 new HttpPost(context_getter_.get(), | |
| 80 kRPCName, | |
| 81 make_scoped_ptr<MessageLite>(new ClientVersion(proto_)), | |
| 82 base::Bind(&HttpPostTest::TestResponseCallback, | |
| 83 base::Unretained(this))); | |
| 84 | |
| 85 // Verify that the right data got sent to the right place. | |
| 86 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( | |
| 87 HttpPost::kUrlFetcherId); | |
| 88 EXPECT_EQ(kFakeServerHost, fetcher->GetOriginalURL().host()); | |
| 89 EXPECT_EQ(std::string("/") + kRPCName, fetcher->GetOriginalURL().path()); | |
| 90 std::string upload_data; | |
| 91 DCHECK(proto_.SerializeToString(&upload_data)); | |
| 92 EXPECT_EQ(upload_data, fetcher->upload_data()); | |
| 93 | |
| 94 // Send a response and check that it's passed along correctly. | |
| 95 fetcher->set_response_code(net::HTTP_OK); | |
| 96 fetcher->SetResponseString("Hello World!"); | |
| 97 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 98 EXPECT_EQ(net::HTTP_OK, received_response_code_); | |
| 99 EXPECT_EQ("Hello World!", received_response_); | |
| 100 } | |
| 101 | |
| 102 TEST_F(HttpPostTest, ErrorResponse) { | |
| 103 EXPECT_TRUE(ResponsePassedThrough( | |
| 104 net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); | |
| 105 EXPECT_TRUE(ResponsePassedThrough( | |
| 106 net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); | |
| 107 EXPECT_TRUE(ResponsePassedThrough(-1, "")); | |
| 108 } | |
| 109 | |
| 110 } // namespace copresence | |
| OLD | NEW |