aboutsummaryrefslogtreecommitdiff
path: root/zenserver/testing
diff options
context:
space:
mode:
authorPer Larsson <[email protected]>2022-02-21 15:14:11 +0100
committerPer Larsson <[email protected]>2022-02-21 15:14:11 +0100
commitdb1c9605e3afbaf86f4231ba4eb7976d896f286b (patch)
tree54b451da4247c69575ff1a05ed006ecef3905c85 /zenserver/testing
parentIf open(O_CREAT) is used then a file mode must be given (diff)
parentRemoved optional offset for GetView. (diff)
downloadzen-db1c9605e3afbaf86f4231ba4eb7976d896f286b.tar.xz
zen-db1c9605e3afbaf86f4231ba4eb7976d896f286b.zip
Initial support for websockets.
Diffstat (limited to 'zenserver/testing')
-rw-r--r--zenserver/testing/httptest.cpp34
-rw-r--r--zenserver/testing/httptest.h6
2 files changed, 39 insertions, 1 deletions
diff --git a/zenserver/testing/httptest.cpp b/zenserver/testing/httptest.cpp
index 230d5d6c5..10b69c469 100644
--- a/zenserver/testing/httptest.cpp
+++ b/zenserver/testing/httptest.cpp
@@ -8,6 +8,8 @@
namespace zen {
+using namespace std::literals;
+
HttpTestingService::HttpTestingService()
{
m_Router.RegisterRoute(
@@ -136,6 +138,38 @@ HttpTestingService::HandlePackageRequest(HttpServerRequest& HttpServiceRequest)
return (InsertResult.first->second = new PackageHandler(*this, RequestId)).Get();
}
+void
+HttpTestingService::RegisterHandlers(WebSocketServer& Server)
+{
+ Server.RegisterRequestHandler("SayHello"sv, *this);
+}
+
+bool
+HttpTestingService::HandleRequest(const WebSocketMessage& RequestMsg)
+{
+ CbObjectView Request = RequestMsg.Body().GetObject();
+
+ std::string_view Method = Request["Method"].AsString();
+
+ if (Method != "SayHello"sv)
+ {
+ return false;
+ }
+
+ CbObjectWriter Response;
+ Response.AddString("Result"sv, "Hello Friend!!");
+
+ WebSocketMessage ResponseMsg;
+ ResponseMsg.SetMessageType(WebSocketMessageType::kResponse);
+ ResponseMsg.SetCorrelationId(RequestMsg.CorrelationId());
+ ResponseMsg.SetSocketId(RequestMsg.SocketId());
+ ResponseMsg.SetBody(Response.Save());
+
+ SocketServer().SendResponse(std::move(ResponseMsg));
+
+ return true;
+}
+
//////////////////////////////////////////////////////////////////////////
HttpTestingService::PackageHandler::PackageHandler(HttpTestingService& Svc, uint32_t RequestId) : m_Svc(Svc), m_RequestId(RequestId)
diff --git a/zenserver/testing/httptest.h b/zenserver/testing/httptest.h
index f7ea0c31c..57d2d63f3 100644
--- a/zenserver/testing/httptest.h
+++ b/zenserver/testing/httptest.h
@@ -5,6 +5,7 @@
#include <zencore/logging.h>
#include <zencore/stats.h>
#include <zenhttp/httpserver.h>
+#include <zenhttp/websocket.h>
#include <atomic>
@@ -13,7 +14,7 @@ namespace zen {
/**
* Test service to facilitate testing the HTTP framework and client interactions
*/
-class HttpTestingService : public HttpService
+class HttpTestingService : public HttpService, public WebSocketService
{
public:
HttpTestingService();
@@ -40,6 +41,9 @@ public:
};
private:
+ virtual void RegisterHandlers(WebSocketServer& Server) override;
+ virtual bool HandleRequest(const WebSocketMessage& Request) override;
+
HttpRequestRouter m_Router;
std::atomic<uint32_t> m_Counter{0};
metrics::OperationTiming m_TimingStats;