Closed slackfarmer closed 8 years ago
I figured out how to do it, by using an apache library(MultipartEntityBuilder) to compose the multipart body.
dependencies {
compile('org.apache.httpcomponents:httpmime:4.4')
...
returns end point and a byte array
public static Map getTestAssetProperties(String assetName){
String destPath = "${REPO_TEST_ASSET_PATH}${assetName}"
String endPoint = "/api/repo/upload/${destPath}"
File asset = new File("./src/test/resources/${assetName}" as String)
MultipartEntityBuilder e = new MultipartEntityBuilder()
e.setBoundary("fnord")
e.addTextBody('filename', asset.getName())
e.addBinaryBody("fileUpload", asset)
ByteArrayOutputStream baos = new ByteArrayOutputStream()
e.build().writeTo(baos)
return [endPoint:endPoint, bodyInBytes: baos.toByteArray()]
}
Notice content type settings: ['Content-Type': 'multipart/form-data; boundary=fnord']
def "Execute revisions request"() {
setup:
String assetName = "testAsset1.jpg" as String
def assetProps = SetupTeardownUtils.getTestAssetProperties(assetName)
// upload it five times
for (i in 1..5) {
def result = sendRequest(assetProps.endPoint,'PUT',['Content-Type': 'multipart/form-data; boundary=fnord'], assetProps.bodyInBytes)
println "setup is uploading 5 test assets. Endpoint:[${assetProps.endPoint}] status:[${result.status}]"
}
when:
sendRequest('/api/repo/revisions/test/static/asset/upload/testAsset1.jpg', 'GET', ['Content-Type': V1_JSON])
then:
response.status == 200
response.contentAsString == '{"truncated":"false","status":{"success":"true"}}'
response.getHeader('Content-Type').equals(V1_JSON)
cleanup:
println "deleting the 5 test assets"
}
Hello slackfarmer,
Could you please help me sharing how you did your resource to accept a MultipartFile.
I'm using this plugin in this verison: compile ":jaxrs:0.11"
But until now I couldn't send a file to my resource.
In advance I thank you.
hello @marciomalewschik I am too facing same problem. Did you find solution ?
This issue was moved to budjb/grails-jaxrs#21
I was curious how one might go about creating an IntegrationTestSpec that would compose a multipartFormData request? Does anyone have an example of how to build up a multipart form object and convert it to bytes using this existing jaxrs framework?
Currently send request will only accept bytes as input
IntegrationTestSpec.groovy....
Jersey Client Example - how I might test multipart form using jersey client