Regarding Using Prettifier()

125 views
Skip to first unread message

udaykanth ranganamyna

unread,
Oct 22, 2013, 1:09:56 PM10/22/13
to rest-a...@googlegroups.com
I really like rest assured for doing rest API testing.

I have recently created a custom filter for rest assured and testng.
It is working great.

I wanted to prettify my xml and json responses in the report.

I have used the below to get the prettified String responses.

new Prettifier().getPrettifiedBodyIfPossible(response);

It is working fine for xml responses.If am trying for json responses.
It is giving me the following error.I am using rest assured 1.8.1
Any of your suggestion on how to handle this would be really appreciated.

java.io.IOException: Attempted read on closed stream.

at org.apache.http.conn.EofSensorInputStream.isReadAllowed(EofSensorInputStream.java:110)

at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:136)

at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)

at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)

at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)

at java.io.InputStreamReader.read(InputStreamReader.java:167)

at java.io.Reader.read(Reader.java:123)

at java_io_Reader$read.call(Unknown Source)

at com.jayway.restassured.internal.RestAssuredResponseImpl.convertStreamToString(RestAssuredResponseImpl.groovy:376)

at com.jayway.restassured.internal.RestAssuredResponseImpl.this$2$convertStreamToString(RestAssuredResponseImpl.groovy)

at com.jayway.restassured.internal.RestAssuredResponseImpl$this$2$convertStreamToString.callCurrent(Unknown Source)

at com.jayway.restassured.internal.RestAssuredResponseImpl.charsetToString(RestAssuredResponseImpl.groovy:432)

at com.jayway.restassured.internal.RestAssuredResponseImpl.this$2$charsetToString(RestAssuredResponseImpl.groovy)

at com.jayway.restassured.internal.RestAssuredResponseImpl$this$2$charsetToString.callCurrent(Unknown Source)

at com.jayway.restassured.internal.RestAssuredResponseImpl.asString(RestAssuredResponseImpl.groovy:147)

at com.jayway.restassured.response.ResponseBodyData$asString.call(Unknown Source)

at com.jayway.restassured.internal.support.Prettifier.getPrettifiedBodyIfPossible(Prettifier.groovy:42)

at com.adobe.dps.entitlement.it.filter.RequestResponseFilter.printResponse(RequestResponseFilter.java:57)

at com.adobe.dps.entitlement.it.filter.RequestResponseFilter.filter(RequestResponseFilter.java:121)

at com.jayway.restassured.filter.Filter$filter.call(Unknown Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)

at com.jayway.restassured.filter.Filter$filter$0.call(Unknown Source)

at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:49)

at com.jayway.restassured.filter.FilterContext$next.call(Unknown Source)

at com.jayway.restassured.internal.RequestSpecificationImpl.invokeFilterChain(RequestSpecificationImpl.groovy:758)

at com.jayway.restassured.internal.RequestSpecificationImpl$invokeFilterChain.callCurrent(Unknown Source)

at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1142)

at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy)

at com.jayway.restassured.internal.RequestSpecificationImpl$this$2$applyPathParamsAndSendRequest.callCurrent(Unknown Source)

at com.jayway.restassured.internal.RequestSpecificationImpl.get(RequestSpecificationImpl.groovy:131)

at com.jayway.restassured.specification.RequestSender$get.call(Unknown Source)

at com.jayway.restassured.internal.ResponseSpecificationImpl.get(ResponseSpecificationImpl.groovy:226)


Thanks,
Uday.

Кирилл Меркушев

unread,
Oct 23, 2013, 10:48:07 AM10/23/13
to rest-a...@googlegroups.com
try to clone response before use:

    byte[] respNonPretty = response.asByteArray();

    String respPretty = new Prettifier()
                .prettify(new String(respNonPretty, Charsets.UTF_8), fromContentType(response.contentType()));

     ....

    private Response cloneResponseIfNeeded(Response response, byte[] responseBytes) {
        if (responseBytes != null && response instanceof RestAssuredResponseImpl && !((RestAssuredResponseImpl) response).getHasExpectations()) {
            final Response build = new ResponseBuilder().clone(response).setBody(responseBytes).build();
            ((RestAssuredResponseImpl) build).setHasExpectations(true);
            return build;
        }
        return response;
    }


вторник, 22 октября 2013 г., 21:09:56 UTC+4 пользователь udaykanth ranganamyna написал:

udaykanth ranganamyna

unread,
Oct 25, 2013, 12:37:14 AM10/25/13
to rest-a...@googlegroups.com


  Hi Кирилл,

  I am cloning the response and than using the response and returning the cloned response back.
  It works fine when the response is a xml.It throws exception only when it is JSON response.

Johan Haleby

unread,
Nov 24, 2013, 5:15:24 AM11/24/13
to rest-a...@googlegroups.com
What kind of exception do you get? Can you provide an example of the JSON you're receiving from the server?


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages