forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptauth_api_call_flow_unittest.cc
146 lines (121 loc) · 5.33 KB
/
cryptauth_api_call_flow_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/cryptauth/cryptauth_api_call_flow.h"
#include <memory>
#include "base/macros.h"
#include "base/test/scoped_task_environment.h"
#include "components/cryptauth/network_request_error.h"
#include "net/base/net_errors.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "services/network/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace cryptauth {
namespace {
const char kSerializedRequestProto[] = "serialized_request_proto";
const char kSerializedResponseProto[] = "result_proto";
const char kRequestUrl[] = "https://googleapis.com/cryptauth/test";
} // namespace
class CryptAuthApiCallFlowTest : public testing::Test {
protected:
CryptAuthApiCallFlowTest()
: shared_factory_(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_)) {
flow_.SetPartialNetworkTrafficAnnotation(
PARTIAL_TRAFFIC_ANNOTATION_FOR_TESTS);
}
void StartApiCallFlow() {
StartApiCallFlowWithRequest(kSerializedRequestProto);
}
void StartApiCallFlowWithRequest(const std::string& serialized_request) {
flow_.Start(
GURL(kRequestUrl), shared_factory_, "access_token", serialized_request,
base::Bind(&CryptAuthApiCallFlowTest::OnResult, base::Unretained(this)),
base::Bind(&CryptAuthApiCallFlowTest::OnError, base::Unretained(this)));
// A pending fetch for the API request should be created.
CheckCryptAuthHttpRequest(serialized_request);
}
void OnResult(const std::string& result) {
EXPECT_FALSE(result_ || network_error_);
result_.reset(new std::string(result));
}
void OnError(NetworkRequestError network_error) {
EXPECT_FALSE(result_ || network_error_);
network_error_.reset(new NetworkRequestError(network_error));
}
void CheckCryptAuthHttpRequest(const std::string& serialized_request) {
const std::vector<network::TestURLLoaderFactory::PendingRequest>& pending =
*test_url_loader_factory_.pending_requests();
ASSERT_EQ(1u, pending.size());
const network::ResourceRequest& request = pending[0].request;
EXPECT_EQ(GURL(kRequestUrl), request.url);
EXPECT_EQ(serialized_request, network::GetUploadData(request));
std::string content_type;
EXPECT_TRUE(request.headers.GetHeader(net::HttpRequestHeaders::kContentType,
&content_type));
EXPECT_EQ("application/x-protobuf", content_type);
}
// Responds to the current HTTP request. If the |error| is not |net::OK|, then
// the |response_code| and |response_string| arguments will be ignored.
void CompleteCurrentRequest(net::Error error,
int response_code,
const std::string& response_string) {
network::URLLoaderCompletionStatus completion_status(error);
network::ResourceResponseHead response_head;
std::string content;
if (error == net::OK) {
response_head = network::CreateResourceResponseHead(
static_cast<net::HttpStatusCode>(response_code));
content = response_string;
}
EXPECT_TRUE(test_url_loader_factory_.SimulateResponseForPendingRequest(
GURL(kRequestUrl), completion_status, response_head, content));
scoped_task_environment_.RunUntilIdle();
EXPECT_TRUE(result_ || network_error_);
}
std::unique_ptr<std::string> result_;
std::unique_ptr<NetworkRequestError> network_error_;
private:
base::test::ScopedTaskEnvironment scoped_task_environment_;
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> shared_factory_;
CryptAuthApiCallFlow flow_;
DISALLOW_COPY_AND_ASSIGN(CryptAuthApiCallFlowTest);
};
TEST_F(CryptAuthApiCallFlowTest, RequestSuccess) {
StartApiCallFlow();
CompleteCurrentRequest(net::OK, net::HTTP_OK, kSerializedResponseProto);
EXPECT_EQ(kSerializedResponseProto, *result_);
EXPECT_FALSE(network_error_);
}
TEST_F(CryptAuthApiCallFlowTest, RequestFailure) {
StartApiCallFlow();
CompleteCurrentRequest(net::ERR_FAILED, 0, std::string());
EXPECT_FALSE(result_);
EXPECT_EQ(NetworkRequestError::kOffline, *network_error_);
}
TEST_F(CryptAuthApiCallFlowTest, RequestStatus500) {
StartApiCallFlow();
CompleteCurrentRequest(net::OK, net::HTTP_INTERNAL_SERVER_ERROR,
"CryptAuth Meltdown.");
EXPECT_FALSE(result_);
EXPECT_EQ(NetworkRequestError::kInternalServerError, *network_error_);
}
// The empty string is a valid protocol buffer message serialization.
TEST_F(CryptAuthApiCallFlowTest, RequestWithNoBody) {
StartApiCallFlowWithRequest(std::string());
CompleteCurrentRequest(net::OK, net::HTTP_OK, kSerializedResponseProto);
EXPECT_EQ(kSerializedResponseProto, *result_);
EXPECT_FALSE(network_error_);
}
// The empty string is a valid protocol buffer message serialization.
TEST_F(CryptAuthApiCallFlowTest, ResponseWithNoBody) {
StartApiCallFlow();
CompleteCurrentRequest(net::OK, net::HTTP_OK, std::string());
EXPECT_EQ(std::string(), *result_);
EXPECT_FALSE(network_error_);
}
} // namespace cryptauth