Skip to content

Instantly share code, notes, and snippets.

@zpapez
Created November 23, 2020 10:48
Show Gist options
  • Save zpapez/873da3df892f21be91b8a8abc178f2bb to your computer and use it in GitHub Desktop.
Save zpapez/873da3df892f21be91b8a8abc178f2bb to your computer and use it in GitHub Desktop.
WireMock - Asserting exactly one Authorization header
@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