forked from microsoft/cpp_client_telemetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpClientCAPITests.cpp
234 lines (195 loc) · 7.42 KB
/
HttpClientCAPITests.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#include "common/Common.hpp"
#include "http/HttpClient_CAPI.hpp"
#include "mat.h"
using namespace testing;
using namespace MAT;
using std::string;
namespace
{
class TestHttpResponseCallback : public IHttpResponseCallback
{
public:
void SetResponseValidation(std::function<void(IHttpResponse*)> fn) { m_validateFn = fn; }
virtual void OnHttpResponse(IHttpResponse* response) override
{
if (m_validateFn)
m_validateFn(response);
}
private:
std::function<void(IHttpResponse*)> m_validateFn;
};
class TestHelper {
public:
void SetShouldSend(bool shouldSend) { m_shouldSend = shouldSend; }
bool ShouldSend() { return m_shouldSend; }
void SetSendValidation(std::function<void(http_request_t*)> fn) { m_validateSendFn = fn; }
void SetCancelValidation(std::function<void(const char*)> fn) { m_validateCancelFn = fn; }
void OnSend(http_request_t* request)
{
if (m_validateSendFn)
m_validateSendFn(request);
}
void OnCancel(const char* requestId)
{
if (m_validateCancelFn)
m_validateCancelFn(requestId);
}
private:
std::function<void(http_request_t*)> m_validateSendFn;
std::function<void(const char*)> m_validateCancelFn;
bool m_shouldSend = false;
};
static std::unique_ptr<TestHelper> s_testHelper;
// RAII helper that automatically uninstalls static TestHelper instance upon destruction
class AutoTestHelper {
public:
AutoTestHelper()
{
s_testHelper.reset(new TestHelper());
}
~AutoTestHelper()
{
s_testHelper = nullptr;
}
TestHelper* operator->()
{
return s_testHelper.get();
}
};
} // namespace
void EVTSDK_LIBABI_CDECL OnHttpSend(http_request_t* request, http_complete_fn_t callback)
{
s_testHelper->OnSend(request);
if (s_testHelper->ShouldSend())
{
// Construct simple test response
uint8_t body[] = {'y', 'e', 's'};
http_header_t header;
header.name = "response_key1";
header.value = "response_value1";
http_response_t response;
response.statusCode = 200;
response.body = body;
response.bodySize = 3;
response.headers = &header;
response.headersCount = 1;
callback(request->id, HTTP_RESULT_OK, &response);
}
}
void EVTSDK_LIBABI_CDECL OnHttpCancel(const char* requestId)
{
s_testHelper->OnCancel(requestId);
}
TEST(HttpClientCAPITests, SendAsync)
{
HttpClient_CAPI httpClient(&OnHttpSend, &OnHttpCancel);
// Build request
std::vector<uint8_t> body = {'a', 'b', 'c'};
auto request = httpClient.CreateRequest();
request->SetUrl("https://www.microsoft.com");
request->SetBody(body);
request->SetMethod("POST");
request->GetHeaders().add("key1", "value1");
request->GetHeaders().add("key2", "value2");
AutoTestHelper testHelper;
testHelper->SetShouldSend(true);
// Validate C++ -> C transformation of request
bool wasSent = false;
testHelper->SetSendValidation([&wasSent](http_request_t* request) {
wasSent = true;
EXPECT_EQ(request->type, HTTP_REQUEST_TYPE_POST);
EXPECT_EQ(string(request->url), string("https://www.microsoft.com"));
EXPECT_EQ(request->bodySize, 3);
EXPECT_EQ(request->body[0], 'a');
EXPECT_EQ(request->body[1], 'b');
EXPECT_EQ(request->body[2], 'c');
EXPECT_EQ(request->headersCount, 2);
EXPECT_EQ(string(request->headers[0].name), string("key1"));
EXPECT_EQ(string(request->headers[0].value), string("value1"));
EXPECT_EQ(string(request->headers[1].name), string("key2"));
EXPECT_EQ(string(request->headers[1].value), string("value2"));
});
// Validate C -> C++ transformation of response
bool wasReceived = false;
TestHttpResponseCallback responseCallback;
responseCallback.SetResponseValidation([&wasReceived](IHttpResponse* response) {
wasReceived = true;
EXPECT_EQ(response->GetResult(), HttpResult_OK);
EXPECT_EQ(response->GetBody().size(), size_t { 3 });
EXPECT_EQ(response->GetBody()[0], 'y');
EXPECT_EQ(response->GetBody()[1], 'e');
EXPECT_EQ(response->GetBody()[2], 's');
EXPECT_EQ(response->GetHeaders().size(), size_t { 1 });
EXPECT_EQ(response->GetHeaders().has("response_key1"), true);
EXPECT_EQ(response->GetHeaders().get("response_key1"), string("response_value1"));
});
httpClient.SendRequestAsync(request, &responseCallback);
EXPECT_EQ(wasSent, true);
EXPECT_EQ(wasReceived, true);
}
TEST(HttpClientCAPITests, Cancel)
{
HttpClient_CAPI httpClient(&OnHttpSend, &OnHttpCancel);
// Build request
auto request = httpClient.CreateRequest();
request->SetUrl("https://www.microsoft.com");
request->SetMethod("GET");
AutoTestHelper testHelper;
testHelper->SetShouldSend(false);
string cancelledId;
testHelper->SetCancelValidation([&cancelledId](const char* requestId) {
cancelledId = requestId;
});
TestHttpResponseCallback responseCallback;
responseCallback.SetResponseValidation([](IHttpResponse* /*response*/) {
FAIL() << "No response should have been received";
});
httpClient.SendRequestAsync(request, &responseCallback);
httpClient.CancelRequestAsync(request->GetId());
EXPECT_EQ(cancelledId, request->GetId());
}
TEST(HttpClientCAPITests, CancelAllThenSend)
{
HttpClient_CAPI httpClient(&OnHttpSend, &OnHttpCancel);
AutoTestHelper testHelper;
testHelper->SetShouldSend(true);
// Cancel all requests (none pending)
httpClient.CancelAllRequests();
// Build request
auto request = httpClient.CreateRequest();
request->SetUrl("https://www.microsoft.com");
request->SetMethod("GET");
request->GetHeaders().add("key1", "value1");
// Validate C++ -> C transformation of request
bool wasSent = false;
testHelper->SetSendValidation([&wasSent](http_request_t* request) {
wasSent = true;
EXPECT_EQ(request->type, HTTP_REQUEST_TYPE_GET);
EXPECT_EQ(request->url, string("https://www.microsoft.com"));
EXPECT_EQ(request->bodySize, 0);
EXPECT_EQ(request->headersCount, 1);
EXPECT_EQ(string(request->headers[0].name), string("key1"));
EXPECT_EQ(string(request->headers[0].value), string("value1"));
});
// Validate C -> C++ transformation of response
bool wasReceived = false;
TestHttpResponseCallback responseCallback;
responseCallback.SetResponseValidation([&wasReceived](IHttpResponse* response) {
wasReceived = true;
EXPECT_EQ(response->GetResult(), HttpResult_OK);
EXPECT_EQ(response->GetBody().size(), size_t { 3 });
EXPECT_EQ(response->GetBody()[0], 'y');
EXPECT_EQ(response->GetBody()[1], 'e');
EXPECT_EQ(response->GetBody()[2], 's');
EXPECT_EQ(response->GetHeaders().size(), size_t { 1 });
EXPECT_EQ(response->GetHeaders().has("response_key1"), true);
EXPECT_EQ(response->GetHeaders().get("response_key1"), string("response_value1"));
});
httpClient.SendRequestAsync(request, &responseCallback);
EXPECT_EQ(wasSent, true);
EXPECT_EQ(wasReceived, true);
}