This repository has been archived by the owner on Mar 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Waldemar Lammert
committed
Dec 29, 2019
1 parent
b5e8335
commit a7da7b9
Showing
5 changed files
with
74 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/test/java/eu/dev089/components/RequestCreatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package eu.dev089.components; | ||
|
||
import static eu.dev089.FeatureMatcher.createFeatureMatcher; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.stream.Stream; | ||
import okhttp3.Request; | ||
import okhttp3.Request.Builder; | ||
import org.hamcrest.Matcher; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class RequestCreatorTest { | ||
|
||
@Test | ||
void setsBearerToken() { | ||
var token = "THIS_IS_MY_TOKEN"; | ||
var creator = new RequestCreator(token); | ||
var request = creator.requestBuilder("somePath").get().build(); | ||
|
||
assertThat(request, hasToken(token)); | ||
} | ||
|
||
private static Matcher<Request> hasToken(String token) { | ||
return createFeatureMatcher(hasItem("Bearer " + token), "token", | ||
r -> r.headers().values("Authorization")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("emptyTokens") | ||
void reportsNoTokenSet(String token) { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
new RequestCreator(token); | ||
}); | ||
} | ||
|
||
static Stream<String> emptyTokens() { | ||
return Stream.of(null, ""); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("headers") | ||
void setsHeaders(String header) { | ||
var creator = new RequestCreator("token"); | ||
var builder = creator.requestBuilder("context"); | ||
|
||
assertThat(builder, hasHeaderProperty(header)); | ||
} | ||
|
||
private static Matcher<Request.Builder> hasHeaderProperty(String header) { | ||
return createFeatureMatcher(is(notNullValue()), "token", r -> r.build().headers().values(header)); | ||
} | ||
|
||
static Stream<String> headers() { | ||
return Stream.of("Authorization", "Cache-Control", "Connection"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters