Skip to content

Commit

Permalink
Merge pull request #54 from companieshouse/feature/IDVA6-1502-handle-…
Browse files Browse the repository at this point in the history
…null-modelAndView-in-TemplateNameInterceptor

IDVA6-1502 handle null modelAndView
  • Loading branch information
griffithsjd authored Sep 4, 2024
2 parents a9d6ad9 + 574fe5b commit 3fabce2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class TemplateNameInterceptor implements HandlerInterceptor {
public void postHandle(HttpServletRequest request, @NonNull HttpServletResponse response,
@NonNull Object handler, ModelAndView modelAndView) {

// Ensure that this is a GET request
if (request.getMethod().equalsIgnoreCase("GET")) {
// Ensure that this is a GET request and a model/view exists
if (request.getMethod().equalsIgnoreCase("GET") && modelAndView != null) {
// Extract the request URI and remove leading '/'
var requestURI = request.getRequestURI().substring(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import uk.gov.companieshouse.common.web.interceptor.TemplateNameInterceptor;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -47,4 +48,21 @@ void postHandle_NotGet() {

assertNull(modelAndView.getModel().get("templateName"));
}

@Test
void postHandle_NullModelAndView() {

var request = new MockHttpServletRequest();
var response = new MockHttpServletResponse();

request.setMethod("GET");
request.setRequestURI("/route-name/page-name");
boolean exceptionThrown = false;
try {
interceptor.postHandle(request, response, new Object(), null);
} catch(Exception e) {
exceptionThrown = true;
}
assertFalse(exceptionThrown);
}
}

0 comments on commit 3fabce2

Please sign in to comment.