-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathHttpResponseDecoderTests.cpp
107 lines (90 loc) · 4.4 KB
/
HttpResponseDecoderTests.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
// Copyright (c) Microsoft Corporation. All rights reserved.
#include "common/Common.hpp"
#include "common/MockIHttpClient.hpp"
#include "http/HttpResponseDecoder.hpp"
#include "config/RuntimeConfig_Default.hpp"
#include <atomic>
using namespace testing;
using namespace MAT;
class HttpResponseDecoderTests : public StrictMock<Test> {
protected:
ILogConfiguration logConfig;
RuntimeConfig_Default config;
HttpResponseDecoder decoder;
RouteSink<HttpResponseDecoderTests, EventsUploadContextPtr const&> eventsAccepted{this, &HttpResponseDecoderTests::resultEventsAccepted};
RouteSink<HttpResponseDecoderTests, EventsUploadContextPtr const&> eventsRejected{this, &HttpResponseDecoderTests::resultEventsRejected};
RouteSink<HttpResponseDecoderTests, EventsUploadContextPtr const&> temporaryNetworkFailure{this, &HttpResponseDecoderTests::resultTemporaryNetworkFailure};
RouteSink<HttpResponseDecoderTests, EventsUploadContextPtr const&> temporaryServerFailure{this, &HttpResponseDecoderTests::resultTemporaryServerFailure};
RouteSink<HttpResponseDecoderTests, EventsUploadContextPtr const&> requestAborted{this, &HttpResponseDecoderTests::resultRequestAborted};
protected:
HttpResponseDecoderTests() :
config(logConfig),
decoder(testing::getSystem())
{
decoder.eventsAccepted >> eventsAccepted;
decoder.eventsRejected >> eventsRejected;
decoder.temporaryNetworkFailure >> temporaryNetworkFailure;
decoder.temporaryServerFailure >> temporaryServerFailure;
decoder.requestAborted >> requestAborted;
}
MOCK_METHOD1(resultEventsAccepted, void(EventsUploadContextPtr const &));
MOCK_METHOD1(resultEventsRejected, void(EventsUploadContextPtr const &));
MOCK_METHOD1(resultTemporaryNetworkFailure, void(EventsUploadContextPtr const &));
MOCK_METHOD1(resultTemporaryServerFailure, void(EventsUploadContextPtr const &));
MOCK_METHOD1(resultRequestAborted, void(EventsUploadContextPtr const &));
std::atomic<unsigned> reqId{0};
std::shared_ptr<EventsUploadContext> createContextWith(HttpResult result, int status, std::string const& body)
{
SimpleHttpResponse* rsp = (new SimpleHttpResponse("HttpResponseDecoderTests"));
rsp->m_result = result;
rsp->m_statusCode = status;
rsp->m_body.assign(reinterpret_cast<uint8_t const*>(body.data()), reinterpret_cast<uint8_t const*>(body.data()) + body.size());
auto ctx = std::make_shared<EventsUploadContext>();
ctx->httpRequest = new SimpleHttpRequest(std::to_string(reqId++));
ctx->httpRequestId = ctx->httpRequest->GetId();
ctx->httpResponse = (rsp);
ctx->durationMs = 1234;
return ctx;
}
};
TEST_F(HttpResponseDecoderTests, AcceptsAccepted)
{
auto ctx = createContextWith(HttpResult_OK, 200, "");
EXPECT_CALL(*this, resultEventsAccepted(ctx)).WillOnce(Return());
decoder.decode(ctx);
}
TEST_F(HttpResponseDecoderTests, RejectsRejected)
{
auto ctx = createContextWith(HttpResult_OK, 404, "<h1>Service not found</h1>");
EXPECT_CALL(*this, resultEventsRejected(ctx)).WillOnce(Return());
decoder.decode(ctx);
}
TEST_F(HttpResponseDecoderTests, UnderstandsTemporaryServerFailures)
{
auto ctx = createContextWith(HttpResult_OK, 500, "{error:500,detail:\"Bad karma\"}");
EXPECT_CALL(*this, resultTemporaryServerFailure(ctx))
.WillOnce(Return());
decoder.decode(ctx);
ctx = createContextWith(HttpResult_OK, 408, "Timeout");
EXPECT_CALL(*this, resultTemporaryServerFailure(ctx))
.WillOnce(Return());
decoder.decode(ctx);
}
TEST_F(HttpResponseDecoderTests, UnderstandsTemporaryNetworkFailures)
{
auto ctx = createContextWith(HttpResult_LocalFailure, -1, "");
EXPECT_CALL(*this, resultTemporaryNetworkFailure(ctx))
.WillOnce(Return());
decoder.decode(ctx);
ctx = createContextWith(HttpResult_NetworkFailure, -1, "");
EXPECT_CALL(*this, resultTemporaryNetworkFailure(ctx))
.WillOnce(Return());
decoder.decode(ctx);
}
TEST_F(HttpResponseDecoderTests, SkipsAbortedRequests)
{
auto ctx = createContextWith(HttpResult_Aborted, -1, "");
EXPECT_CALL(*this, resultRequestAborted(ctx))
.WillOnce(Return());
decoder.decode(ctx);
}