Skip to content

Commit 1924023

Browse files
committed
test: Add more tests
1 parent c0ef3f3 commit 1924023

File tree

7 files changed

+177
-21
lines changed

7 files changed

+177
-21
lines changed

src/main/java/com/spotify/github/http/HttpRequest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@
3434
@JsonSerialize(as = ImmutableHttpRequest.class)
3535
@JsonDeserialize(as = ImmutableHttpRequest.class)
3636
public interface HttpRequest {
37-
String method();
37+
@Value.Default
38+
default String method() {
39+
return "GET";
40+
}
3841

3942
String url();
4043

4144
@Nullable
4245
String body();
4346

4447
@Value.Default
45-
default Map<String, List<String>> headers(){
48+
default Map<String, List<String>> headers() {
4649
return Map.of();
4750
}
4851

src/test/java/com/spotify/github/v3/clients/MockHelper.java renamed to src/test/java/com/spotify/github/MockHelper.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* -/-/-
1919
*/
2020

21-
package com.spotify.github.v3.clients;
21+
package com.spotify.github;
2222

2323
import com.spotify.github.http.BaseHttpResponse;
2424
import com.spotify.github.http.HttpRequest;
@@ -48,11 +48,7 @@ public static HttpResponse createMockHttpResponse(
4848
final int statusCode,
4949
final String body,
5050
final Map<String, List<String>> headers) {
51-
HttpRequest httpRequest = ImmutableHttpRequest.builder()
52-
.method("GET")
53-
.body(null)
54-
.headers(Map.of())
55-
.url(url).build();
51+
HttpRequest httpRequest = ImmutableHttpRequest.builder().url(url).build();
5652
return new BaseHttpResponse(httpRequest, statusCode, "", headers) {
5753
@Override
5854
public InputStream body() {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2021 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.http;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
import static org.hamcrest.MatcherAssert.assertThat;
29+
import static org.hamcrest.Matchers.containsInAnyOrder;
30+
import static org.junit.jupiter.api.Assertions.*;
31+
32+
public class HttpRequestTest {
33+
@Test
34+
void createBareRequest() {
35+
// When
36+
HttpRequest httpRequest = ImmutableHttpRequest.builder().url("https://example.com").build();
37+
// Then
38+
assertNotNull(httpRequest);
39+
assertEquals("GET", httpRequest.method());
40+
assertEquals("https://example.com", httpRequest.url());
41+
assertNull(httpRequest.body());
42+
assertEquals(0, httpRequest.headers().size());
43+
assertNull(httpRequest.headers("Accept-Encoding"));
44+
assertNull(httpRequest.header("Accept-Encoding"));
45+
}
46+
47+
@Test
48+
void createRequest() {
49+
// When
50+
HttpRequest httpRequest =
51+
ImmutableHttpRequest.builder()
52+
.url("https://example.com")
53+
.method("POST")
54+
.body("{\"foo\":\"bar\"}")
55+
.headers(
56+
Map.of(
57+
"Content-Type",
58+
List.of("application/json", "charset=utf-8"),
59+
"Accept",
60+
List.of("application/json")))
61+
.build();
62+
// Then
63+
assertNotNull(httpRequest);
64+
assertEquals("POST", httpRequest.method());
65+
assertEquals("https://example.com", httpRequest.url());
66+
assertEquals("{\"foo\":\"bar\"}", httpRequest.body());
67+
assertEquals(2, httpRequest.headers().size());
68+
assertThat(
69+
httpRequest.headers("Content-Type"),
70+
containsInAnyOrder("application/json", "charset=utf-8"));
71+
assertEquals("application/json,charset=utf-8", httpRequest.header("Content-Type"));
72+
assertThat(httpRequest.headers("Accept"), containsInAnyOrder("application/json"));
73+
assertEquals("application/json", httpRequest.header("Accept"));
74+
}
75+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2021 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.http;
22+
23+
import static com.spotify.github.MockHelper.createMockHttpResponse;
24+
import static org.hamcrest.MatcherAssert.assertThat;
25+
import static org.hamcrest.Matchers.containsInAnyOrder;
26+
import static org.junit.jupiter.api.Assertions.*;
27+
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.util.List;
31+
import java.util.Map;
32+
import org.junit.jupiter.api.Test;
33+
34+
public class HttpResponseTest {
35+
@Test
36+
void createBareResponse() {
37+
// When
38+
HttpResponse httpResponse = createMockHttpResponse("https://example.com", 200, "{}", Map.of());
39+
// Then
40+
assertNotNull(httpResponse);
41+
assertEquals("GET", httpResponse.request().method());
42+
assertEquals("https://example.com", httpResponse.request().url());
43+
assertTrue(httpResponse.isSuccessful());
44+
assertEquals(0, httpResponse.headers().size());
45+
assertNull(httpResponse.headers("Accept-Encoding"));
46+
assertNull(httpResponse.header("Accept-Encoding"));
47+
}
48+
49+
@Test
50+
void createResponse() throws IOException {
51+
// When
52+
HttpResponse httpResponse =
53+
createMockHttpResponse(
54+
"https://example.com",
55+
200,
56+
"{\"foo\":\"bar\"}",
57+
Map.of(
58+
"Content-Type",
59+
List.of("application/json", "charset=utf-8"),
60+
"Accept",
61+
List.of("application/json"),
62+
"Cache-Control",
63+
List.of("no-cache"),
64+
"Set-Cookie",
65+
List.of("sessionId=abc123", "userId=xyz789")));
66+
String responseBody = null;
67+
try (InputStream is = httpResponse.body()) {
68+
responseBody = new String(is.readAllBytes());
69+
}
70+
// Then
71+
assertNotNull(httpResponse);
72+
assertEquals("{\"foo\":\"bar\"}", httpResponse.bodyString());
73+
assertEquals("{\"foo\":\"bar\"}", responseBody);
74+
assertEquals(4, httpResponse.headers().size());
75+
assertThat(
76+
httpResponse.headers("Content-Type"),
77+
containsInAnyOrder("application/json", "charset=utf-8"));
78+
assertEquals("application/json,charset=utf-8", httpResponse.header("Content-Type"));
79+
assertThat(httpResponse.headers("Accept"), containsInAnyOrder("application/json"));
80+
assertEquals("application/json", httpResponse.header("Accept"));
81+
assertThat(httpResponse.headers("Cache-Control"), containsInAnyOrder("no-cache"));
82+
assertEquals("no-cache", httpResponse.header("Cache-Control"));
83+
assertThat(
84+
httpResponse.headers("Set-Cookie"),
85+
containsInAnyOrder("sessionId=abc123", "userId=xyz789"));
86+
assertEquals("sessionId=abc123,userId=xyz789", httpResponse.header("Set-Cookie"));
87+
}
88+
}

src/test/java/com/spotify/github/v3/clients/IssueClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import static com.google.common.io.Resources.getResource;
2424
import static com.spotify.github.FixtureHelper.loadFixture;
2525
import static com.spotify.github.v3.clients.IssueClient.*;
26-
import static com.spotify.github.v3.clients.MockHelper.createMockResponse;
26+
import static com.spotify.github.MockHelper.createMockResponse;
2727
import static java.lang.String.format;
2828
import static java.nio.charset.Charset.defaultCharset;
2929
import static java.util.concurrent.CompletableFuture.completedFuture;
@@ -57,7 +57,7 @@
5757
import java.util.List;
5858
import java.util.concurrent.CompletableFuture;
5959
import java.util.concurrent.CompletionException;
60-
import okhttp3.Response;
60+
6161
import org.junit.jupiter.api.BeforeEach;
6262
import org.junit.jupiter.api.Test;
6363
import org.junit.jupiter.params.ParameterizedTest;

src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE;
3030
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY;
3131
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY_INVITATION;
32-
import static com.spotify.github.v3.clients.MockHelper.createMockHttpResponse;
33-
import static com.spotify.github.v3.clients.MockHelper.createMockResponse;
32+
import static com.spotify.github.MockHelper.createMockHttpResponse;
33+
import static com.spotify.github.MockHelper.createMockResponse;
3434
import static com.spotify.github.v3.clients.RepositoryClient.STATUS_URI_TEMPLATE;
3535
import static java.lang.String.format;
3636
import static java.nio.charset.Charset.defaultCharset;
@@ -50,7 +50,6 @@
5050
import com.google.common.io.Resources;
5151
import com.spotify.github.async.Async;
5252
import com.spotify.github.async.AsyncPage;
53-
import com.spotify.github.http.HttpRequest;
5453
import com.spotify.github.http.HttpResponse;
5554
import com.spotify.github.jackson.Json;
5655
import com.spotify.github.v3.comment.Comment;
@@ -77,13 +76,8 @@
7776
import java.util.Map;
7877
import java.util.Optional;
7978
import java.util.concurrent.CompletableFuture;
80-
import java.util.concurrent.ExecutionException;
8179
import java.util.stream.Collectors;
82-
import okhttp3.MediaType;
83-
import okhttp3.Protocol;
84-
import okhttp3.Request;
85-
import okhttp3.Response;
86-
import okhttp3.ResponseBody;
80+
8781
import org.junit.jupiter.api.BeforeEach;
8882
import org.junit.jupiter.api.Test;
8983
import org.mockito.ArgumentCaptor;

src/test/java/com/spotify/github/v3/clients/TeamClientTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import static com.spotify.github.v3.clients.GitHubClient.LIST_PENDING_TEAM_INVITATIONS;
2525
import static com.spotify.github.v3.clients.GitHubClient.LIST_TEAMS;
2626
import static com.spotify.github.v3.clients.GitHubClient.LIST_TEAM_MEMBERS;
27-
import static com.spotify.github.v3.clients.MockHelper.createMockResponse;
27+
import static com.spotify.github.MockHelper.createMockResponse;
2828
import static java.nio.charset.Charset.defaultCharset;
2929
import static java.util.concurrent.CompletableFuture.completedFuture;
3030
import static java.util.stream.Collectors.toList;
@@ -49,7 +49,7 @@
4949
import java.io.IOException;
5050
import java.util.List;
5151
import java.util.concurrent.CompletableFuture;
52-
import okhttp3.Response;
52+
5353
import org.junit.jupiter.api.BeforeEach;
5454
import org.junit.jupiter.api.Test;
5555
import org.mockito.ArgumentCaptor;

0 commit comments

Comments
 (0)