Hello I am no sure if this is a bug or I am not properly using MockMvc for my controller tests.
I have implemented this regex validation for my GetMapping controller which receives two String params with the follow format "YYYY-MM-DD".
@Component
public class PatterMatcher {
private static Pattern DATE_PATTERN = Pattern.compile(
"^((19|2[0-9])[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$");
public boolean matches(String date) {
return DATE_PATTERN.matcher(date).matches();
}
}
Then my controller makes the bellow validation before processing the request:
boolean startDateMatch = patterMatcher.matches(startDate);
boolean endDateMatch = patterMatcher.matches(endDate);
if (!startDateMatch) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(new FailResponse("failed", "startDate format is invalid"));
}
if (!endDateMatch) {
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(new FailResponse("failed", "endDate format is invalid"));
}
Thing is MockMvc always fails this validation no matters the way I construct the request. The test currently looks like this:
LinkedMultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("startDate", "2021-11-01");
parameters.add("endDate", "2021-11-01");
MvcResult result = mockMvc
.perform(MockMvcRequestBuilders.get("/api/tickets").queryParams(parameters))
.andExpect(status().isOk()).andReturn();
System.out.println(result);
It always returns my custom ResponseEntity from my PatterMatcher regex validation:
ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(new FailResponse("failed", "endDate format is invalid"));
This is the complete response:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /api/tickets
Parameters = {startDate=[2021-11-01], endDate=[2021-11-01]}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = com.assessment.joseassessment.controllers.TicketsController
Method = com.assessment.joseassessment.controllers.TicketsController#getController(String, String)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 400
Error message = null
Headers = [Content-Type:"application/json"]
Content type = application/json
Body = {"status":"failed","reason":"startDate format is invalid"}
Forwarded URL = null
Redirected URL = null
Cookies = []
I suspect it is because the []
brackets on here Parameters = {startDate=[2021-11-01], endDate=[2021-11-01]}
but not sure about it.
Update----------------
OK, I just added one initial "[" and one final "]" on my pattern so it matches the result output of the MockHttpServletRequest like "^\[((19|2[0-9])[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\]$" but the validation still fails so no idea why this validation regex patter works everywhere except using MockMvc for testing.
System.out.println("[2021-11-01]".matches(
"^\\[((19|2[0-9])[0-9]{2})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\\]$"));
Output = true
Thanks the great job you do.
Comment From: sbrannen
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.