-
Notifications
You must be signed in to change notification settings - Fork 55
/
HttpRequestEncoderTests.cpp
128 lines (100 loc) · 4.34 KB
/
HttpRequestEncoderTests.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
// Copyright (c) Microsoft Corporation. All rights reserved.
#include "common/Common.hpp"
#include "common/MockIHttpClient.hpp"
#include "http/HttpRequestEncoder.hpp"
#include "config/RuntimeConfig_Default.hpp"
using namespace testing;
using namespace MAT;
class MockHttpRequestEncoder : public HttpRequestEncoder
{
public:
MockHttpRequestEncoder(ITelemetrySystem& system, IHttpClient& httpClient)
: HttpRequestEncoder(system, httpClient) { }
using HttpRequestEncoder::handleEncode;
void DispatchDataViewerEvent(const StorageBlob& packet)
{
dataPacket = packet;
}
StorageBlob dataPacket;
};
class HttpRequestEncoderTests : public Test {
public:
protected:
ITelemetrySystem & system;
MockIHttpClient mockHttpClient;
HttpRequestEncoder encoder;
protected:
HttpRequestEncoderTests()
: system(testing::getSystem()),
encoder(system, mockHttpClient)
{
EXPECT_CALL(mockHttpClient, CreateRequest())
.WillRepeatedly(Invoke(&HttpRequestEncoderTests::createHttpRequest));
}
static IHttpRequest* createHttpRequest()
{
return new SimpleHttpRequest("HttpRequestEncoderTests");
}
};
TEST_F(HttpRequestEncoderTests, SetsAllParameters)
{
EventsUploadContextPtr ctx = std::make_shared<EventsUploadContext>();
ctx->compressed = false;
ctx->body = { 1, 127, 255 };
ctx->packageIds["tenant1-token"] = 0;
ctx->latency = EventLatency_RealTime;
encoder.encode(ctx);
ASSERT_THAT(ctx->httpRequestId, Eq("HttpRequestEncoderTests"));
SimpleHttpRequest const* req = static_cast<SimpleHttpRequest*>(ctx->httpRequest);
EXPECT_THAT(req->m_id, Eq("HttpRequestEncoderTests"));
EXPECT_THAT(req->m_method, Eq("POST"));
EXPECT_THAT(req->m_url, Eq("https://mobile.events.data.microsoft.com/OneCollector/1.0/"));
EXPECT_THAT(req->m_headers, Contains(Pair("Expect", "100-continue")));
EXPECT_THAT(req->m_headers, Contains(Pair("SDK-Version", PAL::getSdkVersion())));
EXPECT_THAT(req->m_headers, Contains(Pair("Client-Id", "NO_AUTH")));
EXPECT_THAT(req->m_headers, Contains(Pair("Content-Type", "application/bond-compact-binary")));
EXPECT_THAT(req->m_headers, Contains(Pair("APIKey", "tenant1-token")));
EXPECT_THAT(req->m_body, Eq(std::vector<uint8_t>{1, 127, 255}));
EXPECT_THAT(req->m_latency, Eq(EventLatency_RealTime));
}
TEST_F(HttpRequestEncoderTests, AddsCompressionHeader)
{
EventsUploadContextPtr ctx = std::make_shared<EventsUploadContext>();
ctx->compressed = false;
encoder.encode(ctx);
ASSERT_THAT(ctx->httpRequestId, Eq("HttpRequestEncoderTests"));
SimpleHttpRequest const* req = static_cast<SimpleHttpRequest*>(ctx->httpRequest);
EXPECT_THAT(req->m_headers, Not(Contains(Pair("Content-Encoding", "deflate"))));
ctx->compressed = true;
encoder.encode(ctx);
ASSERT_THAT(ctx->httpRequestId, Eq("HttpRequestEncoderTests"));
req = static_cast<SimpleHttpRequest*>(ctx->httpRequest);
EXPECT_THAT(req->m_headers, Contains(Pair("Content-Encoding", "deflate")));
}
TEST_F(HttpRequestEncoderTests, BuildsApiKeyCorrectly)
{
EventsUploadContextPtr ctx = std::make_shared<EventsUploadContext>();
encoder.encode(ctx);
ASSERT_THAT(ctx->httpRequestId, Eq("HttpRequestEncoderTests"));
SimpleHttpRequest const* req = static_cast<SimpleHttpRequest*>(ctx->httpRequest);
EXPECT_THAT(req->m_headers, Contains(Pair("APIKey", "")));
ctx->packageIds["tenant1-token"] = 0;
encoder.encode(ctx);
ASSERT_THAT(ctx->httpRequestId, Eq("HttpRequestEncoderTests"));
req = static_cast<SimpleHttpRequest*>(ctx->httpRequest);
EXPECT_THAT(req->m_headers, Contains(Pair("APIKey", "tenant1-token")));
ctx->packageIds["tenant2-token"] = 1;
ctx->packageIds["tenant3-token"] = 2;
encoder.encode(ctx);
ASSERT_THAT(ctx->httpRequestId, Eq("HttpRequestEncoderTests"));
req = static_cast<SimpleHttpRequest*>(ctx->httpRequest);
EXPECT_THAT(req->m_headers, Contains(Pair("APIKey", "tenant1-token,tenant2-token,tenant3-token")));
}
TEST_F(HttpRequestEncoderTests, DispatchDataViewerEventCorrectly)
{
EventsUploadContextPtr ctx = std::make_shared<EventsUploadContext>();
ctx->body = { 1, 127, 255 };
MockHttpRequestEncoder mockEncoder(system, mockHttpClient);
mockEncoder.encode(ctx);
EXPECT_THAT(mockEncoder.dataPacket, Eq(std::vector<uint8_t>{1, 127, 255}));
}