aboutsummaryrefslogtreecommitdiff
path: root/zenserver/testing/httptest.cpp
blob: 18d63a6eff4b636dfab3c262e691a5c36f1304bd (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "httptest.h"

#include <zencore/compactbinarybuilder.h>
#include <zencore/compactbinarypackage.h>

namespace zen {

HttpTestingService::HttpTestingService()
{
	m_Router.RegisterRoute(
		"hello",
		[this](HttpRouterRequest& Req) { Req.ServerRequest().WriteResponse(HttpResponseCode::OK); },
		HttpVerb::kGet);

	m_Router.RegisterRoute(
		"json",
		[this](HttpRouterRequest& Req) {
			CbObjectWriter Obj;
			Obj.AddBool("ok", true);
			Obj.AddInteger("counter", ++m_Counter);
			Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Obj.Save());
		},
		HttpVerb::kGet);

	m_Router.RegisterRoute(
		"echo",
		[this](HttpRouterRequest& Req) {
			IoBuffer Body = Req.ServerRequest().ReadPayload();
			Req.ServerRequest().WriteResponse(HttpResponseCode::OK, HttpContentType::kBinary, Body);
		},
		HttpVerb::kPost);

	m_Router.RegisterRoute(
		"package",
		[this](HttpRouterRequest& Req) {
			CbPackage Pkg = Req.ServerRequest().ReadPayloadPackage();
			Req.ServerRequest().WriteResponse(HttpResponseCode::OK, Pkg);
		},
		HttpVerb::kPost);
}

HttpTestingService::~HttpTestingService()
{
}

const char*
HttpTestingService::BaseUri() const
{
	return "/testing/";
}

void
HttpTestingService::HandleRequest(HttpServerRequest& Request)
{
	m_Router.HandleRequest(Request);
}

Ref<IHttpPackageHandler>
HttpTestingService::HandlePackageRequest(HttpServerRequest& HttpServiceRequest)
{
	RwLock::ExclusiveLockScope _(m_RwLock);

	const uint32_t RequestId = HttpServiceRequest.RequestId();

	if (auto It = m_HandlerMap.find(RequestId); It != m_HandlerMap.end())
	{
		Ref<HttpTestingService::PackageHandler> Handler = std::move(It->second);

		m_HandlerMap.erase(It);

		return Handler.Get();
	}

	auto InsertResult = m_HandlerMap.insert({RequestId, nullptr});

	_.ReleaseNow();

	return (InsertResult.first->second = new PackageHandler(*this, RequestId)).Get();
}

//////////////////////////////////////////////////////////////////////////

HttpTestingService::PackageHandler::PackageHandler(HttpTestingService& Svc, uint32_t RequestId) : m_Svc(Svc), m_RequestId(RequestId)
{
}

HttpTestingService::PackageHandler::~PackageHandler()
{
}

void
HttpTestingService::PackageHandler::FilterOffer(std::vector<IoHash>& OfferCids)
{
	ZEN_UNUSED(OfferCids);
	// No-op
	return;
}
void
HttpTestingService::PackageHandler::OnRequestBegin()
{
}

void
HttpTestingService::PackageHandler::OnRequestComplete()
{
}

IoBuffer
HttpTestingService::PackageHandler::CreateTarget(const IoHash& Cid, uint64_t StorageSize)
{
	ZEN_UNUSED(Cid);
	return IoBuffer{StorageSize};
}

}  // namespace zen