I'm new inWireMock
. I am running the WireMock
server as a standalone process. I am able to mock simple rest api, by configuring json files under /mappings
folder. Now, I want to mock a file downloading end point. How can I do that?
I don't know if it helps, the end point is in Java/Spring, which looks like this:
@RequestMapping(value = "xxx/get", method = {RequestMethod.GET})
public ResponseEntity<Object> getFile(HttpServletResponse response) throws Exception {
final Resource fileResource = fileResourceFactoryBean.getObject();
if (fileResource.exists()) {
try (InputStream inputStream = fileResource.getInputStream()) {
IOUtils.copy(inputStream, response.getOutputStream());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + fileResource.getFilename());
response.flushBuffer();
inputStream.close();
return new ResponseEntity<>(HttpStatus.OK);
} catch (Exception e) {
LOGGER.error("Error reading file: ", e);
return new ResponseEntity<Object>("Error reading file.", HttpStatus.BAD_REQUEST);
}
}
return new ResponseEntity<Object>(fileResource.getFilename() + " not found.", HttpStatus.BAD_REQUEST);
}
My response section in the mappings json file looks like this:
"response": {
"status": 200,
"bodyFileName": "fileName", // file under __files directory
"headers": {
"Content-Type": "application/octet-stream"
}
}