Skip to content

Commit

Permalink
feat: customize caller builder
Browse files Browse the repository at this point in the history
  • Loading branch information
1grzyb1 committed May 31, 2024
1 parent 7d655c1 commit 943af3b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import ovh.snet.grzybek.controller.client.core.ControllerClientFactory;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -22,8 +21,7 @@ void customizeRequest() {
controllerClientFactory
.builder(ExampleController.class)
.customizeRequest(
request ->
((MockHttpServletRequestBuilder) request).header("X-Example-Header", "token"))
request -> request.header("X-Example-Header", "token"))
.build();
var response = client.headerExample(null);
assertThat(response.message()).isEqualTo("Header value: token");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,21 @@ void checkInvalidStatusWhenEnumInResponse() {
.thenStatus(HttpStatus.INTERNAL_SERVER_ERROR.value())
.execute();
}

@Test
void addCsrfToken() {
var builder =
controllerClientFactory
.builder(ExampleController.class)
.expectStatus(HttpStatus.OK.value());
var clientCaller = controllerClientFactory.caller(builder);
var result =
(ExampleResponse)
clientCaller
.when(ExampleController::exampleMethod)
.thenStatus(HttpStatus.OK.value())
.execute();

assertThat(result.message()).isEqualTo("Hello world!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
Expand All @@ -37,14 +36,14 @@ class ControllerClient<T> {
private final Class<?> clazz;
private final MockMvc mockMvc;
private final ObjectMapper objectMapper;
private final List<Consumer<RequestBuilder>> requestCustomizers;
private final List<Consumer<MockHttpServletRequestBuilder>> requestCustomizers;
private final List<Function<ResultActions, ResultActions>> resultCustomizers;

public ControllerClient(
Class<?> clazz,
MockMvc mockMvc,
ObjectMapper objectMapper,
List<Consumer<RequestBuilder>> requestCustomizers,
List<Consumer<MockHttpServletRequestBuilder>> requestCustomizers,
List<Function<ResultActions, ResultActions>> resultCustomizers) {
this.clazz = clazz;
this.mockMvc = mockMvc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -47,7 +47,7 @@ public class ControllerClientBuilder<T> {
private final Class<T> clazz;
private final ObjectMapper objectMapper;
private final MockMvc mockMvc;
private final List<Consumer<RequestBuilder>> requestCustomizers = new ArrayList<>();
private final List<Consumer<MockHttpServletRequestBuilder>> requestCustomizers = new ArrayList<>();
private final List<Function<ResultActions, ResultActions>> responseCustomizers =
new ArrayList<>();

Expand All @@ -61,10 +61,10 @@ public class ControllerClientBuilder<T> {
* Adds a customizer for the request. This allows for modification of the request before it is
* sent, such as adding headers or request parameters.
*
* @param consumer a {@link Consumer} that customizes the {@link RequestBuilder}
* @param consumer a {@link Consumer} that customizes the {@link MockHttpServletRequestBuilder}
* @return the current instance of {@code ControllerClientBuilder} for fluent chaining
*/
public ControllerClientBuilder<T> customizeRequest(Consumer<RequestBuilder> consumer) {
public ControllerClientBuilder<T> customizeRequest(Consumer<MockHttpServletRequestBuilder> consumer) {
requestCustomizers.add(consumer);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@ public <T> ControllerClientBuilder<T> builder(Class<T> clazz) {
public <T> ControllerClientCaller<T> caller(Class<T> clazz) {
return new ControllerClientCaller<>(builder(clazz));
}

/**
* Creates a new instance of {@link ControllerClientCaller} for the given builder.
*/
public <T> ControllerClientCaller<T> caller(ControllerClientBuilder<T> builder) {
return new ControllerClientCaller<>(builder);
}
}

0 comments on commit 943af3b

Please sign in to comment.