Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / components / proximity_auth / cryptauth / cryptauth_api_call_flow_unittest.cc
blobdadbfda2bcf179812c58c5244b347c996db9557a
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/base/net_errors.h"
9 #include "net/url_request/test_url_fetcher_factory.h"
10 #include "net/url_request/url_request_status.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace proximity_auth {
16 namespace {
18 const char kSerializedRequestProto[] = "serialized_request_proto";
19 const char kSerializedResponseProto[] = "result_proto";
20 const char kRequestUrl[] = "https://googleapis.com/cryptauth/test";
22 } // namespace
24 class ProximityAuthCryptAuthApiCallFlowTest
25 : public testing::Test,
26 public net::TestURLFetcherDelegateForTests {
27 protected:
28 ProximityAuthCryptAuthApiCallFlowTest()
29 : url_request_context_getter_(new net::TestURLRequestContextGetter(
30 new base::TestSimpleTaskRunner())) {}
32 void SetUp() override {
33 // The TestURLFetcherFactory will override the global URLFetcherFactory for
34 // the entire test.
35 url_fetcher_factory_.reset(new net::TestURLFetcherFactory());
36 url_fetcher_factory_->SetDelegateForTests(this);
39 void StartApiCallFlow() {
40 StartApiCallFlowWithRequest(kSerializedRequestProto);
43 void StartApiCallFlowWithRequest(const std::string& serialized_request) {
44 flow_.Start(GURL(kRequestUrl), url_request_context_getter_.get(),
45 "access_token", serialized_request,
46 base::Bind(&ProximityAuthCryptAuthApiCallFlowTest::OnResult,
47 base::Unretained(this)),
48 base::Bind(&ProximityAuthCryptAuthApiCallFlowTest::OnError,
49 base::Unretained(this)));
50 // URLFetcher object for the API request should be created.
51 CheckCryptAuthHttpRequest(serialized_request);
54 void OnResult(const std::string& result) {
55 EXPECT_FALSE(result_);
56 result_.reset(new std::string(result));
59 void OnError(const std::string& error_message) {
60 EXPECT_FALSE(error_message_);
61 error_message_.reset(new std::string(error_message));
64 void CheckCryptAuthHttpRequest(const std::string& serialized_request) {
65 ASSERT_TRUE(url_fetcher_);
66 EXPECT_EQ(GURL(kRequestUrl), url_fetcher_->GetOriginalURL());
67 EXPECT_EQ(serialized_request, url_fetcher_->upload_data());
69 net::HttpRequestHeaders request_headers;
70 url_fetcher_->GetExtraRequestHeaders(&request_headers);
72 EXPECT_EQ("application/x-protobuf", url_fetcher_->upload_content_type());
75 // Responds to the current HTTP request. If the |error| is not |net::OK|, then
76 // the |response_code| and |response_string| arguments will be ignored.
77 void CompleteCurrentRequest(net::Error error,
78 int response_code,
79 const std::string& response_string) {
80 ASSERT_TRUE(url_fetcher_);
81 net::TestURLFetcher* url_fetcher = url_fetcher_;
82 url_fetcher_ = NULL;
83 url_fetcher->set_status(net::URLRequestStatus::FromError(error));
84 if (error == net::OK) {
85 url_fetcher->set_response_code(response_code);
86 url_fetcher->SetResponseString(response_string);
88 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
91 // net::TestURLFetcherDelegateForTests overrides.
92 void OnRequestStart(int fetcher_id) override {
93 url_fetcher_ = url_fetcher_factory_->GetFetcherByID(fetcher_id);
96 void OnChunkUpload(int fetcher_id) override {}
98 void OnRequestEnd(int fetcher_id) override {}
100 net::TestURLFetcher* url_fetcher_;
101 scoped_ptr<std::string> result_;
102 scoped_ptr<std::string> error_message_;
104 private:
105 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
106 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_;
107 CryptAuthApiCallFlow flow_;
109 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthApiCallFlowTest);
112 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestSuccess) {
113 StartApiCallFlow();
114 CompleteCurrentRequest(net::OK, net::HTTP_OK, kSerializedResponseProto);
115 EXPECT_EQ(kSerializedResponseProto, *result_);
116 EXPECT_FALSE(error_message_);
119 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestFailure) {
120 StartApiCallFlow();
121 CompleteCurrentRequest(net::ERR_FAILED, 0, std::string());
122 EXPECT_FALSE(result_);
123 EXPECT_EQ("Request failed", *error_message_);
126 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestStatus500) {
127 StartApiCallFlow();
128 CompleteCurrentRequest(net::OK, net::HTTP_INTERNAL_SERVER_ERROR,
129 "CryptAuth Meltdown.");
130 EXPECT_FALSE(result_);
131 EXPECT_EQ("HTTP status: 500", *error_message_);
134 // The empty string is a valid protocol buffer message serialization.
135 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestWithNoBody) {
136 StartApiCallFlowWithRequest(std::string());
137 CompleteCurrentRequest(net::OK, net::HTTP_OK, kSerializedResponseProto);
138 EXPECT_EQ(kSerializedResponseProto, *result_);
139 EXPECT_FALSE(error_message_);
142 // The empty string is a valid protocol buffer message serialization.
143 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, ResponseWithNoBody) {
144 StartApiCallFlow();
145 CompleteCurrentRequest(net::OK, net::HTTP_OK, std::string());
146 EXPECT_EQ(std::string(), *result_);
147 EXPECT_FALSE(error_message_);
150 } // namespace proximity_auth