Function Unit Test sending multipart/form-data to a Mule flow - mule

I am using JMeter to send Multipart Messages like this:
Raw Post body
Body
Send files with the request
path Parameter Name Mime type
file1.jmx Payload multipart/form-data
test.jpg Attachment
However we would like to move this over to a Function Unit Test. Is there a best practice way to do this in Mule? Do I just send a mule message with attachments? Would this be a good replication of a http Multipart message?
msg.addAttachment(name, dataHandler)
MuleMessage result = client.send("http://localhost:8090/", msg, 200 * 1000);

For this type of tests, I like to use Apache's Common HTTP Client because it is independent of Mule (so it simulates a "neutral" HTTP client) and it's easy to use from a functional test method.

Related

The request does not contain a \"file\" part or \"metadata\" part

I have gone through number of forums including the current forum as well, but I couldn't find concrete answer.
Problem Description: Mule sub flow expects JSON (Content-Type=application/json) as input. An attachment with input JSON, should be sent across with http request to third party REST Service.
Following is the source code used.
Sourcecode of Mule subflow
But the third party REST service is responding with Bad request with error message as "code": 400,. "error": "The request does not contain a \"file\" part or \"metadata\" part. However when tried with Postman, the request was successful as shown below
Request successful in Postman
what is wrong with the code?
First, do not specify the Content-Type since the multipart format requires a special boundary section. The proper header will be generated for you once an attachment is present in the Mule Message. If you hard code it, you will have problems.
Second, if the attachment is suppose to be a JSON, then put application/json as the attachment contentType instead of application/octet-stream.
Third, if the problems persists I would suggest enabling HTTP wire logging (<AsyncLogger name="org.mule.module.http.internal.HttpMessageLogger" level="DEBUG"/>) to actually compare the Postman and Mule requests.

Karate support for validating pipe separated response and avro format

We have webservice where we send the request body as binary (avro format) through postman and have a pipe separated response. We were able send this request using karate and get a valid response but made observation that by default karate sets the content type as ‘application/octect-stream’ where as we expect content type to be application/json. Is there a reason why karate uses application/octect-stream for avro format?
Also for pipe separated response does karate support any default validations similar to json/xml validations?
For e.g : if the sample response is like
|PDT|! PRODUCT!|Item1|!SKUID!|sku123a|!DETAILS!|Itemname|itemnumber|price|!FINISH!|
Karate tries to guess the content-type but clearly needs help here, please refer to the documentation for the header keyword, and you can set (over-ride) it easily: https://github.com/intuit/karate#header
For the second question, I suggest you write a simple utility to convert that response to JSON and then you will be able to do all kinds of awesome validations in Karate. This example should be self-explanatory: dogs.feature

Sending GET request parameters in body

I have an API which accepts many parameters.
Passing the values of the parameters will exceed the length of the URL Header.
I am using Postman client to pass the parameters in the body but this is not working any ideas on how to make this work.
The API accepts many parameters because the backend is legacy and is exposed as an API by a service bus.
Older versions of Postman didn't allow you to send body data with get request.
Yet, if your server receives data as URL parameters you won't be able just to change the way of sending them and include them to the body (server won't accept them).
So if the length of parameters is indeed so big and the server indeed can receive the same data from body instead of from parameters then the Postman is just not the tool that you can use (maybe cURL is for you).
If your server allows to send data only as URL parameters and they are so long (more then 2000 chars What is the maximum length of a URL in different browsers?) then I think you have no chances to test this API.
UPDATE: new Version 7.20.1 now allows to send Body with GET request
Workaround:
Change the request type to POST.
Set the value of your body
Change request type to GET
Send request and the body is included
Postman is already added this feature of sending body in get
request.
But i still i recommended to go for post request (if body is present) since many projects like angular http client does't have updated protocols yet.
Latest Postman supports body object for Get request
just choose json format as shown in pic above
If you want to make a GET request in Postman then you can use Params or Body to pass parameters, but not both. Either Params only or Body only. If you specify both Params and Body, Postman will select and send only Params (in GET request of course). So if you want it to send Body, clear Params.

msf4j chunked encoding and multipart/form-data

I've been running some of the examples for MSF4J. Im looking into creating a service that allows uploading of files, but they are send using multipart/form-data. Our front sends the data with chunked encoding.
So, the FileServer example shows how to handle chunked streams with the HttpStreamer and the Formparam examples show how to handle multipart/form-data. But when I send a chunked request to the /simpleFormStreaming, it doesn't work (get a HTTP 500 response). When sending an non-chunked request (Content-Lenght is set). It does work ok.
So how can I handle a multipart/form-data request that is send using chunked encoding?
Thanks,
Danny
At the moment MSF4J doesn't support chunked data with FormParam. But you can use the HttpStreamer as in FileServer sample.
HttpStreamer.chunk method will get execute for each and every chunk. You need to implement the multipart/form-data handling logic in your HttpStreamHandler implementation. I think you can use commons-file-upload directly and do the processing.

spray autochunked requests with multipart/form-data

I'm using spray-can and spray-routing to support a REST service that includes an operation to upload files. This operation accepts multipart/form-data and the formFields directive works well.
When I try to use the spray request chunking support though, I find that the formFields directive is not working as the whole multipart message is chunked and not just the one part that is large.
Has anyone got any advice on how the handle large multipart messages in spray-can?
All I can think of right now is to put the whole request data in a temp file and to use something like org.apache.commons.fileupload.MultipartStream to parse the request.