Skip to content

Consider adding a new location() API to HttpHeadersAssert #37489

Closed as not planned

Description

@scordio

I recently refactored a test using RestTestClient that asserted the Location header for a 302 response.

Originally, the test looked like the following:

ResponseSpec spec = client.get().uri(...).exchange();

assertThat(RestTestClientResponse.from(spec))
  .hasStatus(HttpStatus.FOUND)
  .headers()
  .hasHeaderSatisfying(HttpHeaders.LOCATION, values -> assertThat(values)
    .singleElement()
    .extracting(value -> UriComponentsBuilder.fromUri(URI.create(value)).build())
    .returns("http", from(UriComponents::getScheme))
    .returns("127.0.0.1", from(UriComponents::getHost))
    .returns(8081, from(UriComponents::getPort))
    .returns("value", from(UriComponents::getQueryParams).andThen(params -> params.getFirst("param")))
    .returns(null, from(UriComponents::getQueryParams).andThen(params -> params.getFirst("error")))
    .doesNotReturn(null, from(UriComponents::getQueryParams).andThen(params -> params.getFirst("number"))));

I could improve it by using AssertJ's URI assertions:

assertThat(RestTestClientResponse.from(spec))
  .hasStatus(HttpStatus.FOUND)
  .headers()
  .hasHeaderSatisfying(HttpHeaders.LOCATION, values -> assertThat(values)
    .singleElement()
    .extracting(URI::create, InstanceOfAssertFactories.URI_TYPE)
    .hasScheme("http")
    .hasHost("127.0.0.1")
    .hasPort(8081)
    .hasParameter("param", "value")
    .hasParameter("number")
    .hasNoParameter("error"));

However, inspired by location(String) in AbstractHeaderAssertions and getLocation() in HttpHeaders, I could imagine an improvement for the first part as well:

assertThat(RestTestClientResponse.from(spec))
  .hasStatus(HttpStatus.FOUND)
  .headers()
  .location() // new API that returns AssertJ's AbstractUriAssert
  .hasScheme("http")
  .hasHost("127.0.0.1")
  .hasPort(8081)
  .hasParameter("param", "value")
  .hasParameter("number")
  .hasNoParameter("error"));

Would you be open to adding a new location() API to HttpHeadersAssert? I'd be happy to submit a PR.

Activity

  1. added
    in: testIssues in the test module
    in: webIssues in web modules (web, webmvc, webflux, websocket)
    status: supersededAn issue that has been superseded by another
    and removed on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: testIssues in the test modulein: webIssues in web modules (web, webmvc, webflux, websocket)status: supersededAn issue that has been superseded by another

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions