Skip to content

Commit d8fca53

Browse files
committed
support other methods for SSE and bodies
1 parent 4ab136c commit d8fca53

File tree

8 files changed

+55
-6
lines changed

8 files changed

+55
-6
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package BehaviorTests;
2727

28+
import kong.unirest.core.HttpMethod;
2829
import kong.unirest.core.Unirest;
2930
import kong.unirest.core.java.Event;
3031
import org.junit.jupiter.api.BeforeEach;
@@ -110,4 +111,21 @@ void canSendLastEventIdHeader() {
110111
MockServer.lastRequest()
111112
.assertHeader("Last-Event-ID", "42");
112113
}
114+
115+
@Test
116+
void sendSSEAsAPost() {
117+
MockServer.Sse.queueEvent("1", "message", "hi mom");
118+
119+
var events = Unirest.sse(MockServer.SSE, HttpMethod.POST)
120+
.connect()
121+
.collect(Collectors.toList());
122+
123+
assertThat(events)
124+
.hasSize(2)
125+
.containsExactly(
126+
new Event("", "connect", "Welcome to Server Sent Events"),
127+
new Event("1", "message", "hi mom")
128+
);
129+
130+
}
113131
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static void keepAlive(boolean value) {
7676

7777
@Override
7878
public void accept(SseClient client) {
79+
client.ctx().
7980
lastRequest = new RequestCapture(client.ctx());
8081
if(keepAlive) {
8182
client.keepAlive();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@ <T> CompletableFuture<HttpResponse<T>> request(HttpRequest request,
8484
*/
8585
CompletableFuture<Void> sse(SseRequest request, SseHandler handler);
8686

87+
8788
Stream<Event> sse(SseRequest request);
8889
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,10 @@ public interface SseRequest {
181181
* @return a stream of events
182182
*/
183183
Stream<Event> connect();
184+
185+
/**
186+
* The method of the request. Defaults to GET
187+
* @return the method
188+
*/
189+
HttpMethod getMethod();
184190
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
public class SseRequestImpl implements SseRequest {
3737
private final Config config;
3838
private final Path url;
39+
private final HttpMethod method;
3940
protected Headers headers = new Headers();
4041

41-
42-
43-
public SseRequestImpl(Config config, String url) {
42+
public SseRequestImpl(Config config, String url, HttpMethod method) {
43+
this.method = method;
4444
Objects.requireNonNull(config, "Config cannot be null");
4545
Objects.requireNonNull(url, "URL cannot be null");
4646

@@ -142,6 +142,11 @@ public Stream<Event> connect() {
142142
return config.getClient().sse(this);
143143
}
144144

145+
@Override
146+
public HttpMethod getMethod() {
147+
return method;
148+
}
149+
145150
@Override
146151
public Headers getHeaders() {
147152
return headers;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ public static SseRequest sse(String url) {
138138
return primaryInstance.sse(url);
139139
}
140140

141+
public static SseRequest sse(String url, HttpMethod method) {
142+
return primaryInstance.sse(url, method);
143+
}
144+
141145
/**
142146
* Spawn a new Unirest Instance with a new config.
143147
* Don't forget to shut it down when your done.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public WebSocketRequest webSocket(String url) {
158158
}
159159

160160
public SseRequestImpl sse(String url) {
161-
return new SseRequestImpl(config, url);
161+
return new SseRequestImpl(config, url, HttpMethod.GET);
162+
}
163+
164+
public SseRequestImpl sse(String url, HttpMethod method) {
165+
return new SseRequestImpl(config, url, method);
162166
}
163167
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,21 @@
2626
package kong.unirest.core.java;
2727

2828
import kong.unirest.core.ContentType;
29+
import kong.unirest.core.HttpMethod;
2930
import kong.unirest.core.SseRequest;
3031

3132
import java.net.URI;
3233
import java.net.http.HttpRequest;
3334

3435
public class SseRequestBuilder {
3536
static java.net.http.HttpRequest getHttpRequest(SseRequest request) {
36-
var accept = HttpRequest
37+
var a = HttpRequest
3738
.newBuilder()
3839
.header("Accept", ContentType.EVENT_STREAMS.getMimeType())
39-
.GET()
4040
.uri(URI.create(request.getUrl()));
4141

42+
var accept = addMethod(request, a);
43+
4244
request.getHeaders().all().forEach(h -> {
4345
accept.header(h.getName(), h.getValue());
4446
});
@@ -47,5 +49,13 @@ static java.net.http.HttpRequest getHttpRequest(SseRequest request) {
4749
return accept.build();
4850
}
4951

52+
private static HttpRequest.Builder addMethod(SseRequest request, HttpRequest.Builder a) {
53+
if(request.getMethod().equals(HttpMethod.GET)){
54+
return a.GET();
55+
} else {
56+
return a.method(request.getMethod().toString(), HttpRequest.BodyPublishers.noBody());
57+
}
58+
}
59+
5060

5161
}

0 commit comments

Comments
 (0)