Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue when configuring an exception for an asynchronously evaluated request #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/com/pgssoft/httpclient/HttpClientMock.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pgssoft.httpclient;

import com.pgssoft.httpclient.internal.debug.Debugger;
import com.pgssoft.httpclient.internal.HttpResponseProxy;
import com.pgssoft.httpclient.internal.debug.Debugger;
import com.pgssoft.httpclient.internal.rule.Rule;
import com.pgssoft.httpclient.internal.rule.RuleBuilder;

Expand Down Expand Up @@ -330,7 +330,9 @@ public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, Htt
var response = new HttpResponseProxy<>(serverResponse.statusCode(), httpHeaders, body, request);
return CompletableFuture.completedFuture(response);
} catch (IOException e) {
throw new IllegalStateException(e);
var future = new CompletableFuture<HttpResponse<T>>();
future.completeExceptionally(e);
return future;
}

}
Expand Down
25 changes: 23 additions & 2 deletions src/test/java/com/pgssoft/httpclient/HttpClientMockAsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpResponse;
import java.util.concurrent.ExecutionException;

import static java.net.http.HttpRequest.BodyPublishers.noBody;
import static java.net.http.HttpRequest.newBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class HttpClientMockAsyncTest {

private static final String EXAMPLE_URI = "http://localhost/login";

@Test
void sendAsync_Should_ReturnCompletedFuture() throws ExecutionException, InterruptedException {
HttpClientMock httpClientMock = new HttpClientMock();
Expand All @@ -22,15 +26,32 @@ void sendAsync_Should_ReturnCompletedFuture() throws ExecutionException, Interru
.withPath("/login")
.doReturn(200, "ABC");

var req = newBuilder(URI.create("http://localhost/login"))
var req = newBuilder(URI.create(EXAMPLE_URI))
.POST(noBody())
.build();
var res = httpClientMock.sendAsync(req, HttpResponse.BodyHandlers.ofString());

assertThat(res.get().body(), equalTo("ABC"));
assertThat(res.get().statusCode(), equalTo(200));
httpClientMock.verify().post("http://localhost/login");
httpClientMock.verify().post(EXAMPLE_URI);
}

@Test
void sendAsync_Should_ReturnTheConfiguredExceptionInTheCompletedFuture() {
HttpClientMock httpClientMock = new HttpClientMock();
var expectedException = new IOException("expected exception");

httpClientMock.onGet(EXAMPLE_URI).doThrowException(expectedException);

var req = newBuilder(URI.create(EXAMPLE_URI)).GET().build();
var res = httpClientMock.sendAsync(req, HttpResponse.BodyHandlers.ofString());

assertThat(res.isCompletedExceptionally(), is(true));
try {
res.get();
} catch (Exception e) {
assertThat(e.getCause(), equalTo(expectedException));
}
}

}