| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" | 5 #include "components/proximity_auth/cryptauth/cryptauth_api_call_flow.h" |
| 6 | 6 |
| 7 #include "base/test/test_simple_task_runner.h" | 7 #include "base/test/test_simple_task_runner.h" |
| 8 #include "net/base/net_errors.h" |
| 8 #include "net/url_request/test_url_fetcher_factory.h" | 9 #include "net/url_request/test_url_fetcher_factory.h" |
| 10 #include "net/url_request/url_request_status.h" |
| 9 #include "net/url_request/url_request_test_util.h" | 11 #include "net/url_request/url_request_test_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 13 |
| 12 namespace proximity_auth { | 14 namespace proximity_auth { |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 const char kSerializedRequestProto[] = "serialized_request_proto"; | 18 const char kSerializedRequestProto[] = "serialized_request_proto"; |
| 17 const char kSerializedResponseProto[] = "result_proto"; | 19 const char kSerializedResponseProto[] = "result_proto"; |
| 18 const char kRequestUrl[] = "https://googleapis.com/cryptauth/test"; | 20 const char kRequestUrl[] = "https://googleapis.com/cryptauth/test"; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 ASSERT_TRUE(url_fetcher_); | 61 ASSERT_TRUE(url_fetcher_); |
| 60 EXPECT_EQ(GURL(kRequestUrl), url_fetcher_->GetOriginalURL()); | 62 EXPECT_EQ(GURL(kRequestUrl), url_fetcher_->GetOriginalURL()); |
| 61 EXPECT_EQ(kSerializedRequestProto, url_fetcher_->upload_data()); | 63 EXPECT_EQ(kSerializedRequestProto, url_fetcher_->upload_data()); |
| 62 | 64 |
| 63 net::HttpRequestHeaders request_headers; | 65 net::HttpRequestHeaders request_headers; |
| 64 url_fetcher_->GetExtraRequestHeaders(&request_headers); | 66 url_fetcher_->GetExtraRequestHeaders(&request_headers); |
| 65 | 67 |
| 66 EXPECT_EQ("application/x-protobuf", url_fetcher_->upload_content_type()); | 68 EXPECT_EQ("application/x-protobuf", url_fetcher_->upload_content_type()); |
| 67 } | 69 } |
| 68 | 70 |
| 69 // Responds to the current HTTP request. If the |request_status| is not | 71 // Responds to the current HTTP request. If the |error| is not |net::OK|, then |
| 70 // success, then the |response_code| and |response_string| arguments will be | 72 // the |response_code| and |response_string| arguments will be ignored. |
| 71 // ignored. | 73 void CompleteCurrentRequest(net::Error error, |
| 72 void CompleteCurrentRequest(net::URLRequestStatus::Status request_status, | |
| 73 int response_code, | 74 int response_code, |
| 74 const std::string& response_string) { | 75 const std::string& response_string) { |
| 75 ASSERT_TRUE(url_fetcher_); | 76 ASSERT_TRUE(url_fetcher_); |
| 76 net::TestURLFetcher* url_fetcher = url_fetcher_; | 77 net::TestURLFetcher* url_fetcher = url_fetcher_; |
| 77 url_fetcher_ = NULL; | 78 url_fetcher_ = NULL; |
| 78 url_fetcher->set_status(net::URLRequestStatus(request_status, 0)); | 79 url_fetcher->set_status(net::URLRequestStatus::FromError(error)); |
| 79 if (request_status == net::URLRequestStatus::SUCCESS) { | 80 if (error == net::OK) { |
| 80 url_fetcher->set_response_code(response_code); | 81 url_fetcher->set_response_code(response_code); |
| 81 url_fetcher->SetResponseString(response_string); | 82 url_fetcher->SetResponseString(response_string); |
| 82 } | 83 } |
| 83 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 84 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 84 } | 85 } |
| 85 | 86 |
| 86 // net::TestURLFetcherDelegateForTests overrides. | 87 // net::TestURLFetcherDelegateForTests overrides. |
| 87 void OnRequestStart(int fetcher_id) override { | 88 void OnRequestStart(int fetcher_id) override { |
| 88 url_fetcher_ = url_fetcher_factory_->GetFetcherByID(fetcher_id); | 89 url_fetcher_ = url_fetcher_factory_->GetFetcherByID(fetcher_id); |
| 89 } | 90 } |
| 90 | 91 |
| 91 void OnChunkUpload(int fetcher_id) override {} | 92 void OnChunkUpload(int fetcher_id) override {} |
| 92 | 93 |
| 93 void OnRequestEnd(int fetcher_id) override {} | 94 void OnRequestEnd(int fetcher_id) override {} |
| 94 | 95 |
| 95 net::TestURLFetcher* url_fetcher_; | 96 net::TestURLFetcher* url_fetcher_; |
| 96 scoped_ptr<std::string> result_; | 97 scoped_ptr<std::string> result_; |
| 97 scoped_ptr<std::string> error_message_; | 98 scoped_ptr<std::string> error_message_; |
| 98 | 99 |
| 99 private: | 100 private: |
| 100 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 101 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
| 101 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; | 102 scoped_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; |
| 102 CryptAuthApiCallFlow flow_; | 103 CryptAuthApiCallFlow flow_; |
| 103 | 104 |
| 104 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthApiCallFlowTest); | 105 DISALLOW_COPY_AND_ASSIGN(ProximityAuthCryptAuthApiCallFlowTest); |
| 105 }; | 106 }; |
| 106 | 107 |
| 107 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestSuccess) { | 108 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestSuccess) { |
| 108 StartApiCallFlow(); | 109 StartApiCallFlow(); |
| 109 CompleteCurrentRequest( | 110 CompleteCurrentRequest(net::OK, net::HTTP_OK, kSerializedResponseProto); |
| 110 net::URLRequestStatus::SUCCESS, net::HTTP_OK, kSerializedResponseProto); | |
| 111 EXPECT_EQ(kSerializedResponseProto, *result_); | 111 EXPECT_EQ(kSerializedResponseProto, *result_); |
| 112 EXPECT_FALSE(error_message_); | 112 EXPECT_FALSE(error_message_); |
| 113 } | 113 } |
| 114 | 114 |
| 115 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestFailure) { | 115 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestFailure) { |
| 116 StartApiCallFlow(); | 116 StartApiCallFlow(); |
| 117 CompleteCurrentRequest(net::URLRequestStatus::FAILED, 0, std::string()); | 117 CompleteCurrentRequest(net::ERR_FAILED, 0, std::string()); |
| 118 EXPECT_FALSE(result_); | 118 EXPECT_FALSE(result_); |
| 119 EXPECT_EQ("Request failed", *error_message_); | 119 EXPECT_EQ("Request failed", *error_message_); |
| 120 } | 120 } |
| 121 | 121 |
| 122 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestStatus500) { | 122 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, RequestStatus500) { |
| 123 StartApiCallFlow(); | 123 StartApiCallFlow(); |
| 124 CompleteCurrentRequest(net::URLRequestStatus::SUCCESS, | 124 CompleteCurrentRequest(net::OK, net::HTTP_INTERNAL_SERVER_ERROR, |
| 125 net::HTTP_INTERNAL_SERVER_ERROR, | |
| 126 "CryptAuth Meltdown."); | 125 "CryptAuth Meltdown."); |
| 127 EXPECT_FALSE(result_); | 126 EXPECT_FALSE(result_); |
| 128 EXPECT_EQ("HTTP status: 500", *error_message_); | 127 EXPECT_EQ("HTTP status: 500", *error_message_); |
| 129 } | 128 } |
| 130 | 129 |
| 131 // The empty string is a valid protocol buffer message serialization. | 130 // The empty string is a valid protocol buffer message serialization. |
| 132 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, ResponseWithNoBody) { | 131 TEST_F(ProximityAuthCryptAuthApiCallFlowTest, ResponseWithNoBody) { |
| 133 StartApiCallFlow(); | 132 StartApiCallFlow(); |
| 134 CompleteCurrentRequest( | 133 CompleteCurrentRequest(net::OK, net::HTTP_OK, std::string()); |
| 135 net::URLRequestStatus::SUCCESS, net::HTTP_OK, std::string()); | |
| 136 EXPECT_EQ(std::string(), *result_); | 134 EXPECT_EQ(std::string(), *result_); |
| 137 EXPECT_FALSE(error_message_); | 135 EXPECT_FALSE(error_message_); |
| 138 } | 136 } |
| 139 | 137 |
| 140 } // namespace proximity_auth | 138 } // namespace proximity_auth |
| OLD | NEW |