Skip to content

Commit

Permalink
Merge pull request #165 from peggy-gov-bc-ca/httpFileNull
Browse files Browse the repository at this point in the history
Process issue #164
  • Loading branch information
alexjoybc authored Jan 9, 2020
2 parents 7b53f73 + ab68cde commit cf8f5b3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ca.bc.gov.open.jrccaccess.autoconfigure.common.Constants;
import org.slf4j.MDC;
import org.springframework.http.HttpStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.HandlerInterceptor;
Expand All @@ -19,7 +20,13 @@ public boolean preHandle(HttpServletRequest req, HttpServletResponse res,
Object handler) throws Exception {
if(req instanceof MultipartHttpServletRequest ){
MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest) req;

MultipartFile file = multiRequest.getFile("file");
if(file == null){
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Expecting file in the request.");
return false;
}

MDC.put(Constants.MDC_KEY_FILENAME,file.getOriginalFilename());
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;
import static org.mockito.Mockito.verify;

public class DocumentInterceptorTests {

Expand All @@ -37,6 +37,14 @@ public void init() throws Exception {
Mockito.when(request.getFile("file")).thenReturn(multipartFile);
}

@Test
public void http_request_no_file_should_reture_bad_request() throws Exception{
MultipartHttpServletRequest badRequest = Mockito.mock(MultipartHttpServletRequest.class);
Mockito.when(badRequest.getFile("file")).thenReturn(null);
assertFalse(interceptor.preHandle(badRequest, response, o ) );
verify(response).sendError(HttpServletResponse.SC_BAD_REQUEST,"Expecting file in the request." );
}

@Test
public void filename_should_be_added_to_MDC_after_preHandle() throws Exception{
interceptor.preHandle(request, response, o);
Expand Down

0 comments on commit cf8f5b3

Please sign in to comment.