|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.web.configurers;
|
18 | 18 |
|
19 |
| -import java.io.IOException; |
| 19 | +import java.nio.charset.StandardCharsets; |
20 | 20 | import java.util.List;
|
21 | 21 |
|
22 | 22 | import org.junit.jupiter.api.Test;
|
|
25 | 25 | import org.springframework.beans.factory.annotation.Autowired;
|
26 | 26 | import org.springframework.context.annotation.Bean;
|
27 | 27 | import org.springframework.context.annotation.Configuration;
|
28 |
| -import org.springframework.http.HttpInputMessage; |
29 | 28 | import org.springframework.http.HttpOutputMessage;
|
30 |
| -import org.springframework.http.converter.AbstractHttpMessageConverter; |
31 | 29 | import org.springframework.http.converter.HttpMessageConverter;
|
32 |
| -import org.springframework.http.converter.HttpMessageNotReadableException; |
33 |
| -import org.springframework.http.converter.HttpMessageNotWritableException; |
34 | 30 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
35 | 31 | import org.springframework.security.config.Customizer;
|
36 | 32 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
53 | 49 | import static org.hamcrest.Matchers.containsString;
|
54 | 50 | import static org.mockito.ArgumentMatchers.any;
|
55 | 51 | import static org.mockito.BDDMockito.given;
|
| 52 | +import static org.mockito.BDDMockito.willAnswer; |
56 | 53 | import static org.mockito.Mockito.mock;
|
57 | 54 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
58 | 55 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
@@ -153,29 +150,19 @@ public void webauthnWhenConfiguredMessageConverter() throws Exception {
|
153 | 150 | WebAuthnRelyingPartyOperations rpOperations = mock(WebAuthnRelyingPartyOperations.class);
|
154 | 151 | ConfigMessageConverter.rpOperations = rpOperations;
|
155 | 152 | given(rpOperations.createPublicKeyCredentialCreationOptions(any())).willReturn(options);
|
156 |
| - HttpMessageConverter<Object> converter = new AbstractHttpMessageConverter<>() { |
157 |
| - @Override |
158 |
| - protected boolean supports(Class<?> clazz) { |
159 |
| - return true; |
160 |
| - } |
161 |
| - |
162 |
| - @Override |
163 |
| - protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) |
164 |
| - throws IOException, HttpMessageNotReadableException { |
165 |
| - return null; |
166 |
| - } |
167 |
| - |
168 |
| - @Override |
169 |
| - protected void writeInternal(Object o, HttpOutputMessage outputMessage) |
170 |
| - throws IOException, HttpMessageNotWritableException { |
171 |
| - outputMessage.getBody().write("123".getBytes()); |
172 |
| - } |
173 |
| - }; |
| 153 | + HttpMessageConverter<Object> converter = mock(HttpMessageConverter.class); |
| 154 | + given(converter.canWrite(any(), any())).willReturn(true); |
| 155 | + String expectedBody = "123"; |
| 156 | + willAnswer((args) -> { |
| 157 | + HttpOutputMessage out = (HttpOutputMessage) args.getArguments()[2]; |
| 158 | + out.getBody().write(expectedBody.getBytes(StandardCharsets.UTF_8)); |
| 159 | + return null; |
| 160 | + }).given(converter).write(any(), any(), any()); |
174 | 161 | ConfigMessageConverter.converter = converter;
|
175 | 162 | this.spring.register(ConfigMessageConverter.class).autowire();
|
176 | 163 | this.mvc.perform(post("/webauthn/register/options"))
|
177 | 164 | .andExpect(status().isOk())
|
178 |
| - .andExpect(content().string("123")); |
| 165 | + .andExpect(content().string(expectedBody)); |
179 | 166 | }
|
180 | 167 |
|
181 | 168 | @Configuration
|
|
0 commit comments