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