Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_api_call_flow_unittest.cc
blob53417e724249443f91fb9a51f48b800769b59723
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.
5 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h"
7 #include "base/test/test_simple_task_runner.h"
8 #include "net/url_request/test_url_fetcher_factory.h"
9 #include "net/url_request/url_request_test_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace proximity_auth {
14 namespace {
16 const char kSerializedRequestProto[] = "serialized_request_proto";
17 const char kSerializedResponseProto[] = "result_proto";
18 const char kRequestUrl[] = "https://googleapis.com/cryptauth/test";
20 } // namespace
22 class ProximityAuthCryptAuthApiCallFlowTest
23 : public testing::Test,
24 public net::TestURLFetcherDelegateForTests {
25 protected:
26 ProximityAuthCryptAuthApiCallFlowTest()
27 : url_request_context_getter_(new net::TestURLRequestContextGetter(
28 new base::TestSimpleTaskRunner())),
29 flow_(GURL(kRequestUrl)) {}
31 void SetUp() override {
32 // The TestURLFetcherFactory will override the global URLFetcherFactory for
33 // the entire test.
34 url_fetcher_factory_.reset(new net::TestURLFetcherFactory());
35 url_fetcher_factory_->SetDelegateForTests(this);
38 void StartApiCallFlow() {
39 flow_.Start(url_request_context_getter_.get(),
40 "access_token",
41 kSerializedRequestProto,
42 base::Bind(&ProximityAuthCryptAuthApiCallFlowTest::OnResult,
43 base::Unretained(this)),
44 base::Bind(&ProximityAuthCryptAuthApiCallFlowTest::OnError,
45 base::Unretained(this)));
46 // URLFetcher object for the API request should be created.
47 CheckCryptAuthHttpRequest();
50 void OnResult(const std::string& result) {
51 EXPECT_FALSE(result_);
52 result_.reset(new std::string(result));
55 void OnError(const std::string& error_message) {
56 EXPECT_FALSE(error_message_);
57 error_message_.reset(new std::string(error_message));
60 void CheckCryptAuthHttpRequest() {
61 ASSERT_TRUE(url_fetcher_);
62 EXPECT_EQ(GURL(kRequestUrl), url_fetcher_->GetOriginalURL());
63 EXPECT_EQ(kSerializedRequestProto, url_fetcher_->upload_data());
65 net::HttpRequestHeaders request_headers;
66 url_fetcher_->GetExtraRequestHeaders(&request_headers);
68 EXPECT_EQ("application/x-protobuf", url_fetcher_->upload_content_type());
71 // Responds to the current HTTP request. If the |request_status| is not
72 // success, then the |response_code| and |response_string| arguments will be
73 // ignored.
74 void CompleteCurrentRequest(net::URLRequestStatus::Status request_status,
75 int response_code,
76 const std::string& response_string) {
77 ASSERT_TRUE(url_fetcher_);
78 net::TestURLFetcher* url_fetcher = url_fetcher_;
79 url_fetcher_ = NULL;
80 url_fetcher->set_status(net::URLRequestStatus(request_status, 0));
81 if (request_status == net::URLRequestStatus::SUCCESS) {
82 url_fetcher->set_response_code(response_code);
83 url_fetcher->SetResponseString(response_string);
85 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
88 // net::TestURLFetcherDelegateForTests overrides.
89 void OnRequestStart(int fetcher_id) override {
90 url_fetcher_ = url_fetcher_factory_->GetFetcherByID(fetcher_id);
93 void OnChunkUpload(int fetcher_id) override {}
95 void OnRequestEnd(int fetcher_id) override {}
97 net::TestURLFetcher* url_fetcher_;
98 scoped_ptr<std::string> result_;
99 scoped_ptr<std::string> error_message_;
101 private:
102 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
103 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_;
104 CryptAuthApiCallFlow flow_;
106 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthApiCallFlowTest);
109 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestSuccess) {
110 StartApiCallFlow();
111 CompleteCurrentRequest(
112 net::URLRequestStatus::SUCCESS, net::HTTP_OK, kSerializedResponseProto);
113 EXPECT_EQ(kSerializedResponseProto, *result_);
114 EXPECT_FALSE(error_message_);
117 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestFailure) {
118 StartApiCallFlow();
119 CompleteCurrentRequest(net::URLRequestStatus::FAILED, 0, std::string());
120 EXPECT_FALSE(result_);
121 EXPECT_EQ("Request failed", *error_message_);
124 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestStatus500) {
125 StartApiCallFlow();
126 CompleteCurrentRequest(net::URLRequestStatus::SUCCESS,
127 net::HTTP_INTERNAL_SERVER_ERROR,
128 "CryptAuth Meltdown.");
129 EXPECT_FALSE(result_);
130 EXPECT_EQ("HTTP status: 500", *error_message_);
133 // The empty string is a valid protocol buffer message serialization.
134 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, ResponseWithNoBody) {
135 StartApiCallFlow();
136 CompleteCurrentRequest(
137 net::URLRequestStatus::SUCCESS, net::HTTP_OK, std::string());
138 EXPECT_EQ(std::string(), *result_);
139 EXPECT_FALSE(error_message_);
142 } // namespace proximity_auth