sahaya / rest-assured

Automatically exported from code.google.com/p/rest-assured
0 stars 0 forks source link

multipart object serialization #166

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to upload a file and a JSON serialized object. 

When I am using the following  approach (reading the json from a file) 
everything is fine: 

    given(). 
        multiPart("document", new File("./data/json.txt"), "application/json"). 
        multiPart("image", new File("./data/image.txt"), "image/ 
jpeg"). 
   ... 

But I am not able to serialize a class into a multiPart, like 

    JsonBean json = new JsonBean(); 
    json.setVal1("Value 1"); 
    json.setVal2("Value 2"); 

and serialize the instance json into a multiPart like

    given(). 
        multiPart("document", json, "application/json"). 
        multiPart("image", new File("./data/image.txt"), "image/ 
jpeg"). 
   ... 

REST Assured 1.6.1 doesn't support this functionality.

I tried to implement the feaute but I was not able to compile rest-assured 
(Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.3:generateStubs 
(default) on project rest-assured: Execution default of goal 
org.codehaus.gmaven:gmaven-plugin:1.3:generateStubs failed: A required class 
was missing while executing 
org.codehaus.gmaven:gmaven-plugin:1.3:generateStubs: AuthenticationScheme).

The untested implementation would be:
- add the following function in RequestSpecificationImpl.groovy

def RequestSpecification multiPart(String name, Object object, String mimeType) 
{
    // assert
    notNull object, "object"

    // serialize
    def serializedObject = SerializableCandidate(object) ?
            ObjectMapping.serialize(object, mimeType, null) :
            object.toString()

    // add it to the multiParts list
    multiParts << new MultiPart(name: name, content: serializedObject, mimeType: mimeType)
    this
}

Original issue reported on code.google.com by MailKont...@gmail.com on 23 Apr 2012 at 1:07

GoogleCodeExporter commented 9 years ago
I managed to compile rest-assured using IDEA. The above code is wrong, 
multiPart(String, Object,..) will not work because otherwise you cannot 
distinguish between File, String and objects to serialize.

Therefore I introduced a new function in Interface RequestSpecification:
    RequestSpecification multiPartObject(String name, Object object, String mimeType);

and changed the implementation to:
    def RequestSpecification multiPartObject(String name, Object object, String mimeType) {
        // assert
        notNull object, "object"

        // serialize
        def serializedObject = isSerializableCandidate(object) ?
                ObjectMapping.serialize(object, mimeType, null) :
                object.toString()

        // add it to the multiParts list
        multiParts << new MultiPart(name: name, content: serializedObject, mimeType: mimeType)
        this
    }

This works, but the Content-Type is not included, which makes problems at the 
server side, something I couldn't solve.

Original comment by MailKont...@gmail.com on 24 Apr 2012 at 10:12

GoogleCodeExporter commented 9 years ago
Hi, 

Thanks for the detailed report and for your effort in trying to fix it. The 
issue with the content-type that is not included could be related to issue 167.

Original comment by johan.ha...@gmail.com on 26 Apr 2012 at 7:08

GoogleCodeExporter commented 9 years ago
Thank you! Maybe you find a better name than "multiPartObject".

Just a note: your unit tests (XML serialization) are platform dependent. On 
windows you got "\r\n", which leads to an assert failure. During the 
implementation I disabled the tests.

Original comment by MailKont...@gmail.com on 26 Apr 2012 at 7:41

GoogleCodeExporter commented 9 years ago
Thanks for pointing. Actually there are worse problems with the tests since 
they only work in Maven 2 (at least for me) because all tests extend from the 
"WithJetty" which is Maven 2 dependent. The problem is, as always, to find time 
to fix it :)

Original comment by johan.ha...@gmail.com on 26 Apr 2012 at 7:56

GoogleCodeExporter commented 9 years ago

Original comment by johan.ha...@gmail.com on 9 May 2012 at 2:15

GoogleCodeExporter commented 9 years ago
I've now fixed this in trunk. If you want to try it out depend on version 
1.6.2-SNAPSHOT after having added the following repo to your pom.xml:

<repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots />
        </repository>
</repositories>

Original comment by johan.ha...@gmail.com on 23 May 2012 at 11:27

GoogleCodeExporter commented 9 years ago
Works! Thank you very much!

Original comment by MailKont...@gmail.com on 30 May 2012 at 12:03