|
|
@ -36,37 +36,53 @@ class APIWrapperTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
void successfullyReturn400WhenApiTokenInvalid() throws APIResponseException, IOException, InterruptedException {
|
|
|
|
void successfullyReturn400WhenApiTokenInvalid() throws APIResponseException, IOException, InterruptedException {
|
|
|
|
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpRequest("InvalidAPIKey");
|
|
|
|
|
|
|
|
int expectedStatusCode = 400;
|
|
|
|
|
|
|
|
String expectedBody = "{\"message\":\"Access denied - please check your token\",\"code\":400}";
|
|
|
|
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
|
|
|
|
|
|
|
sut.setApiKey("InvalidAPIKey");
|
|
|
|
sut.setApiKey("InvalidAPIKey");
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpRequest(sut);
|
|
|
|
Result actualResult = sut.sendGetRequest("waifu/1");
|
|
|
|
HttpResponse<String> expectedHttpResponse = build400HttpResponse();
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(actualResult.getStatusCode(), is(expectedStatusCode));
|
|
|
|
|
|
|
|
assertThat(actualResult.getBody(), is(expectedBody));
|
|
|
|
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
void successfullyReturn200WhenApiTokenValid() throws APIResponseException, IOException, InterruptedException {
|
|
|
|
|
|
|
|
HttpRequest expectedHttpRequest = buildHttpRequest("ValidAPIKey");
|
|
|
|
|
|
|
|
int expectedStatusCode = 200;
|
|
|
|
|
|
|
|
String expectedBody = "{\"message\":\"Token Valid\",\"code\":200}";
|
|
|
|
|
|
|
|
HttpResponse<String> expectedHttpResponse = buildHttpResponse(expectedStatusCode, expectedBody);
|
|
|
|
|
|
|
|
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
doReturn(expectedHttpResponse).when(httpClient).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
doReturn(HttpClient.Version.HTTP_2).when(httpClient).version();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Result actualResult = sut.sendRequest("waifu/1");
|
|
|
|
Result actualResult = sut.sendGetRequest("waifu/1");
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(actualResult.getStatusCode(), is(400));
|
|
|
|
assertThat(actualResult.getStatusCode(), is(expectedStatusCode));
|
|
|
|
assertThat(actualResult.getBody(), is("{\"message\":\"Access denied - please check your token\",\"code\":400}"));
|
|
|
|
assertThat(actualResult.getBody(), is(expectedBody));
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
verify(httpClient, times(1)).send(expectedHttpRequest, HttpResponse.BodyHandlers.ofString());
|
|
|
|
verify(httpClient, times(1)).version();
|
|
|
|
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
verifyNoMoreInteractions(httpClient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private HttpRequest buildHttpRequest(APIWrapper sut) {
|
|
|
|
private HttpRequest buildHttpRequest(String apiKey) {
|
|
|
|
return HttpRequest.newBuilder()
|
|
|
|
return HttpRequest.newBuilder()
|
|
|
|
.uri(URI.create("https://mywaifulist.moe/api/v1/waifu/1"))
|
|
|
|
.uri(URI.create("https://mywaifulist.moe/api/v1/waifu/1"))
|
|
|
|
.version(HttpClient.Version.HTTP_2)
|
|
|
|
|
|
|
|
.timeout(Duration.ofSeconds(20))
|
|
|
|
.timeout(Duration.ofSeconds(20))
|
|
|
|
.headers("Content-Type", "application/json", "apikey", sut.getApiKey())
|
|
|
|
.headers("Content-Type", "application/json", "apikey", apiKey)
|
|
|
|
.GET()
|
|
|
|
.GET()
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private HttpResponse<String> build400HttpResponse() {
|
|
|
|
private HttpResponse<String> buildHttpResponse(int statusCode, String body) {
|
|
|
|
return new HttpResponse<>() {
|
|
|
|
return new HttpResponse<>() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int statusCode() {
|
|
|
|
public int statusCode() {
|
|
|
|
return 400;
|
|
|
|
return statusCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -86,7 +102,7 @@ class APIWrapperTest {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public String body() {
|
|
|
|
public String body() {
|
|
|
|
return "{\"message\":\"Access denied - please check your token\",\"code\":400}";
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|