blob: d8d88365053a28afaa4f87561a95eedbe56d8e29 [file] [log] [blame]
Ed Tanous95c63072024-03-26 13:19:52 -07001#include "boost_formatters.hpp"
Ed Tanous8f79c5b2024-01-30 15:56:37 -08002#include "http/server_sent_event.hpp"
3
Ed Tanousf0b59af2024-03-20 13:38:04 -07004#include <boost/asio/buffer.hpp>
5#include <boost/asio/io_context.hpp>
Ed Tanous8d45b9c2024-04-08 17:50:10 -07006#include <boost/asio/read.hpp>
Ed Tanous8f79c5b2024-01-30 15:56:37 -08007#include <boost/beast/_experimental/test/stream.hpp>
8
Ed Tanousf0b59af2024-03-20 13:38:04 -07009#include <chrono>
Ed Tanous8f79c5b2024-01-30 15:56:37 -080010#include <memory>
11#include <string>
12#include <string_view>
Ed Tanousf0b59af2024-03-20 13:38:04 -070013#include <utility>
Ed Tanous8f79c5b2024-01-30 15:56:37 -080014
15#include "gtest/gtest.h"
16namespace crow
17{
18namespace sse_socket
19{
20
21namespace
22{
23
24TEST(ServerSentEvent, SseWorks)
25{
26 boost::asio::io_context io;
27 boost::beast::test::stream stream(io);
28 boost::beast::test::stream out(io);
29 stream.connect(out);
30
Ed Tanousf80a87f2024-06-16 12:10:33 -070031 Request req;
32
Ed Tanous8f79c5b2024-01-30 15:56:37 -080033 bool openCalled = false;
Ed Tanousf80a87f2024-06-16 12:10:33 -070034 auto openHandler = [&openCalled](Connection&,
35 const Request& /*handedReq*/) {
36 openCalled = true;
37 };
Ed Tanous8f79c5b2024-01-30 15:56:37 -080038 bool closeCalled = false;
39 auto closeHandler = [&closeCalled](Connection&) { closeCalled = true; };
40
41 std::shared_ptr<ConnectionImpl<boost::beast::test::stream>> conn =
42 std::make_shared<ConnectionImpl<boost::beast::test::stream>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070043 std::move(stream), openHandler, closeHandler);
Ed Tanousf80a87f2024-06-16 12:10:33 -070044 conn->start(req);
Ed Tanous8f79c5b2024-01-30 15:56:37 -080045 // Connect
46 {
47 constexpr std::string_view expected =
48 "HTTP/1.1 200 OK\r\n"
49 "Content-Type: text/event-stream\r\n"
50 "\r\n";
51
52 while (out.str().size() != expected.size())
53 {
54 io.run_for(std::chrono::milliseconds(1));
55 }
56
57 std::string eventContent;
58 eventContent.resize(expected.size());
59 boost::asio::read(out, boost::asio::buffer(eventContent));
60
61 EXPECT_EQ(eventContent, expected);
62 EXPECT_TRUE(openCalled);
63 EXPECT_FALSE(closeCalled);
64 EXPECT_TRUE(out.str().empty());
65 }
66 // Send one event
67 {
68 conn->sendEvent("TestEventId", "TestEventContent");
69 std::string_view expected = "id: TestEventId\n"
70 "data: TestEventContent\n"
71 "\n";
72
73 while (out.str().size() < expected.size())
74 {
75 io.run_for(std::chrono::milliseconds(1));
76 }
77 EXPECT_EQ(out.str(), expected);
78
79 std::string eventContent;
80 eventContent.resize(expected.size());
81 boost::asio::read(out, boost::asio::buffer(eventContent));
82
83 EXPECT_EQ(eventContent, expected);
84 EXPECT_TRUE(out.str().empty());
85 }
86 // Send second event
87 {
88 conn->sendEvent("TestEventId2", "TestEvent\nContent2");
89 constexpr std::string_view expected = "id: TestEventId2\n"
90 "data: TestEvent\n"
91 "data: Content2\n"
92 "\n";
93
94 while (out.str().size() < expected.size())
95 {
96 io.run_for(std::chrono::milliseconds(1));
97 }
98
99 std::string eventContent;
100 eventContent.resize(expected.size());
101 boost::asio::read(out, boost::asio::buffer(eventContent));
102 EXPECT_EQ(eventContent, expected);
103 EXPECT_TRUE(out.str().empty());
104 }
105 // close the remote
106 {
107 out.close();
108 while (!closeCalled)
109 {
110 io.run_for(std::chrono::milliseconds(1));
111 }
112 }
113}
114} // namespace
115
116} // namespace sse_socket
117} // namespace crow