djangid / rest-assured

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

Support for JSonBuilder in RequestSpecification.body method #244

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I guess it more an enhancement but it would be nice to be hable to do:

        JsonBuilder json = new JsonBuilder()
        json {
            name "myname"
            description "mydesc"
        }

        def request = testSpec.getRequestSpecification()
                .contentType("application/json")
                .header("Accept", "application/json")
                .body(json)
Any plan on it?

Original issue reported on code.google.com by corinnek...@gmail.com on 6 Jun 2013 at 4:35

GoogleCodeExporter commented 8 years ago
Well I don't have any plans to implement it at the moment since REST Assured is 
supposed to be a  Java project so first off all your code wouldn't compile and 
secondly RA would have to expose parts of the Groovy API. 

I can totally see the benefits of this if you're using Groovy though. Perhaps 
you could create a Groovy extendo method and add that to the RA API which then 
converts the JsonBuilder to a string or something which is sent to RA.

Original comment by johan.ha...@gmail.com on 7 Jun 2013 at 5:40

GoogleCodeExporter commented 8 years ago
Hi,
This will work 

JsonBuilder json = new JsonBuilder()
def request = testSpec.getRequestSpecification()
                .contentType("application/json")
                .header("Accept", "application/json")
                .body(json  {
                  name "MyApp"
                  description  "awesome app"
                  }
                )

You can also pass it as a map : 
.body(["name":"ddd", description:"ddd"])

Original comment by scm.bl...@gmail.com on 7 Jun 2013 at 6:45

GoogleCodeExporter commented 8 years ago
Cool I didn't know this!

Original comment by johan.ha...@gmail.com on 7 Jun 2013 at 6:46

GoogleCodeExporter commented 8 years ago
Really wierd the syntax proposed by Sebastien will work but if changed to:

        JsonBuilder json = new JsonBuilder()
        json  {
            name "MyApp"
            description  "awesome app"
        }
        def request = testSpec.getRequestSpecification()
                .contentType("application/json")
                .header("Accept", "application/json")
                .body(json)

I got:

com.jayway.restassured.path.json.exception.JsonPathException: Failed to parse 
the JSON document
    at com.jayway.restassured.path.json.JsonPath$ExceptionCatcher.invoke(JsonPath.java:830)
    at com.jayway.restassured.path.json.JsonPath$1.doParseWith(JsonPath.java:752)
    at com.jayway.restassured.path.json.JsonPath$JsonParser.parseWith(JsonPath.java:871)
    at com.jayway.restassured.path.json.JsonPath.get(JsonPath.java:182)
    at org.aerogear.pushee.tests.RegisterMobileVariantsSpecification.Registering a push application(RegisterMobileVariantsSpecification.groovy:72)
Caused by: groovy.json.JsonException: Lexing failed on line: 1, column: 1, 
while reading '<', no possible valid JSON value or punctuation could be 
recognized.
    at groovy.json.JsonLexer.nextToken(JsonLexer.java:82)
    at com.jayway.restassured.internal.path.json.ConfigurableJsonSlurper.parse(ConfigurableJsonSlurper.groovy:92)
    at com.jayway.restassured.path.json.JsonPath$1$1.method(JsonPath.java:754)
    at com.jayway.restassured.path.json.JsonPath$ExceptionCatcher.invoke(JsonPath.java:828)
    ... 4 more

Original comment by corinnek...@gmail.com on 7 Jun 2013 at 6:55

GoogleCodeExporter commented 8 years ago
If I understand that correctly, calling json { foo "bar"} directly in build() 
method API will return a Map, that RestAssured understands.

However, calling it first, then passing json object to build() will return 
JsonBuilder, which will call body(Object) and this is something RESTAssured has 
no bindings for.

Original comment by karel.pi...@gmail.com on 7 Jun 2013 at 7:00

GoogleCodeExporter commented 8 years ago
Very well found Karel, indeed something like
.body(json.toString())
will also work

It's a confusing one, would be nice to have a JsonBuilder support... 

Original comment by corinnek...@gmail.com on 7 Jun 2013 at 7:07