Created
November 23, 2020 10:48
-
-
Save zpapez/873da3df892f21be91b8a8abc178f2bb to your computer and use it in GitHub Desktop.
WireMock - Asserting exactly one Authorization header
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
@BeforeAll | |
public static void beforeAll(@WireMockInstance WireMockServer server1, @WireMockInstance WireMockServer server2) { | |
zephyrWiremockServer = server1; | |
slackWiremockServer = server2; | |
zephyrWiremockServer.addMockServiceRequestListener(new RequestListener() { | |
@Override | |
public void requestReceived(Request request, Response response) { | |
// if we would fail directly here in listener, JUnit error would be really hard to understand (as no response would be generated) | |
// therefore saving the value to assert it later in main test flow | |
isZephyrAuthSingleValued = request.getHeaders().getHeader("Authorization").isSingleValued(); | |
} | |
}); | |
slackWiremockServer.addMockServiceRequestListener(new RequestListener() { | |
@Override | |
public void requestReceived(Request request, Response response) { | |
// if we would fail directly here in listener, JUnit error would be really hard to understand (as no response would be generated) | |
// therefore saving the value to assert it later in main test flow | |
isSlackAuthSingleValued = request.getHeaders().getHeader("Authorization").isSingleValued(); | |
} | |
}); | |
} | |
@AfterEach | |
public void afterEach() { | |
assertTrue(isZephyrAuthSingleValued, | |
"There must be only one 'Authorization' header in all Zephyr requests"); | |
assertTrue(isSlackAuthSingleValued, | |
"There must be only one 'Authorization' header in all Slack requests"); | |
zephyrWiremockServer.verify(anyRequestedFor(anyUrl()) | |
.withBasicAuth(new BasicCredentials("zephyrTestUser", "zephyrTestPassword"))); | |
slackWiremockServer.verify(anyRequestedFor(anyUrl()) | |
.withHeader("Authorization", equalTo("Bearer testFakeToken"))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment