|
| 1 | +/* |
| 2 | + * Copyright 2019-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mattbertolini.spring.web.reactive.test; |
| 18 | + |
| 19 | +import com.mattbertolini.spring.test.web.bind.DirectFieldAccessController; |
| 20 | +import com.mattbertolini.spring.web.reactive.bind.config.BinderConfiguration; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | +import org.springframework.format.support.FormattingConversionService; |
| 26 | +import org.springframework.http.MediaType; |
| 27 | +import org.springframework.lang.NonNull; |
| 28 | +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; |
| 29 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 30 | +import org.springframework.validation.Validator; |
| 31 | +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
| 32 | +import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; |
| 33 | +import org.springframework.web.context.WebApplicationContext; |
| 34 | +import org.springframework.web.reactive.config.WebFluxConfigurationSupport; |
| 35 | + |
| 36 | +import static org.springframework.web.reactive.function.BodyInserters.fromFormData; |
| 37 | + |
| 38 | +@SpringJUnitWebConfig(classes = {DirectFieldAccessIntegrationTest.Context.class}) |
| 39 | +public class DirectFieldAccessIntegrationTest { |
| 40 | + |
| 41 | + private WebTestClient webTestClient; |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void setUp(WebApplicationContext webApplicationContext) { |
| 45 | + webTestClient = WebTestClient.bindToApplicationContext(webApplicationContext).build(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void cookieParameter() { |
| 50 | + webTestClient.get() |
| 51 | + .uri("/cookieParameter") |
| 52 | + .accept(MediaType.TEXT_PLAIN) |
| 53 | + .cookie("cookie_parameter", "expectedValue") |
| 54 | + .exchange() |
| 55 | + .expectStatus().isOk() |
| 56 | + .expectBody(String.class).isEqualTo("expectedValue"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void formParameter() { |
| 61 | + webTestClient.post() |
| 62 | + .uri("/formParameter") |
| 63 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 64 | + .body(fromFormData("form_parameter", "expectedValue")) |
| 65 | + .accept(MediaType.TEXT_PLAIN) |
| 66 | + .exchange() |
| 67 | + .expectStatus().isOk() |
| 68 | + .expectBody(String.class).isEqualTo("expectedValue"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void headerParameter() { |
| 73 | + webTestClient.get() |
| 74 | + .uri("/headerParameter") |
| 75 | + .accept(MediaType.TEXT_PLAIN) |
| 76 | + .header("header_parameter", "expectedValue") |
| 77 | + .exchange() |
| 78 | + .expectStatus().isOk() |
| 79 | + .expectBody(String.class).isEqualTo("expectedValue"); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void pathParameter() { |
| 84 | + webTestClient.get() |
| 85 | + .uri("/pathParameter/expectedValue") |
| 86 | + .accept(MediaType.TEXT_PLAIN) |
| 87 | + .exchange() |
| 88 | + .expectStatus().isOk() |
| 89 | + .expectBody(String.class).isEqualTo("expectedValue"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void requestParameter() { |
| 94 | + webTestClient.get() |
| 95 | + .uri("/requestParameter?request_parameter=expectedValue") |
| 96 | + .accept(MediaType.TEXT_PLAIN) |
| 97 | + .exchange() |
| 98 | + .expectStatus().isOk() |
| 99 | + .expectBody(String.class).isEqualTo("expectedValue"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void sessionParameter() { |
| 104 | + webTestClient.mutateWith(SessionMutator.session() |
| 105 | + .attribute("session_parameter", "expectedValue")) |
| 106 | + .get().uri("/sessionParameter") |
| 107 | + .accept(MediaType.TEXT_PLAIN) |
| 108 | + .exchange() |
| 109 | + .expectStatus().isOk() |
| 110 | + .expectBody(String.class).isEqualTo("expectedValue"); |
| 111 | + } |
| 112 | + |
| 113 | + @Configuration |
| 114 | + static class Context extends WebFluxConfigurationSupport { |
| 115 | + |
| 116 | + @Override |
| 117 | + @NonNull |
| 118 | + protected ConfigurableWebBindingInitializer getConfigurableWebBindingInitializer(@NonNull FormattingConversionService mvcConversionService, @NonNull Validator mvcValidator) { |
| 119 | + ConfigurableWebBindingInitializer initializer = super.getConfigurableWebBindingInitializer(mvcConversionService, mvcValidator); |
| 120 | + initializer.setDirectFieldAccess(true); |
| 121 | + return initializer; |
| 122 | + } |
| 123 | + |
| 124 | + @Bean |
| 125 | + public BinderConfiguration binderConfiguration() { |
| 126 | + return new BinderConfiguration(); |
| 127 | + } |
| 128 | + |
| 129 | + @Bean |
| 130 | + public LocalValidatorFactoryBean validator() { |
| 131 | + return new LocalValidatorFactoryBean(); |
| 132 | + } |
| 133 | + |
| 134 | + @Bean |
| 135 | + public DirectFieldAccessController controller() { |
| 136 | + return new DirectFieldAccessController(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments