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
{
16 const char kSerializedRequestProto
[] = "serialized_request_proto";
17 const char kSerializedResponseProto
[] = "result_proto";
18 const char kRequestUrl
[] = "https://googleapis.com/cryptauth/test";
22 class ProximityAuthCryptAuthApiCallFlowTest
23 : public testing::Test
,
24 public net::TestURLFetcherDelegateForTests
{
26 ProximityAuthCryptAuthApiCallFlowTest()
27 : url_request_context_getter_(new net::TestURLRequestContextGetter(
28 new base::TestSimpleTaskRunner())) {}
30 void SetUp() override
{
31 // The TestURLFetcherFactory will override the global URLFetcherFactory for
33 url_fetcher_factory_
.reset(new net::TestURLFetcherFactory());
34 url_fetcher_factory_
->SetDelegateForTests(this);
37 void StartApiCallFlow() {
38 flow_
.Start(GURL(kRequestUrl
), url_request_context_getter_
.get(),
39 "access_token", kSerializedRequestProto
,
40 base::Bind(&ProximityAuthCryptAuthApiCallFlowTest::OnResult
,
41 base::Unretained(this)),
42 base::Bind(&ProximityAuthCryptAuthApiCallFlowTest::OnError
,
43 base::Unretained(this)));
44 // URLFetcher object for the API request should be created.
45 CheckCryptAuthHttpRequest();
48 void OnResult(const std::string
& result
) {
49 EXPECT_FALSE(result_
);
50 result_
.reset(new std::string(result
));
53 void OnError(const std::string
& error_message
) {
54 EXPECT_FALSE(error_message_
);
55 error_message_
.reset(new std::string(error_message
));
58 void CheckCryptAuthHttpRequest() {
59 ASSERT_TRUE(url_fetcher_
);
60 EXPECT_EQ(GURL(kRequestUrl
), url_fetcher_
->GetOriginalURL());
61 EXPECT_EQ(kSerializedRequestProto
, url_fetcher_
->upload_data());
63 net::HttpRequestHeaders request_headers
;
64 url_fetcher_
->GetExtraRequestHeaders(&request_headers
);
66 EXPECT_EQ("application/x-protobuf", url_fetcher_
->upload_content_type());
69 // Responds to the current HTTP request. If the |request_status| is not
70 // success, then the |response_code| and |response_string| arguments will be
72 void CompleteCurrentRequest(net::URLRequestStatus::Status request_status
,
74 const std::string
& response_string
) {
75 ASSERT_TRUE(url_fetcher_
);
76 net::TestURLFetcher
* url_fetcher
= url_fetcher_
;
78 url_fetcher
->set_status(net::URLRequestStatus(request_status
, 0));
79 if (request_status
== net::URLRequestStatus::SUCCESS
) {
80 url_fetcher
->set_response_code(response_code
);
81 url_fetcher
->SetResponseString(response_string
);
83 url_fetcher
->delegate()->OnURLFetchComplete(url_fetcher
);
86 // net::TestURLFetcherDelegateForTests overrides.
87 void OnRequestStart(int fetcher_id
) override
{
88 url_fetcher_
= url_fetcher_factory_
->GetFetcherByID(fetcher_id
);
91 void OnChunkUpload(int fetcher_id
) override
{}
93 void OnRequestEnd(int fetcher_id
) override
{}
95 net::TestURLFetcher
* url_fetcher_
;
96 scoped_ptr
<std::string
> result_
;
97 scoped_ptr
<std::string
> error_message_
;
100 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
101 scoped_ptr
<net::TestURLFetcherFactory
> url_fetcher_factory_
;
102 CryptAuthApiCallFlow flow_
;
104 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthApiCallFlowTest
);
107 TEST_F(ProximityAuthCryptAuthApiCallFlowTest
, RequestSuccess
) {
109 CompleteCurrentRequest(
110 net::URLRequestStatus::SUCCESS
, net::HTTP_OK
, kSerializedResponseProto
);
111 EXPECT_EQ(kSerializedResponseProto
, *result_
);
112 EXPECT_FALSE(error_message_
);
115 TEST_F(ProximityAuthCryptAuthApiCallFlowTest
, RequestFailure
) {
117 CompleteCurrentRequest(net::URLRequestStatus::FAILED
, 0, std::string());
118 EXPECT_FALSE(result_
);
119 EXPECT_EQ("Request failed", *error_message_
);
122 TEST_F(ProximityAuthCryptAuthApiCallFlowTest
, RequestStatus500
) {
124 CompleteCurrentRequest(net::URLRequestStatus::SUCCESS
,
125 net::HTTP_INTERNAL_SERVER_ERROR
,
126 "CryptAuth Meltdown.");
127 EXPECT_FALSE(result_
);
128 EXPECT_EQ("HTTP status: 500", *error_message_
);
131 // The empty string is a valid protocol buffer message serialization.
132 TEST_F(ProximityAuthCryptAuthApiCallFlowTest
, ResponseWithNoBody
) {
134 CompleteCurrentRequest(
135 net::URLRequestStatus::SUCCESS
, net::HTTP_OK
, std::string());
136 EXPECT_EQ(std::string(), *result_
);
137 EXPECT_FALSE(error_message_
);
140 } // namespace proximity_auth