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 java.util.UUID;
|
||||||
|
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
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.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@ActiveProfiles("integration")
|
@ActiveProfiles("integration")
|
||||||
|
@ -86,6 +85,6 @@ public class ForwardMessageFormTest {
|
||||||
.param("onError", "http://myerrorsite.com")
|
.param("onError", "http://myerrorsite.com")
|
||||||
)
|
)
|
||||||
.andExpect(status().is3xxRedirection())
|
.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 org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Clock;
|
import java.time.Clock;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -29,7 +32,7 @@ public class MessageFormController {
|
||||||
BindingResult bindingResult) {
|
BindingResult bindingResult) {
|
||||||
|
|
||||||
if(bindingResult.hasErrors()) {
|
if(bindingResult.hasErrors()) {
|
||||||
String onError = (form.getOnError() != null ? form.getOnError() : referer);
|
String onError = formatErrorRedirect(referer, form, bindingResult);
|
||||||
return "redirect:" + onError;
|
return "redirect:" + onError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +50,20 @@ public class MessageFormController {
|
||||||
return "redirect:" + redirect;
|
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")
|
.extracting("subject", "content", "from", "timestamp")
|
||||||
.containsExactly("Subject", "Message", "sender@company.com", now);
|
.containsExactly("Subject", "Message", "sender@company.com", now);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue