Skip to content

Commit 83a7c73

Browse files
committed
align with spec on names
1 parent 4fa6c07 commit 83a7c73

File tree

5 files changed

+98
-49
lines changed

5 files changed

+98
-49
lines changed

unirest-bdd-tests/src/test/java/BehaviorTests/TestListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
package BehaviorTests;
2727

2828
import kong.unirest.core.SseListener;
29-
import kong.unirest.core.java.SseEvent;
29+
import kong.unirest.core.java.Event;
3030

3131
import java.util.ArrayList;
3232
import java.util.Collections;
@@ -44,7 +44,7 @@ void assertHasComment(String comment) {
4444

4545

4646
@Override
47-
public void onEvent(SseEvent event) {
47+
public void onEvent(Event event) {
4848

4949
}
5050

unirest/src/main/java/kong/unirest/core/SseListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
package kong.unirest.core;
2727

28-
import kong.unirest.core.java.SseEvent;
28+
import kong.unirest.core.java.Event;
2929

3030
public interface SseListener {
31-
void onEvent(SseEvent event);
31+
void onEvent(Event event);
3232
void onComment(String line);
3333
}

unirest/src/main/java/kong/unirest/core/java/SseEvent.java renamed to unirest/src/main/java/kong/unirest/core/java/Event.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727

2828
import java.util.Objects;
2929

30-
public class SseEvent {
30+
public class Event {
3131
private final String id;
3232
private final Object data;
33-
private final String name;
33+
private final String field;
3434

35-
public SseEvent(String id, String type, Object data){
35+
public Event(String id, String field, Object data){
3636
this.id = id;
37-
this.name = type;
37+
this.field = field;
3838
this.data = data;
3939
}
4040

@@ -47,29 +47,29 @@ public String id() {
4747
}
4848

4949
public String name() {
50-
return name;
50+
return field;
5151
}
5252

5353
@Override
5454
public boolean equals(Object o) {
55-
if (!(o instanceof SseEvent)) return false;
56-
SseEvent sseEvent = (SseEvent) o;
57-
return Objects.equals(id, sseEvent.id)
58-
&& Objects.equals(data, sseEvent.data)
59-
&& Objects.equals(name, sseEvent.name);
55+
if (!(o instanceof Event)) return false;
56+
Event event = (Event) o;
57+
return Objects.equals(id, event.id)
58+
&& Objects.equals(data, event.data)
59+
&& Objects.equals(field, event.field);
6060
}
6161

6262
@Override
6363
public int hashCode() {
64-
return Objects.hash(id, data);
64+
return Objects.hash(id, data, field);
6565
}
6666

6767
@Override
6868
public String toString() {
6969
return "SseEvent{" +
70-
"id='" + id + '\'' +
71-
", data=" + data +
72-
", name=" + name +
70+
"data=" + data +
71+
", field=" + field +
72+
", id=" + id +
7373
'}';
7474
}
7575
}

unirest/src/main/java/kong/unirest/core/java/SseResponseHandler.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private ParsedLine parse(String line) {
7979
private class ParsedLine {
8080

8181
private final String value;
82-
private final String name;
82+
private final String field;
8383

8484
public ParsedLine(String value) {
8585
this(null, value);
@@ -89,8 +89,8 @@ public ParsedLine(){
8989
this(null, null);
9090
}
9191

92-
public ParsedLine(String name, String value) {
93-
this.name = name;
92+
public ParsedLine(String field, String value) {
93+
this.field = field;
9494
if(value == null){
9595
this.value = null;
9696
} else if (value.startsWith(" ")) {
@@ -101,38 +101,38 @@ public ParsedLine(String name, String value) {
101101
}
102102

103103
public boolean isDispatch() {
104-
return name == null && value == null;
104+
return field == null && value == null;
105105
}
106106

107107
public boolean isComment() {
108-
return name == null;
108+
return field == null;
109109
}
110110

111111
public String value() {
112112
return value;
113113
}
114114

115115
public String name() {
116-
return name;
116+
return field;
117117
}
118118

119119
public boolean isData() {
120-
return name != null && name.equalsIgnoreCase("data");
120+
return field != null && field.equalsIgnoreCase("data");
121121
}
122122

123123
public boolean isEvent() {
124-
return name != null && name.equalsIgnoreCase("event");
124+
return field != null && field.equalsIgnoreCase("event");
125125
}
126126

127127
public boolean isId() {
128-
return name != null
129-
&& name.equalsIgnoreCase("id")
128+
return field != null
129+
&& field.equalsIgnoreCase("id")
130130
&& value != null;
131131
}
132132

133133
public boolean isRetry() {
134-
return value == null && name != null
135-
&& name.matches("^\\d+$");
134+
return value == null && field != null
135+
&& field.matches("^\\d+$");
136136

137137
}
138138
}
@@ -143,8 +143,8 @@ private class EventBuffer {
143143
String type = "";
144144
StringBuffer buffer = new StringBuffer();
145145

146-
public SseEvent toEvent() {
147-
return new SseEvent(id, type, getValue());
146+
public Event toEvent() {
147+
return new Event(id, type, getValue());
148148
}
149149

150150
private String getValue() {

unirest/src/test/java/kong/unirest/core/java/SseResponseHandlerTest.java

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,38 @@ void dataWithJson(){
7676
}
7777

7878
@Test
79-
void multipleDataElementEvent() {
79+
void eventWithId() {
80+
handle("data: foo", "id: 1", "");
81+
listener.assertEvent("1", "", "foo");
82+
}
83+
84+
@Test //https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
85+
void specExample1() {
86+
//The following event stream, once followed by a blank line:
8087
handle("data: YHOO",
8188
"data: +2",
8289
"data: 10",
8390
"");
8491

92+
//...would cause an event message with the interface MessageEvent to be dispatched on the EventSource object.
93+
// The event's data attribute would contain the string "YHOO\n+2\n10" (where "\n" represents a newline).
8594
listener.assertEvent("", "", "YHOO\n+2\n10");
8695
}
8796

8897
@Test
89-
void eventWithId() {
90-
handle("data: foo", "id: 1", "");
91-
listener.assertEvent("1", "", "foo");
92-
}
93-
94-
@Test
95-
void dataEventsWithIds() {
98+
void specExample2() {
99+
//The following stream contains four blocks. The first block has just a comment, and will fire nothing.
100+
//
101+
// The second block has two fields with names "data" and "id" respectively; an event will be fired for this block,
102+
// with the data "first event", and will then set the last event ID to "1" so that if the connection died between this block and the next,
103+
// the server would be sent a `Last-Event-ID` header with the value `1`.
104+
//
105+
// The third block fires an event with data "second event", and also has an "id" field,
106+
// this time with no value, which resets the last event ID to the empty string
107+
// (meaning no `Last-Event-ID` header will now be sent in the event of a reconnection being attempted).
108+
// Finally, the last block just fires an event with the data " third event" (with a single leading space character).
109+
// Note that the last still has to end with a blank line,
110+
// the end of the stream is not enough to trigger the dispatch of the last event.
96111
handle(": test stream",
97112
"",
98113
"data: first event",
@@ -101,32 +116,66 @@ void dataEventsWithIds() {
101116
"data:second event",
102117
"id",
103118
"",
104-
"data: third event");
119+
"data: third event",
120+
"",
121+
"data: fourth event");
122+
105123

106-
listener.assertComment("test stream");
107-
listener.assertEvent("1", "", "first event");
108-
listener.assertEvent("", "", "second event");
109-
listener.assertNoEventContaining("third event");
124+
listener.assertComment("test stream")
125+
.assertEvent("1", "", "first event")
126+
.assertEvent("", "", "second event")
127+
.assertEvent("", "", " third event")
128+
.assertNoEventContaining("fourth event");
129+
}
130+
131+
@Test
132+
void specExample3() {
133+
//The following stream fires two events:
134+
//The first block fires events with the data set to the empty string, as would the last block if it was followed by a blank line. The middle block fires an event with the data set to a single newline character.
135+
// The last block is discarded because it is not followed by a blank line.
136+
handle("data",
137+
"",
138+
"data",
139+
"data",
140+
"",
141+
"data:");
142+
143+
listener.assertEvent("", "", "")
144+
.assertEvent("", "", "\n");
145+
146+
}
147+
148+
@Test
149+
void specExample4() {
150+
//The following stream fires two identical events:
151+
//This is because the space after the colon is ignored if present.
152+
handle("data:test",
153+
"",
154+
"data: test",
155+
"");
156+
157+
listener.assertEvent("", "", "test");
110158
}
111159

112160
private class TestListener implements SseListener {
113161

114-
private List<SseEvent> events = new ArrayList<>();
162+
private List<Event> events = new ArrayList<>();
115163
private List<String> comments = new ArrayList<>();
116164

117165
@Override
118-
public void onEvent(SseEvent event) {
166+
public void onEvent(Event event) {
119167
events.add(event);
120168
}
121169
@Override
122170
public void onComment(String line) {
123171
comments.add(line);
124172
}
125173

126-
public void assertEvent(String id, String event, String value) {
127-
SseEvent expected = new SseEvent(id, event, value);
174+
public TestListener assertEvent(String id, String event, String value) {
175+
Event expected = new Event(id, event, value);
128176
assertThat(events)
129177
.contains(expected);
178+
return this;
130179

131180
}
132181

0 commit comments

Comments
 (0)