Chromium Code Reviews| Index: components/copresence/rpc/http_post_unittest.cc |
| diff --git a/components/copresence/rpc/http_post_unittest.cc b/components/copresence/rpc/http_post_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7b628a1c0db76a93db8e114fed04aea612f1585a |
| --- /dev/null |
| +++ b/components/copresence/rpc/http_post_unittest.cc |
| @@ -0,0 +1,110 @@ |
| +// 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.
|
| +// Use of this source code is governed by a BSD-style license |
| +// that can be found in the LICENSE file. |
| + |
| +#include "base/command_line.h" |
| +#include "base/test/test_simple_task_runner.h" |
| +#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.
|
| +#include "components/copresence/proto/data.pb.h" |
| +#include "components/copresence/rpc/http_post.h" |
| +#include "net/http/http_status_code.h" |
| +#include "net/url_request/test_url_fetcher_factory.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +const char kFakeServerHost[] = "test.server.google.com"; |
| +const char kRPCName[] = "testRpc"; |
| + |
| +} // namespace |
| + |
| +using google::protobuf::MessageLite; |
| + |
| +namespace copresence { |
| + |
| +class HttpPostTest : public testing::Test { |
| + public: |
| + HttpPostTest() { |
| + context_getter_ = new net::TestURLRequestContextGetter( |
| + 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
|
| + } |
| + virtual ~HttpPostTest() {} |
| + |
| + // Record the response sent back to the client for verification. |
| + void TestResponseCallback(int response_code, const std::string& response) { |
| + received_response_code_ = response_code; |
| + received_response_ = response; |
| + } |
| + |
| + protected: |
| + bool ResponsePassedThrough(int response_code, const std::string& response) { |
| + new HttpPost(context_getter_.get(), |
| + kRPCName, |
| + make_scoped_ptr<MessageLite>(new ClientVersion(proto_)), |
| + base::Bind(&HttpPostTest::TestResponseCallback, |
| + base::Unretained(this))); |
| + net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( |
| + HttpPost::kUrlFetcherId); |
| + fetcher->set_response_code(response_code); |
| + fetcher->SetResponseString(response); |
| + fetcher->delegate()->OnURLFetchComplete(fetcher); |
| + return received_response_code_ == response_code && |
| + received_response_ == response; |
| + } |
| + |
| + virtual void SetUp() { |
| + 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.
|
| + proto_.set_version_code(123); |
| + } |
| + |
| + static void SetUpTestCase() { |
| + 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
|
| + switches::kCopresenceServer, std::string("http://") + kFakeServerHost); |
| + } |
| + |
| + 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
|
| + |
| + net::TestURLFetcherFactory fetcher_factory_; |
| + scoped_refptr<net::TestURLRequestContextGetter> context_getter_; |
| + |
| + ClientVersion proto_; |
| + |
| + 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
|
| + std::string received_response_; |
| +}; |
| + |
| +TEST_F(HttpPostTest, OKResponse) { |
| + // "Send" the proto to the "server". |
| + new HttpPost(context_getter_.get(), |
| + kRPCName, |
| + make_scoped_ptr<MessageLite>(new ClientVersion(proto_)), |
| + base::Bind(&HttpPostTest::TestResponseCallback, |
| + base::Unretained(this))); |
| + |
| + // Verify that the right data got sent to the right place. |
| + net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID( |
| + HttpPost::kUrlFetcherId); |
| + EXPECT_EQ(kFakeServerHost, fetcher->GetOriginalURL().host()); |
| + EXPECT_EQ(std::string("/") + kRPCName, fetcher->GetOriginalURL().path()); |
| + std::string upload_data; |
| + DCHECK(proto_.SerializeToString(&upload_data)); |
| + EXPECT_EQ(upload_data, fetcher->upload_data()); |
| + |
| + // Send a response and check that it's passed along correctly. |
| + fetcher->set_response_code(net::HTTP_OK); |
| + fetcher->SetResponseString("Hello World!"); |
| + fetcher->delegate()->OnURLFetchComplete(fetcher); |
| + EXPECT_EQ(net::HTTP_OK, received_response_code_); |
| + EXPECT_EQ("Hello World!", received_response_); |
| +} |
| + |
| +TEST_F(HttpPostTest, ErrorResponse) { |
| + EXPECT_TRUE(ResponsePassedThrough( |
| + net::HTTP_BAD_REQUEST, "Bad client. Shame on you.")); |
| + EXPECT_TRUE(ResponsePassedThrough( |
| + net::HTTP_INTERNAL_SERVER_ERROR, "I'm dying. Forgive me.")); |
| + EXPECT_TRUE(ResponsePassedThrough(-1, "")); |
| +} |
| + |
| +} // namespace copresence |