This repository was archived by the owner on Dec 15, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +22
-5
lines changed
main/java/org/springframework/issues
test/java/org/springframework/issues Expand file tree Collapse file tree 2 files changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -15,8 +15,13 @@ public void initBinder(WebDataBinder binder) {
15
15
binder .addValidators (new PayloadValidator ());
16
16
}
17
17
18
- @ RequestMapping ("/" )
19
- public String emptyBody (@ RequestBody @ Validated Payload payload ) {
18
+ @ RequestMapping ("/mandatory-body" )
19
+ public String mandatoryBody (@ RequestBody @ Validated Payload payload ) {
20
+ return "hello" ;
21
+ }
22
+
23
+ @ RequestMapping ("/optional-body" )
24
+ public String optionalBody (@ RequestBody (required = false ) @ Validated Payload payload ) {
20
25
return "hello" ;
21
26
}
22
27
}
Original file line number Diff line number Diff line change @@ -24,19 +24,31 @@ public void prepare() {
24
24
25
25
@ Test
26
26
public void shouldSend200WithValidPayload () throws Exception {
27
- mockMvc .perform (post ("/" ).content ("{\" message\" :\" spring\" }" ).contentType (MediaType .APPLICATION_JSON ))
27
+ mockMvc .perform (post ("/mandatory-body " ).content ("{\" message\" :\" spring\" }" ).contentType (MediaType .APPLICATION_JSON ))
28
28
.andExpect (status ().isOk ())
29
29
.andExpect (content ().string ("\" hello\" " ));
30
30
}
31
31
32
32
@ Test
33
33
public void shouldSend400WithEmptyBody () throws Exception {
34
- mockMvc .perform (post ("/" )).andExpect (status ().isBadRequest ());
34
+ mockMvc .perform (post ("/mandatory-body " )).andExpect (status ().isBadRequest ());
35
35
}
36
36
37
37
@ Test
38
38
public void shouldSend400WithEmptyJsonObject () throws Exception {
39
- mockMvc .perform (post ("/" ).content ("{}" ).contentType (MediaType .APPLICATION_JSON ))
39
+ mockMvc .perform (post ("/mandatory-body " ).content ("{}" ).contentType (MediaType .APPLICATION_JSON ))
40
40
.andExpect (status ().isBadRequest ());
41
41
}
42
+
43
+ @ Test
44
+ public void shouldSend400WithNullJsonObject () throws Exception {
45
+ mockMvc .perform (post ("/mandatory-body" ).content ("null" ).contentType (MediaType .APPLICATION_JSON ))
46
+ .andExpect (status ().isBadRequest ());
47
+ }
48
+
49
+ @ Test
50
+ public void shouldSend200WithNullJsonObject () throws Exception {
51
+ mockMvc .perform (post ("/optional-body" ).content ("null" ).contentType (MediaType .APPLICATION_JSON ))
52
+ .andExpect (status ().isOk ());
53
+ }
42
54
}
You can’t perform that action at this time.
0 commit comments