Skip to content

Commit

Permalink
Add header expectation tests in forwarded requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tgeens committed Sep 6, 2023
1 parent ecbe3e2 commit ec58211
Showing 1 changed file with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
import static com.contentgrid.gateway.runtime.web.ContentGridRuntimeHeaders.CONTENTGRID_DEPLOYMENT_ID;
import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static org.hamcrest.Matchers.is;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_HOST_HEADER;
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_PORT_HEADER;
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_PROTO_HEADER;
import static org.springframework.http.HttpHeaders.HOST;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOidcLogin;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.springSecurity;

Expand All @@ -27,6 +32,7 @@
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
Expand Down Expand Up @@ -132,20 +138,60 @@ public static void setup(@Autowired ServiceCatalog catalog, @Autowired WireMockS
@Autowired
WebTestClient webTestClient;

@Test
void happy_path_http200() {
wireMockServer.stubFor(WireMock.get("/test").willReturn(WireMock.ok("OK")));
@Nested
class HappyPath {

webTestClient
.mutateWith(mockOidcLogin())
.get().uri("https://{hostname}/test", hostname(APP_ID))
.header("Host", hostname(APP_ID))
.exchange()
.expectStatus().isEqualTo(HttpStatus.OK)
.expectHeader().value(CONTENTGRID_APPLICATION_ID, is(APP_ID.toString()))
.expectHeader().value(CONTENTGRID_DEPLOYMENT_ID, is(DEPLOY_ID.toString()));
@Test
void contentgridHeaders_expected() {
wireMockServer.stubFor(WireMock.get("/test").willReturn(WireMock.ok("OK")));

webTestClient
.mutateWith(mockOidcLogin())
.get().uri("https://{hostname}/test", hostname(APP_ID))
.header("Host", hostname(APP_ID))
.exchange()
.expectStatus().isEqualTo(HttpStatus.OK)
.expectHeader().value(CONTENTGRID_APPLICATION_ID, is(APP_ID.toString()))
.expectHeader().value(CONTENTGRID_DEPLOYMENT_ID, is(DEPLOY_ID.toString()));

wireMockServer.verify(WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
wireMockServer.verify(WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
}

@Test
void xForwardedHeaders_expected() {
var hostname = hostname(APP_ID);
wireMockServer.stubFor(WireMock.get("/test").willReturn(WireMock.ok("OK")));

webTestClient
.mutateWith(mockOidcLogin())
.get().uri("https://{hostname}/test", hostname)
.header("Host", hostname)
.exchange()
.expectStatus().isEqualTo(HttpStatus.OK);

wireMockServer.verify(WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))
.withHeader(X_FORWARDED_HOST_HEADER, equalTo(hostname))
.withHeader(X_FORWARDED_PROTO_HEADER, equalTo("https"))
.withHeader(X_FORWARDED_PORT_HEADER, equalTo("443"))
);
}

@Test
void preserveHostHeader_expected() {
var hostname = hostname(APP_ID);
wireMockServer.stubFor(WireMock.get("/test").willReturn(WireMock.ok("OK")));

webTestClient
.mutateWith(mockOidcLogin())
.get().uri("https://{hostname}/test", hostname)
.header("Host", hostname)
.exchange()
.expectStatus().isEqualTo(HttpStatus.OK);

wireMockServer.verify(WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))
.withHeader(HOST, equalTo(hostname))
);
}
}

@Test
Expand Down

0 comments on commit ec58211

Please sign in to comment.