rspec-sse-matchers 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bcc165ce931a68f9f7a0be4ae3122a1aee5829293555b08c61f71c9d3e4e037b
4
+ data.tar.gz: 4ff64228691c17c84bd3efd723048d0b9a8331bbfcc317d4115bfffdcc53114d
5
+ SHA512:
6
+ metadata.gz: 955ce4158e46bedc2ba2241eeaf094d97e4422a159d0f59f45628ff36307c64521288d573a5e3daa50682c449c8cd746134d66d6ac92ab5ce31edb6406bc925f
7
+ data.tar.gz: 95eb00670ad06584b248f2de758010a46a96d952e8c5590a50516e8d98ca2e54e63b9cdf39b24219a65515d9be23d2057556cd10084d0c080e4464509d053aea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.0.1] - 2025-05-08
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2025 Taiki Kawakami (a.k.a moznion) https://moznion.net
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,228 @@
1
+ # RSpec SSE Matchers
2
+
3
+ A collection of [RSpec](https://rspec.info/) matchers for testing [Server-Sent Events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events).
4
+
5
+ [![Tests](https://github.com/moznion/rspec-sse-matchers/actions/workflows/test.yml/badge.svg)](https://github.com/moznion/rspec-sse-matchers/actions/workflows/test.yml)
6
+ [![Gem Version](https://badge.fury.io/rb/rspec-sse-matchers.svg)](https://badge.fury.io/rb/rspec-sse-matchers)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'rspec-sse-matchers'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```bash
19
+ $ bundle install
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```bash
25
+ $ gem install rspec-sse-matchers
26
+ ```
27
+
28
+ ## Synopsis
29
+
30
+ This gem provides a set of matchers to test SSE responses in your RSpec request specs:
31
+
32
+ ```ruby
33
+ # In your controller
34
+ def index
35
+ response.headers['Cache-Control'] = 'no-store'
36
+ response.headers['Content-Type'] = 'text/event-stream'
37
+ sse = SSE.new(response.stream)
38
+
39
+ sse.write({id: 1, message: 'Hello'}, event: 'message', id: 1)
40
+ sse.write({id: 2, message: 'World'}, event: 'update', id: 2)
41
+ sse.close
42
+ end
43
+
44
+ # In your spec
45
+ RSpec.describe 'SSE endpoint', type: :request do
46
+ it 'sends the expected events' do
47
+ get '/events', headers: { 'Accept' => 'text/event-stream' }
48
+
49
+ # Verify the response indicates a successful SSE connection
50
+ expect(response).to be_successfully_opened
51
+
52
+ # Verify the event types
53
+ expect(response).to be_event_types(['message', 'update'])
54
+
55
+ # Verify that the response is properly closed
56
+ expect(response).to be_gracefully_closed
57
+ end
58
+ end
59
+ ```
60
+
61
+ ### Available Matchers
62
+
63
+ #### Exact Order Matchers
64
+
65
+ These matchers check that the specified values appear in the exact order:
66
+
67
+ - `be_events`: Check that the events exactly match the expected events
68
+ - `be_event_types`: Check that the event types exactly match the expected types
69
+ - `be_event_data`: Check that the event data exactly match the expected data
70
+ - `be_event_ids`: Check that the event IDs exactly match the expected IDs
71
+ - `be_reconnection_times`: Check that the reconnection times exactly match the expected times
72
+
73
+ All event data matchers (`be_event_data`, `contain_exactly_event_data`, `have_event_data`) and event matchers (`be_events`, `contain_exactly_events`, `have_events`) accept a `json: true` option that will parse the JSON in event data for comparison.
74
+
75
+ #### Order-Independent Matchers
76
+
77
+ These matchers check that the specified values appear in any order:
78
+
79
+ - `contain_exactly_events`: Check that the events match the expected events regardless of order
80
+ - `contain_exactly_event_types`: Check that the event types match the expected types regardless of order
81
+ - `contain_exactly_event_data`: Check that the event data match the expected data regardless of order
82
+ - `contain_exactly_event_ids`: Check that the event IDs match the expected IDs regardless of order
83
+ - `contain_exactly_reconnection_times`: Check that the reconnection times match the expected times regardless of order
84
+
85
+ #### Inclusion Matchers
86
+
87
+ These matchers check that all the expected values are included:
88
+
89
+ - `have_events`: Check that all the expected events are included
90
+ - `have_event_types`: Check that all the expected event types are included
91
+ - `have_event_data`: Check that all the expected event data are included
92
+ - `have_event_ids`: Check that all the expected event IDs are included
93
+ - `have_reconnection_times`: Check that all the expected reconnection times are included
94
+
95
+ #### Miscellaneous Matchers
96
+
97
+ - `be_successfully_opened`: Check that the response indicates the SSE connection has been opened successfully
98
+ - `be_gracefully_closed`: Check that the response body ends with "\n\n" (indicating proper SSE close)
99
+
100
+ ### Argument Formats
101
+
102
+ All matchers can accept their arguments either as individual arguments or as an array:
103
+
104
+ ```ruby
105
+ # These are equivalent:
106
+ expect(response).to have_event_types('message', 'update', 'close')
107
+ expect(response).to have_event_types(['message', 'update', 'close'])
108
+ ```
109
+
110
+ ### JSON Parsing Option
111
+
112
+ Event data matchers and event matchers accept a `json: true` option that automatically parses JSON in event `data` for comparison:
113
+
114
+ ```ruby
115
+ # Without JSON parsing (string comparison)
116
+ expect(response).to be_event_data(['{"id":1,"name":"Alice"}', '{"id":2,"name":"Bob"}'])
117
+
118
+ # With JSON parsing (object comparison)
119
+ expect(response).to be_event_data([{"id" => 1, "name" => "Alice"}, {"id" => 2, "name" => "Bob"}], json: true)
120
+
121
+ # This also works with event matchers
122
+ expect(response).to be_events([
123
+ { type: 'message', data: {"id" => 1, "name" => "Alice"}, id: '1' },
124
+ { type: 'update', data: {"id" => 2, "name" => "Bob"}, id: '2' }
125
+ ], json: true)
126
+ ```
127
+
128
+ When the `json: true` option is enabled, the matcher attempts to parse each event's `data` as JSON. If parsing fails (the data is not valid JSON), it raises an error.
129
+
130
+ ## Examples
131
+
132
+ ### Testing Event Types
133
+
134
+ ```ruby
135
+ # Exact order
136
+ expect(response).to be_event_types(['message', 'update', 'close'])
137
+
138
+ # Any order
139
+ expect(response).to contain_exactly_event_types(['update', 'message', 'close'])
140
+
141
+ # Inclusion
142
+ expect(response).to have_event_types(['message', 'update'])
143
+ ```
144
+
145
+ ### Testing Event Data
146
+
147
+ ```ruby
148
+ # Exact order
149
+ expect(response).to be_event_data(['{"id":1}', '{"id":2}', '{"id":3}'])
150
+
151
+ # Any order
152
+ expect(response).to contain_exactly_event_data(['{"id":2}', '{"id":1}', '{"id":3}'])
153
+
154
+ # Inclusion
155
+ expect(response).to have_event_data(['{"id":1}', '{"id":2}'])
156
+
157
+ # With JSON parsing
158
+ expect(response).to be_event_data([{"id" => 1}, {"id" => 2}, {"id" => 3}], json: true)
159
+ ```
160
+
161
+ ### Testing Event IDs
162
+
163
+ ```ruby
164
+ # Exact order
165
+ expect(response).to be_event_ids(['1', '2', '3'])
166
+
167
+ # Any order
168
+ expect(response).to contain_exactly_event_ids(['2', '1', '3'])
169
+
170
+ # Inclusion
171
+ expect(response).to have_event_ids(['1', '2'])
172
+ ```
173
+
174
+ ### Testing Reconnection Times
175
+
176
+ ```ruby
177
+ # Exact order
178
+ expect(response).to be_reconnection_times([1000, 2000, 3000])
179
+
180
+ # Any order
181
+ expect(response).to contain_exactly_reconnection_times([2000, 1000, 3000])
182
+
183
+ # Inclusion
184
+ expect(response).to have_reconnection_times([1000, 2000])
185
+ ```
186
+
187
+ ### Testing Complete Events
188
+
189
+ ```ruby
190
+ events = [
191
+ { type: 'message', data: '{"id":1}', id: '1', retry: 1000 },
192
+ { type: 'update', data: '{"id":2}', id: '2', retry: 2000 }
193
+ ]
194
+
195
+ # Exact order
196
+ expect(response).to be_events(events)
197
+
198
+ # Any order
199
+ expect(response).to contain_exactly_events(events)
200
+
201
+ # Inclusion
202
+ expect(response).to have_events([events.first])
203
+
204
+ # With JSON parsing
205
+ json_events = [
206
+ { type: 'message', data: {"id" => 1}, id: '1', retry: 1000 },
207
+ { type: 'update', data: {"id" => 2}, id: '2', retry: 2000 }
208
+ ]
209
+ expect(response).to be_events(json_events, json: true)
210
+ ```
211
+
212
+ ## Development
213
+
214
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
215
+
216
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
217
+
218
+ ## Contributing
219
+
220
+ Bug reports and pull requests are welcome on GitHub at https://github.com/moznion/rspec-sse-matchers. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/moznion/rspec-sse-matchers/blob/main/CODE_OF_CONDUCT.md).
221
+
222
+ ## License
223
+
224
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
225
+
226
+ ## Code of Conduct
227
+
228
+ Everyone interacting in the RSpec SSE Matchers project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/moznion/rspec-sse-matchers/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
11
+
12
+ namespace :rbs do
13
+ task gen: %i[] do
14
+ sh "rbs-inline --output --opt-out lib"
15
+ end
16
+ end
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec/sse/matchers"
4
+
5
+ # Mock response class for examples
6
+ class MockResponse
7
+ attr_reader :body, :headers
8
+
9
+ def initialize(body, headers = {})
10
+ @body = body
11
+ @headers = headers
12
+ end
13
+ end
14
+
15
+ RSpec.describe "SSE Matchers Examples" do
16
+ # Helper method to create a test SSE response with events
17
+ def create_sse_response(events)
18
+ body = ""
19
+ events.each do |event|
20
+ body += "id: #{event[:id]}\n" if event[:id]
21
+ body += "event: #{event[:type]}\n" if event[:type]
22
+ body += "data: #{event[:data]}\n" if event[:data]
23
+ body += "retry: #{event[:retry]}\n" if event[:retry]
24
+ body += "\n"
25
+ end
26
+
27
+ # Add proper closing for gracefully closed examples
28
+ body += "\n" if events.any?
29
+
30
+ MockResponse.new(body, {"Content-Type" => "text/event-stream"})
31
+ end
32
+
33
+ let(:event1) { {type: "message", data: '{"id":1,"message":"Hello"}', id: "1", retry: 1000} }
34
+ let(:event2) { {type: "update", data: '{"id":2,"message":"World"}', id: "2", retry: 2000} }
35
+ let(:event3) { {type: "close", data: '{"id":3,"message":"Goodbye"}', id: "3", retry: 3000} }
36
+
37
+ let(:single_event_response) { create_sse_response([event1]) }
38
+ let(:multiple_events_response) { create_sse_response([event1, event2, event3]) }
39
+ let(:empty_response) { create_sse_response([]) }
40
+
41
+ describe "Exact Order Matchers" do
42
+ context "be_events" do
43
+ it "passes when events match exactly in order" do
44
+ expect(single_event_response).to be_events([event1])
45
+ expect(multiple_events_response).to be_events([event1, event2, event3])
46
+ end
47
+
48
+ it "fails when events are in different order" do
49
+ expect(multiple_events_response).not_to be_events([event3, event2, event1])
50
+ end
51
+ end
52
+
53
+ context "be_event_types" do
54
+ it "passes when event types match exactly in order" do
55
+ expect(multiple_events_response).to be_event_types(%w[message update close])
56
+ end
57
+
58
+ it "accepts individual arguments" do
59
+ expect(multiple_events_response).to be_event_types("message", "update", "close")
60
+ end
61
+ end
62
+
63
+ context "be_event_data" do
64
+ it "passes when event data match exactly in order" do
65
+ expect(multiple_events_response).to be_event_data(%w[{"id":1,"message":"Hello"} {"id":2,"message":"World"} {"id":3,"message":"Goodbye"}])
66
+ end
67
+ end
68
+
69
+ context "be_event_ids" do
70
+ it "passes when event IDs match exactly in order" do
71
+ expect(multiple_events_response).to be_event_ids(%w[1 2 3])
72
+ end
73
+ end
74
+
75
+ context "be_reconnection_times" do
76
+ it "passes when reconnection times match exactly in order" do
77
+ expect(multiple_events_response).to be_reconnection_times([1000, 2000, 3000])
78
+ end
79
+ end
80
+ end
81
+
82
+ describe "Order-Independent Matchers" do
83
+ context "contain_exactly_events" do
84
+ it "passes when events match in any order" do
85
+ expect(multiple_events_response).to contain_exactly_events([event3, event1, event2])
86
+ end
87
+ end
88
+
89
+ context "contain_exactly_event_types" do
90
+ it "passes when event types match in any order" do
91
+ expect(multiple_events_response).to contain_exactly_event_types(%w[close message update])
92
+ end
93
+ end
94
+
95
+ context "contain_exactly_event_data" do
96
+ it "passes when event data match in any order" do
97
+ expect(multiple_events_response).to contain_exactly_event_data(%w[{"id":3,"message":"Goodbye"} {"id":1,"message":"Hello"} {"id":2,"message":"World"}])
98
+ end
99
+ end
100
+
101
+ context "contain_exactly_event_ids" do
102
+ it "passes when event IDs match in any order" do
103
+ expect(multiple_events_response).to contain_exactly_event_ids(%w[3 1 2])
104
+ end
105
+ end
106
+
107
+ context "contain_exactly_reconnection_times" do
108
+ it "passes when reconnection times match in any order" do
109
+ expect(multiple_events_response).to contain_exactly_reconnection_times([3000, 1000, 2000])
110
+ end
111
+ end
112
+ end
113
+
114
+ describe "Inclusion Matchers" do
115
+ context "have_events" do
116
+ it "passes when all expected events are included" do
117
+ expect(multiple_events_response).to have_events([event1, event2])
118
+ end
119
+
120
+ it "passes when a single expected event is included" do
121
+ expect(multiple_events_response).to have_events([event1])
122
+ end
123
+ end
124
+
125
+ context "have_event_types" do
126
+ it "passes when all expected event types are included" do
127
+ expect(multiple_events_response).to have_event_types(%w[message update])
128
+ end
129
+ end
130
+
131
+ context "have_event_data" do
132
+ it "passes when all expected event data are included" do
133
+ expect(multiple_events_response).to have_event_data(%w[{"id":1,"message":"Hello"} {"id":2,"message":"World"}])
134
+ end
135
+ end
136
+
137
+ context "have_event_ids" do
138
+ it "passes when all expected event IDs are included" do
139
+ expect(multiple_events_response).to have_event_ids(%w[1 2])
140
+ end
141
+ end
142
+
143
+ context "have_reconnection_times" do
144
+ it "passes when all expected reconnection times are included" do
145
+ expect(multiple_events_response).to have_reconnection_times([1000, 2000])
146
+ end
147
+ end
148
+ end
149
+
150
+ describe "Miscellaneous Matchers" do
151
+ context "be_gracefully_closed" do
152
+ it "passes when the response body ends with a double newline" do
153
+ expect(single_event_response).to be_gracefully_closed
154
+ expect(multiple_events_response).to be_gracefully_closed
155
+ end
156
+
157
+ it "fails when the response body doesn't end with a double newline" do
158
+ incomplete_response = MockResponse.new("event: message\ndata: test\n")
159
+ expect(incomplete_response).not_to be_gracefully_closed
160
+ end
161
+ end
162
+ end
163
+
164
+ describe "Edge Cases" do
165
+ context "empty responses" do
166
+ it "works with empty event collections" do
167
+ expect(empty_response).to be_events([])
168
+ expect(empty_response).to be_event_types([])
169
+ expect(empty_response).to be_event_data([])
170
+ expect(empty_response).to be_event_ids([])
171
+ expect(empty_response).to be_reconnection_times([])
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "JSON Parsing Option" do
177
+ context "with :json option" do
178
+ it "parses JSON data for event_data matchers" do
179
+ # The data is already in JSON format in our examples
180
+ expect(single_event_response).to be_event_data([{"id" => 1, "message" => "Hello"}], json: true)
181
+ expect(multiple_events_response).to be_event_data([
182
+ {"id" => 1, "message" => "Hello"},
183
+ {"id" => 2, "message" => "World"},
184
+ {"id" => 3, "message" => "Goodbye"}
185
+ ], json: true)
186
+ end
187
+
188
+ it "works with contain_exactly matchers" do
189
+ expect(multiple_events_response).to contain_exactly_event_data([
190
+ {"id" => 3, "message" => "Goodbye"},
191
+ {"id" => 1, "message" => "Hello"},
192
+ {"id" => 2, "message" => "World"}
193
+ ], json: true)
194
+ end
195
+
196
+ it "works with inclusion matchers" do
197
+ expect(multiple_events_response).to have_event_data([
198
+ {"id" => 1, "message" => "Hello"},
199
+ {"id" => 2, "message" => "World"}
200
+ ], json: true)
201
+ end
202
+
203
+ it "works with full event matchers" do
204
+ expected_events = [
205
+ {type: "message", data: {"id" => 1, "message" => "Hello"}, id: "1", retry: 1000},
206
+ {type: "update", data: {"id" => 2, "message" => "World"}, id: "2", retry: 2000},
207
+ {type: "close", data: {"id" => 3, "message" => "Goodbye"}, id: "3", retry: 3000}
208
+ ]
209
+ expect(multiple_events_response).to be_events(expected_events, json: true)
210
+ end
211
+ end
212
+ end
213
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpec
4
+ module SSE
5
+ module Matchers
6
+ VERSION = "0.0.1"
7
+ end
8
+ end
9
+ end