Skip to content

Commit b59d0a3

Browse files
committed
Add equals/hashCode methods to ServerSentEvent
Closes gh-33606
1 parent 8ab965c commit b59d0a3

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

spring-web/src/main/java/org/springframework/http/codec/ServerSentEvent.java

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020

2121
import org.springframework.lang.Nullable;
22+
import org.springframework.util.ObjectUtils;
2223

2324
/**
2425
* Representation for a Server-Sent Event for use with Spring's reactive Web support.
@@ -102,6 +103,21 @@ public T data() {
102103
}
103104

104105

106+
@Override
107+
public boolean equals(@Nullable Object other) {
108+
return (this == other || (other instanceof ServerSentEvent<?> that &&
109+
ObjectUtils.nullSafeEquals(this.id, that.id) &&
110+
ObjectUtils.nullSafeEquals(this.event, that.event) &&
111+
ObjectUtils.nullSafeEquals(this.retry, that.retry) &&
112+
ObjectUtils.nullSafeEquals(this.comment, that.comment) &&
113+
ObjectUtils.nullSafeEquals(this.data, that.data)));
114+
}
115+
116+
@Override
117+
public int hashCode() {
118+
return ObjectUtils.nullSafeHash(this.id, this.event, this.retry, this.comment, this.data);
119+
}
120+
105121
@Override
106122
public String toString() {
107123
return ("ServerSentEvent [id = '" + this.id + "', event='" + this.event + "', retry=" +

spring-web/src/test/java/org/springframework/http/codec/ServerSentEventHttpMessageReaderTests.java

+8-28
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,10 @@ void readServerSentEvents() {
7474
request, Collections.emptyMap()).cast(ServerSentEvent.class);
7575

7676
StepVerifier.create(events)
77-
.consumeNextWith(event -> {
78-
assertThat(event.id()).isEqualTo("c42");
79-
assertThat(event.event()).isEqualTo("foo");
80-
assertThat(event.retry()).isEqualTo(Duration.ofMillis(123));
81-
assertThat(event.comment()).isEqualTo("bla\nbla bla\nbla bla bla");
82-
assertThat(event.data()).isEqualTo("bar");
83-
})
84-
.consumeNextWith(event -> {
85-
assertThat(event.id()).isEqualTo("c43");
86-
assertThat(event.event()).isEqualTo("bar");
87-
assertThat(event.retry()).isEqualTo(Duration.ofMillis(456));
88-
assertThat(event.comment()).isNull();
89-
assertThat(event.data()).isEqualTo("baz");
90-
})
77+
.expectNext(ServerSentEvent.builder().id("c42").event("foo")
78+
.retry(Duration.ofMillis(123)).comment("bla\nbla bla\nbla bla bla").data("bar").build())
79+
.expectNext(ServerSentEvent.builder().id("c43").event("bar")
80+
.retry(Duration.ofMillis(456)).data("baz").build())
9181
.consumeNextWith(event -> assertThat(event.data()).isNull())
9282
.consumeNextWith(event -> assertThat(event.data()).isNull())
9383
.expectComplete()
@@ -108,20 +98,10 @@ void readServerSentEventsWithMultipleChunks() {
10898
request, Collections.emptyMap()).cast(ServerSentEvent.class);
10999

110100
StepVerifier.create(events)
111-
.consumeNextWith(event -> {
112-
assertThat(event.id()).isEqualTo("c42");
113-
assertThat(event.event()).isEqualTo("foo");
114-
assertThat(event.retry()).isEqualTo(Duration.ofMillis(123));
115-
assertThat(event.comment()).isEqualTo("bla\nbla bla\nbla bla bla");
116-
assertThat(event.data()).isEqualTo("bar");
117-
})
118-
.consumeNextWith(event -> {
119-
assertThat(event.id()).isEqualTo("c43");
120-
assertThat(event.event()).isEqualTo("bar");
121-
assertThat(event.retry()).isEqualTo(Duration.ofMillis(456));
122-
assertThat(event.comment()).isNull();
123-
assertThat(event.data()).isEqualTo("baz");
124-
})
101+
.expectNext(ServerSentEvent.builder().id("c42").event("foo")
102+
.retry(Duration.ofMillis(123)).comment("bla\nbla bla\nbla bla bla").data("bar").build())
103+
.expectNext(ServerSentEvent.builder().id("c43").event("bar")
104+
.retry(Duration.ofMillis(456)).data("baz").build())
125105
.expectComplete()
126106
.verify();
127107
}

0 commit comments

Comments
 (0)