blob: 199f9b3204b846b0a0e31510f2da847d429d266c [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous8f79c5b2024-01-30 15:56:37 -08003#include "http/server_sent_event.hpp"
Ed Tanous41fe81c2024-09-02 15:08:41 -07004#include "http_request.hpp"
Ed Tanous8f79c5b2024-01-30 15:56:37 -08005
Ed Tanousf0b59af2024-03-20 13:38:04 -07006#include <boost/asio/buffer.hpp>
7#include <boost/asio/io_context.hpp>
Ed Tanous8d45b9c2024-04-08 17:50:10 -07008#include <boost/asio/read.hpp>
Ed Tanous8f79c5b2024-01-30 15:56:37 -08009#include <boost/beast/_experimental/test/stream.hpp>
10
Ed Tanousf0b59af2024-03-20 13:38:04 -070011#include <chrono>
Ed Tanous8f79c5b2024-01-30 15:56:37 -080012#include <memory>
13#include <string>
14#include <string_view>
Ed Tanousf0b59af2024-03-20 13:38:04 -070015#include <utility>
Ed Tanous8f79c5b2024-01-30 15:56:37 -080016
17#include "gtest/gtest.h"
18namespace crow
19{
20namespace sse_socket
21{
22
23namespace
24{
25
26TEST(ServerSentEvent, SseWorks)
27{
28 boost::asio::io_context io;
29 boost::beast::test::stream stream(io);
30 boost::beast::test::stream out(io);
31 stream.connect(out);
32
Ed Tanousf80a87f2024-06-16 12:10:33 -070033 Request req;
34
Ed Tanous8f79c5b2024-01-30 15:56:37 -080035 bool openCalled = false;
Patrick Williamsbd79bce2024-08-16 15:22:20 -040036 auto openHandler =
37 [&openCalled](Connection&, const Request& /*handedReq*/) {
38 openCalled = true;
39 };
Ed Tanous8f79c5b2024-01-30 15:56:37 -080040 bool closeCalled = false;
41 auto closeHandler = [&closeCalled](Connection&) { closeCalled = true; };
42
43 std::shared_ptr<ConnectionImpl<boost::beast::test::stream>> conn =
44 std::make_shared<ConnectionImpl<boost::beast::test::stream>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070045 std::move(stream), openHandler, closeHandler);
Ed Tanousf80a87f2024-06-16 12:10:33 -070046 conn->start(req);
Ed Tanous8f79c5b2024-01-30 15:56:37 -080047 // Connect
48 {
49 constexpr std::string_view expected =
50 "HTTP/1.1 200 OK\r\n"
51 "Content-Type: text/event-stream\r\n"
52 "\r\n";
53
Ed Tanous464924c2024-12-20 14:49:45 -080054 while (out.str().size() != expected.size() || !openCalled)
Ed Tanous8f79c5b2024-01-30 15:56:37 -080055 {
56 io.run_for(std::chrono::milliseconds(1));
57 }
58
59 std::string eventContent;
60 eventContent.resize(expected.size());
61 boost::asio::read(out, boost::asio::buffer(eventContent));
62
63 EXPECT_EQ(eventContent, expected);
64 EXPECT_TRUE(openCalled);
65 EXPECT_FALSE(closeCalled);
66 EXPECT_TRUE(out.str().empty());
67 }
68 // Send one event
69 {
Ed Tanous6d799e12024-09-11 14:33:37 -070070 conn->sendSseEvent("TestEventId", "TestEventContent");
Ed Tanous8f79c5b2024-01-30 15:56:37 -080071 std::string_view expected = "id: TestEventId\n"
72 "data: TestEventContent\n"
73 "\n";
74
75 while (out.str().size() < expected.size())
76 {
77 io.run_for(std::chrono::milliseconds(1));
78 }
79 EXPECT_EQ(out.str(), expected);
80
81 std::string eventContent;
82 eventContent.resize(expected.size());
83 boost::asio::read(out, boost::asio::buffer(eventContent));
84
85 EXPECT_EQ(eventContent, expected);
86 EXPECT_TRUE(out.str().empty());
87 }
88 // Send second event
89 {
Ed Tanous6d799e12024-09-11 14:33:37 -070090 conn->sendSseEvent("TestEventId2", "TestEvent\nContent2");
Patrick Williamsbd79bce2024-08-16 15:22:20 -040091 constexpr std::string_view expected =
92 "id: TestEventId2\n"
93 "data: TestEvent\n"
94 "data: Content2\n"
95 "\n";
Ed Tanous8f79c5b2024-01-30 15:56:37 -080096
97 while (out.str().size() < expected.size())
98 {
99 io.run_for(std::chrono::milliseconds(1));
100 }
101
102 std::string eventContent;
103 eventContent.resize(expected.size());
104 boost::asio::read(out, boost::asio::buffer(eventContent));
105 EXPECT_EQ(eventContent, expected);
106 EXPECT_TRUE(out.str().empty());
107 }
108 // close the remote
109 {
110 out.close();
111 while (!closeCalled)
112 {
113 io.run_for(std::chrono::milliseconds(1));
114 }
115 }
116}
117} // namespace
118
119} // namespace sse_socket
120} // namespace crow