Skip to content

Commit cbe9472

Browse files
committed
add method to explicitly set the Last-Event-ID header
1 parent 45e12d8 commit cbe9472

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public static void reset() {
105105
WebSocketHandler.reset();
106106
retryTimes = 0;
107107
errorCode = 400;
108+
lastRequest = null;
108109
}
109110

110111
static {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,15 @@ void sendHeaders() {
9999
.assertHeader("fruit", "apple")
100100
.assertCookie("snack", "snickerdoodle");
101101
}
102+
103+
@Test
104+
void canSendLastEventIdHeader() {
105+
Unirest.sse(MockServer.SSE)
106+
.lastEventId("42")
107+
.connect()
108+
.collect(Collectors.toList());
109+
110+
MockServer.lastRequest()
111+
.assertHeader("Last-Event-ID", "42");
112+
}
102113
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ public interface SseRequest {
152152
*/
153153
Headers getHeaders();
154154

155+
/**
156+
* Sets the Last-Event-ID HTTP request header reports an EventSource object's last event ID string to the server when the user agent is to reestablish the connection.
157+
* @param id the ID
158+
* @return this request builder
159+
*/
160+
SseRequest lastEventId(String id);
161+
155162
/**
156163
* @return the full URL if the request
157164
*/

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class SseRequestImpl implements SseRequest {
3939
protected Headers headers = new Headers();
4040

4141

42+
4243
public SseRequestImpl(Config config, String url) {
4344
Objects.requireNonNull(config, "Config cannot be null");
4445
Objects.requireNonNull(url, "URL cannot be null");
@@ -126,6 +127,11 @@ public SseRequest queryString(Map<String, Object> parameters) {
126127
return this;
127128
}
128129

130+
@Override
131+
public SseRequest lastEventId(String id) {
132+
return header("Last-Event-ID", id);
133+
}
134+
129135
@Override
130136
public CompletableFuture<Void> connect(SseHandler handler) {
131137
return config.getClient().sse(this, handler);

0 commit comments

Comments
 (0)