-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHttpClientMockAsyncTest.java
More file actions
57 lines (43 loc) · 1.89 KB
/
HttpClientMockAsyncTest.java
File metadata and controls
57 lines (43 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.pgssoft.httpclient;
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();
httpClientMock.onPost()
.withHost("localhost")
.withPath("/login")
.doReturn(200, "ABC");
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(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));
}
}
}