sahaya / rest-assured

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

Can't call response.asString() twice because the InputStream is closed #241

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
We can't use some body retrieval methods like asString() twice because the 
stream is closed.

That would be helpful in some cases.

I myself use FestAssert instead of RestAssured for assertions, with a syntax 
being more given/when/then friendly

Exemple:
    // when
    Response response = ...
    // then
    assertThat(response).has(httpStatus(Status.NOT_FOUND));
    assertThat(response).has(apiErrors(
            ApiError.having().error(ErrorCode.SOME_DOCUMENTS_NOT_FOUND).errorDescription(containsString("id3"))
    ));

I think in test env we don't really expect RestAssured to be extra performant, 
we just want to make the tests readable. So I don't think there should be any 
optimisation, lazy evaluation or anything: it would be nice to just load the 
full immutable byte[] in memory and then being able to get it as String, new 
InputStream, byte[], Object (with the mapper etc...)

thanks

Original issue reported on code.google.com by lorber.s...@gmail.com on 30 May 2013 at 4:35

GoogleCodeExporter commented 9 years ago
There are plans for this for the 2.0 release (or later, everything depends on 
how much time I have). 

Original comment by johan.ha...@gmail.com on 3 Jun 2013 at 6:49

GoogleCodeExporter commented 9 years ago
I'm not sure it's exactly the same topic. I support #44 too because I don't 
really like the form of assertions of RA but in this one I just want to be able 
to read twice the body :) 

It's not a big deal, will just remove 1 line from all my tests where I have to 
extract the body to a "readable twice" source, like String instead of 
InputStream.

You just need to keep a copy of the byte[] of the body somewhere, and 
eventually recreate the String/InputStream when someone ask for it I guess

Original comment by lorber.s...@gmail.com on 3 Jun 2013 at 8:18

GoogleCodeExporter commented 9 years ago
If you get the content as an input stream you are on your own (you need to 
close the stream manually as well because RA can't do that for you). You cannot 
read contents from an input stream more than once. If you need to do that then 
you should convert it to a String or byte[] just as you imply.  The reason why 
I don't think it's a good idea to let RA copy the content is if you want 
performance.

Original comment by johan.ha...@gmail.com on 3 Jun 2013 at 8:26

GoogleCodeExporter commented 9 years ago
I understand.
Just not sure performance is an important point for a test framework.

Original comment by lorber.s...@gmail.com on 3 Jun 2013 at 8:29

GoogleCodeExporter commented 9 years ago
True, but people are using RA for non test purposes as well. Since version 1.8 
JsonPath and XmlPath have been extract to their own projects which is the first 
step in creating RA more modular and in the future more production ready as 
well.

Original comment by johan.ha...@gmail.com on 3 Jun 2013 at 8:33