Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the fluency of the ResponseCreator API #27280

Closed
wants to merge 3 commits into from
Closed

Improve the fluency of the ResponseCreator API #27280

wants to merge 3 commits into from

Conversation

ascopes
Copy link
Contributor

@ascopes ascopes commented Aug 14, 2021

  • Added utility methods to DefaultResponseCreator

    This enables the ability to fluently add single headers like you can
    already with the mock request specification APIs by chaining
    .header(name, value) repeatedly. This reduces code clutter within
    test cases.

    Added the ability to add cookies as this simplifies tests that work
    with sessions.

    Added the ability to override the character encoding used for setting
    a string body on a response, as this is useful when working in
    environments that do not automatically assume UTF-8, such as
    integrating with legacy applications from a new Spring one.

  • Added unit tests for DefaultResponseCreator

    These were already partially tested by the MockRestResponseCreator, but some
    existing calls prior to this PR were missing, and this was also an opportunity
    for me to directly test the changes I added.

  • Added common response statuses to MockRestResponseCreators

    It appears that a couple of the more commonly used HTTP status codes
    were missed out from the default methods in MockRestResponseCreators.
    The methods included are common enough that most test packs will probably
    have at least one case that uses one of these calls, so it enables
    keeping the fluent API tidy.

    I added a couple of additional edge cases for common response
    statuses that will occur when working in cloud environments, such as
    AWS, CloudFlare, or using gateways such as Kong, where resillient
    applications should be able to respond to ratelimits, gateway errors,
    and gateway timeouts (which may occur if a remote service is down).

    Added test cases for any changes made.

These changes allow for cases to be written like so:

server
    .expect(requestTo("/api/v1/users"))
    .andExpect(method(POST))
    .andExpect(header("Content-Type", APPLICATION_JSON_VALUE))
    .andExpect(header("Accept", APPLICATION_JSON_VALUE))
    .andExpect(jsonPath("$.name").value("Ashley"))
    .andExpect(jsonPath("$.username").value("ascopes"))
    .andExpect(jsonPath("$.password").value("12345"))
    .andRespond(withRequestConflict()
        .header("X-Request-ID", "12345")
        .header(HttpHeaders.PRAGMA, "no-cache")
        .cookie(ResponseCookie.from("anon-session-id", "12345").build())
        .body("Entity already exists", StandardCharsets.US_ASCII));

which should be more fluent and easy to read than what would be needed on the existing API:

HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("X-Request-ID", "12345");
responseHeaders.add(HttpHeaders.PRAGMA, "no-cache");
responseHeaders.add(HttpHeaders.SET_COOKIE, ResponseCookie.from("anon-session-id", "12345").build().toString());


server
    .expect(requestTo("/api/v1/users"))
    .andExpect(method(POST))
    .andExpect(header("Content-Type", APPLICATION_JSON_VALUE))
    .andExpect(header("Accept", APPLICATION_JSON_VALUE))
    .andExpect(jsonPath("$.name").value("Ashley"))
    .andExpect(jsonPath("$.username").value("ascopes"))
    .andExpect(jsonPath("$.password").value("12345"))
    .andRespond(withStatus(HttpStatus.CONFLICT)
        .headers(responseHeaders)
        .body("Entity already exists".getBytes(StandardCharsets.US_ASCII)));

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 14, 2021
@ascopes ascopes changed the title Feature/improve response creator api Improve the fluency of the ResponseCreator API [Spring Test] Aug 14, 2021
This enables the ability to fluently add single headers like you can
already with the mock request specification APIs by chaining
`.header(name, value)` repeatedly. This reduces code clutter within
test cases.

Added the ability to add cookies as this simplifies tests that work
with sessions.

Added the ability to override the character encoding used for setting
a string body on a response, as this is useful when working in
environments that do not automatically assume UTF-8, such as
integrating with legacy applications from a new Spring one.
These were already *partially* tested by the MockRestResponseCreator, but some
existing calls prior to this PR were missing, and this was also an opportunity
for me to directly test the changes I added.
It appears that a couple of the more commonly used HTTP status codes
were missed out from the default methods in MockRestResponseCreators.
The methods included are common enough that most test packs will probably
have at least one case that uses one of these calls, so it enables
keeping the fluent API tidy.

I added a couple of additional edge cases for common response
statuses that will occur when working in cloud environments, such as
AWS, CloudFlare, or using gateways such as Kong, where resillient
applications should be able to respond to ratelimits, gateway errors,
and gateway timeouts (which may occur if a remote service is down).

Added test cases for any changes made.
@snicoll snicoll changed the title Improve the fluency of the ResponseCreator API [Spring Test] Improve the fluency of the ResponseCreator API Aug 16, 2021
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Nov 10, 2021
@rstoyanchev rstoyanchev self-assigned this Sep 26, 2022
@rstoyanchev rstoyanchev added this to the 6.0.0-RC1 milestone Sep 26, 2022
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 26, 2022
rstoyanchev pushed a commit that referenced this pull request Sep 27, 2022
New methods in DefaultResponseCreator for adding headers and cookies,
for specifying the character encoding when setting a string body on
a response, which is useful when working in environments that do not
automatically assume UTF-8, such as integrating with legacy
applications from a new Spring one.

New methods in MockRestResponseCreators support extra commonly used
HTTP status codes, including some that occur when working in AWS,
CloudFlare, or when using gateways such as Kong, where resilient
applications should be able to respond to ratelimits, gateway errors,
and gateway timeouts that may occur if a remote service is down.

Added test cases for any changes made.

See gh-27280
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants