Add QueryParams to error redirect from from #5
parent
a284b8f9c8
commit
d6ba02e29e
|
@ -19,8 +19,7 @@ import java.util.List;
|
|||
import java.util.UUID;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("integration")
|
||||
|
@ -86,6 +85,6 @@ public class ForwardMessageFormTest {
|
|||
.param("onError", "http://myerrorsite.com")
|
||||
)
|
||||
.andExpect(status().is3xxRedirection())
|
||||
.andExpect(redirectedUrl("http://myerrorsite.com"));
|
||||
.andExpect(redirectedUrlPattern("http://myerrorsite.com?*"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,8 +11,11 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
|||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Clock;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
@Slf4j
|
||||
|
@ -29,7 +32,7 @@ public class MessageFormController {
|
|||
BindingResult bindingResult) {
|
||||
|
||||
if(bindingResult.hasErrors()) {
|
||||
String onError = (form.getOnError() != null ? form.getOnError() : referer);
|
||||
String onError = formatErrorRedirect(referer, form, bindingResult);
|
||||
return "redirect:" + onError;
|
||||
}
|
||||
|
||||
|
@ -47,6 +50,20 @@ public class MessageFormController {
|
|||
return "redirect:" + redirect;
|
||||
}
|
||||
|
||||
private String formatErrorRedirect(String referer, MessageForm form, BindingResult bindingResult) {
|
||||
String queryParams = bindingResult.getAllErrors().stream()
|
||||
.map(ValidationError::fromObjectError)
|
||||
.map(MessageFormController::mapValidationErrorToQueryParam)
|
||||
.collect(Collectors.joining("&"));
|
||||
String origin = form.getOnError() != null ? form.getOnError() : referer;
|
||||
|
||||
return origin + "?" + URLEncoder.encode(queryParams, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static String mapValidationErrorToQueryParam(ValidationError error) {
|
||||
return String.format("%s=%s", error.getTarget(), error.getMessage());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -44,5 +44,4 @@ class MessageFormControllerTest {
|
|||
.extracting("subject", "content", "from", "timestamp")
|
||||
.containsExactly("Subject", "Message", "sender@company.com", now);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue