Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ public void initBinder(WebDataBinder binder) {
binder.addValidators(new PayloadValidator());
}

@RequestMapping("/")
public String emptyBody(@RequestBody @Validated Payload payload) {
@RequestMapping("/mandatory-body")
public String mandatoryBody(@RequestBody @Validated Payload payload) {
return "hello";
}

@RequestMapping("/optional-body")
public String optionalBody(@RequestBody(required = false) @Validated Payload payload) {
return "hello";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,31 @@ public void prepare() {

@Test
public void shouldSend200WithValidPayload() throws Exception {
mockMvc.perform(post("/").content("{\"message\":\"spring\"}").contentType(MediaType.APPLICATION_JSON))
mockMvc.perform(post("/mandatory-body").content("{\"message\":\"spring\"}").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("\"hello\""));
}

@Test
public void shouldSend400WithEmptyBody() throws Exception {
mockMvc.perform(post("/")).andExpect(status().isBadRequest());
mockMvc.perform(post("/mandatory-body")).andExpect(status().isBadRequest());
}

@Test
public void shouldSend400WithEmptyJsonObject() throws Exception {
mockMvc.perform(post("/").content("{}").contentType(MediaType.APPLICATION_JSON))
mockMvc.perform(post("/mandatory-body").content("{}").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
}

@Test
public void shouldSend400WithNullJsonObject() throws Exception {
mockMvc.perform(post("/mandatory-body").content("null").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
}

@Test
public void shouldSend200WithNullJsonObject() throws Exception {
mockMvc.perform(post("/optional-body").content("null").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}