I am new to wiremock and trying to stub the invocation of the following springboot restful endpoint.
#PostMapping(path = "/template/pdf", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<String> bindData(
#ApiParam(value = "BindDataRequest payload", required = true)
#RequestParam String template, #RequestParam String templateDataAsJson) throws IOException {
//Some code
return ResponseEntity.ok("xyz");
}
**The following basic logic works:**
templatingService.stubFor(
post(urlEqualTo("/template/pdf"))
.willReturn(aResponse().withBody(JSON_INPUT_TO_PDF_GEN).withStatus(200)));
But, i need a way of setting the 2 string request parameters before invoking .willReturn(.....)
I have tried :
templateBinderService.stubFor(
post(urlEqualTo("/template/pdf"))
.withRequestBody(WireMock.equalTo("jixhcjxhcjxhcxhchx"))
.withRequestBody(WireMock.equalTo("nhhhxhxhhhhhxhhhh"))
.willReturn(aResponse().withBody(JSON_INPUT_TO_HTML2PDF_GEN).withStatus(200)));
But got:
org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found
//I have also tried:
templateBinderService.stubFor(
post(urlEqualTo("/template/test"))
.withRequestBody(containing("param1-value"))
.withRequestBody(containing("param2-value"))
.willReturn(aResponse().withBody("i-am-a-response").withStatus(200)));
//I have also tried:
templateBinderService.stubFor(
post(urlEqualTo("/template/test"))
.withRequestBody(equalToJson("{}"))
.willReturn(aResponse().withBody("i-am-a-response").withStatus(200)));
Please help with code snippet or reference.
Since both the parameters template and templateDataAsJson are annotated with #RequestParam, they should be passed accordingly in the wiremock stub as below.
templatingService.stubFor(
post(urlEqualTo("/template/pdf?template=value1&templateDataAsJson=value2"))
.willReturn(aResponse().withBody(JSON_INPUT_TO_PDF_GEN).withStatus(200)));
where value1 and value2 are the respective values for both parameters.
Related
Sometimes we send a POST HTTP request with POST payload to an endpoint with URL variable, for example:
[POST] http://example.com/update-item?itemid=123456
To get the POST payload in the Spring controller class, I can do something this:
#RequestMapping(value = "/update-item", method = RequestMethod.POST)
public String updateItem(#RequestBody Item json) {
//some logics
return "/update-item-result";
}
However, at the same time, how can I get the variable from the URL (i.e. itemid in the above example) even for method = RequestMethod.POST?
I see a lot of Spring MVC examples on the web either get the GET variables from the URL or the POST variables from the payload, but I never see getting both in action.
You can use multiple HTTP requests by specifying the method attribute as an array in the #RequestMapping annotation.
#RequestMapping(value = "/update-item", method = {RequestMethod.POST,RequestMethod.GET})
public String updateItem(#RequestBody Item json) {
//some logics
return "/update-item-result";
}
I'm working with Facebook messenger app (chatbot) and I want to see what GET request I'm receiving from it. I'm using Spring Framework to start http server and ngrok to make it visible for facebook.
Facebook sending webhooks to me and i receive them, but i don't understand how to extract data from this request. Here what i get when I try HttpRequest to receive GET request. ngrok screenshot (error 500).
When I tried without HttpRequest, i had response 200 (ok).
What do i need to put to parameters of my find method to see GET request data?
My code:
#RestController
public class botAnswer {
#RequestMapping(method= RequestMethod.GET)
public String find(HttpRequest request) {
System.out.println(request.getURI());
String aaa = "222";
return aaa;
}
}
I guess HttpRequest will not help you here. For simplicity, just change HttpRequest to HttpServletRequest. You can access all query string parameters from it using request.getParameter("..."). Something like the following should work:
#RequestMapping(value = "/", method = RequestMethod.GET)
public String handleMyGetRequest(HttpServletRequest request) {
// Reading the value of one specific parameter ...
String value = request.getParameter("myParam");
// or all parameters
Map<String, String[]> params = request.getParameterMap();
...
}
This blog post shows how to use the #RequestParam annotation as an alternative to reading the parameters from HttpServletRequest directly.
I would like to test file uploading by REST API. I found it quite confusing to send file as RequestParam instead of RequestBody.
Controller method:
#PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public
#ResponseBody
ResponseEntity<String> uploadFile(
#RequestParam(name = "file") MultipartFile multipartFile,
#RequestParam(name = "path") String path) {
logger.debug("File upload REST requested");
return new ResponseEntity<>(fileService.uploadFile(
multipartFile, path),
HttpStatus.OK);
}
1.Now I would like to test it and I've used mocks. While debugging it, I see that mock service working but method exactly with this arguments is not invoked, so the test if failed. How could I handle this?
#Test
public void testUploadFile() throws Exception {
String mockName = "mock";
MockMultipartFile mockMultipartFile = new MockMultipartFile(mockName, mockName.getBytes());
when(mockFileService.uploadFile(mockMultipartFile, rootDir)).thenReturn("success");
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/files/upload")
.file("file", mockMultipartFile.getBytes())
.param("path", rootDir))
.andExpect(status().isOk())
.andExpect(content().string("success"));
verify(mockFileService, times(1)).updateFile(mockMultipartFile, rootDir);
verifyNoMoreInteractions(mockFileService);
}
2.How could I test this with Postman? I see that I can send file in Body, but how could I send it as param?
EDIT:
I've changed the method, but the problem is not there:
Argument(s) are different! Wanted:
mockFileService.uploadFile(
org.springframework.mock.web.MockMultipartFile#61bd0845,
"/"
);
Looks like method are using 2 instances of MockMultipartFile. And the second question is still open, how could this method be tested from Postman?
Yes, the test case fails, because
the problem is in your test case at the end, you are verifying the call to updateFile() which is incorrect as in your controller you are using uploadFile(), you MUST use the same method to verify.
So, you need to change the verify line as below:
verify(mockFileService, times(1)). uploadFile(mockMultipartFile, rootDir);
In other words, Mockito verify validates the number of times a method is invoked as you are trying to verify the call on a different method (not being used in controller), it is failing.
I have a request handler for which I would like to skip json processing and retrieve the request body as a string. Eg -
#RequestMapping(value = "/webhook", method = RequestMethod.POST)
public void webHook(#RequestBody String body) {
}
However, the above method definition doesnt work as Spring forcibly tries to parse the posted string as json and thus throws an exception.
How do i tell spring to skip json processing for this request?
use like this it'll work.
#RequestMapping(value = "/webhook", method = RequestMethod.POST)
public void webHook(HttpServletRequest request) {
String body = IOUtils.toString( request.getInputStream());
// do stuff
}
Not using #RequestBody is key here. When spring sees #RequestBody it tries to map the entire body as object.
I have this piece of code:
#RequestMapping(value = "/test.json", method = RequestMethod.GET)
#ResponseStatus(HttpStatus.OK)
public #ResponseBody Object[] generateFile(#RequestParam String tipo) {
Object[] variaveis = Variavel.getListVariavelByTipo(tipo);
return variaveis;
}
As far as I know it should take a request to test.json?tipo=H and return the JSON representation of Variavel[], however when I make such request I get:
HTTP Status 406 -
type Status report
message
descriptionThe resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()
By using the following function I can get the expected json:
#RequestMapping(value = "/teste.json")
public void testeJson(Model model, #RequestParam String tipo) {
model.addAttribute("data", Variavel.getListVariavelByTipo("H"));
}
What I'm doing wrong?
#RequestBody/#ResponseBody annotations don't use normal view resolvers, they use their own HttpMessageConverters. In order to use these annotations, you should configure these converters in AnnotationMethodHandlerAdapter, as described in the reference (you probably need MappingJacksonHttpMessageConverter).