1
1
package com .cosium .hal_mock_mvc ;
2
2
3
+ import static com .cosium .hal_mock_mvc .OrMatcher .anyOf ;
3
4
import static java .util .Objects .requireNonNull ;
4
5
import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
6
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
5
7
6
8
import com .fasterxml .jackson .annotation .JsonCreator ;
7
9
import com .fasterxml .jackson .annotation .JsonProperty ;
14
16
import java .util .Optional ;
15
17
import java .util .stream .Collectors ;
16
18
import org .springframework .hateoas .MediaTypes ;
17
- import org .springframework .mock .web .MockHttpServletResponse ;
18
- import org .springframework .test .web .servlet .MvcResult ;
19
19
import org .springframework .test .web .servlet .ResultActions ;
20
20
21
21
/**
@@ -30,27 +30,17 @@ public class Templates {
30
30
Templates (RequestExecutor requestExecutor , ResultActions resultActions ) throws Exception {
31
31
this .requestExecutor = requireNonNull (requestExecutor );
32
32
33
- resultActions .andExpect (content ().contentType (MediaTypes .HAL_FORMS_JSON ));
33
+ resultActions
34
+ .andExpect (anyOf (status ().is2xxSuccessful (), status ().is3xxRedirection ()))
35
+ .andExpect (content ().contentType (MediaTypes .HAL_FORMS_JSON ));
34
36
35
- MvcResult mvcResult = resultActions .andReturn ();
36
- MockHttpServletResponse response = mvcResult .getResponse ();
37
- int responseStatus = response .getStatus ();
38
- String jsonBody = response .getContentAsString ();
39
- if (responseStatus < 200 || responseStatus >= 400 ) {
40
- throw new IllegalStateException (
41
- "GET on "
42
- + mvcResult .getRequest ().getRequestURI ()
43
- + " failed with code "
44
- + responseStatus
45
- + " and body '"
46
- + jsonBody
47
- + "'" );
48
- }
49
37
objectMapper =
50
38
new ObjectMapper ()
51
39
.disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES )
52
40
.registerModule (new JacksonModule ());
53
- body = objectMapper .readValue (jsonBody , HalFormsBody .class );
41
+ body =
42
+ objectMapper .readValue (
43
+ resultActions .andReturn ().getResponse ().getContentAsString (), HalFormsBody .class );
54
44
}
55
45
56
46
public Optional <Template > byOptionalKey (String key ) {
0 commit comments