Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 1 | #include "http/server_sent_event.hpp" |
Ed Tanous | 41fe81c | 2024-09-02 15:08:41 -0700 | [diff] [blame] | 2 | #include "http_request.hpp" |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 3 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 4 | #include <boost/asio/buffer.hpp> |
| 5 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 8d45b9c | 2024-04-08 17:50:10 -0700 | [diff] [blame] | 6 | #include <boost/asio/read.hpp> |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 7 | #include <boost/beast/_experimental/test/stream.hpp> |
| 8 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 9 | #include <chrono> |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <string_view> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 13 | #include <utility> |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 14 | |
| 15 | #include "gtest/gtest.h" |
| 16 | namespace crow |
| 17 | { |
| 18 | namespace sse_socket |
| 19 | { |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | TEST(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 Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame] | 31 | Request req; |
| 32 | |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 33 | bool openCalled = false; |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 34 | auto openHandler = |
| 35 | [&openCalled](Connection&, const Request& /*handedReq*/) { |
| 36 | openCalled = true; |
| 37 | }; |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 38 | 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 Tanous | 93cf0ac | 2024-03-28 00:35:13 -0700 | [diff] [blame] | 43 | std::move(stream), openHandler, closeHandler); |
Ed Tanous | f80a87f | 2024-06-16 12:10:33 -0700 | [diff] [blame] | 44 | conn->start(req); |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 45 | // 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 | { |
Ed Tanous | 6d799e1 | 2024-09-11 14:33:37 -0700 | [diff] [blame] | 68 | conn->sendSseEvent("TestEventId", "TestEventContent"); |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 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 | { |
Ed Tanous | 6d799e1 | 2024-09-11 14:33:37 -0700 | [diff] [blame] | 88 | conn->sendSseEvent("TestEventId2", "TestEvent\nContent2"); |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 89 | constexpr std::string_view expected = |
| 90 | "id: TestEventId2\n" |
| 91 | "data: TestEvent\n" |
| 92 | "data: Content2\n" |
| 93 | "\n"; |
Ed Tanous | 8f79c5b | 2024-01-30 15:56:37 -0800 | [diff] [blame] | 94 | |
| 95 | while (out.str().size() < expected.size()) |
| 96 | { |
| 97 | io.run_for(std::chrono::milliseconds(1)); |
| 98 | } |
| 99 | |
| 100 | std::string eventContent; |
| 101 | eventContent.resize(expected.size()); |
| 102 | boost::asio::read(out, boost::asio::buffer(eventContent)); |
| 103 | EXPECT_EQ(eventContent, expected); |
| 104 | EXPECT_TRUE(out.str().empty()); |
| 105 | } |
| 106 | // close the remote |
| 107 | { |
| 108 | out.close(); |
| 109 | while (!closeCalled) |
| 110 | { |
| 111 | io.run_for(std::chrono::milliseconds(1)); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } // namespace |
| 116 | |
| 117 | } // namespace sse_socket |
| 118 | } // namespace crow |