how to copy RequestSpecification & delete path parameters

998 views
Skip to first unread message

Jeffrey Ji

unread,
Jul 13, 2016, 1:24:16 PM7/13/16
to REST assured
Let's say I'm testing an API, but I want to save my port, base URI, default headers and parameters to a RequestSpecification:

RequestSpecification baseCase = given().baseUri( " http://api.twitter.com/1.1" ).port( 8080 ).param( "user_id", "12345" ).header( "Content-Type", "application/vnd+twitter.category+json" );

now I want to build a RequestSpecification built on baseCase. How would I copy baseCase over to another object? I thought I could just do:

RequestSpecification specificCase1 = baseCase.given();

b/c I assumed that .given() just returned a copy of itself, but I guess it returns a reference to itself (which means specificCase1 and baseCase both point to the same object)? This is a problem b/c when I have path parameters for the specificCase:

specificCase1.pathParam("id", "149305" ).when().post( "/statuses/retweet/{id}" );

then baseCase is mutated too. Every case built off of baseCase thereafter throws an "Invalid number of path parameters. Expected 0, was 1. Redundant path parameters" exception.

Any input would be appreciated.
-Jeffrey Ji

Jeffrey Ji

unread,
Jul 13, 2016, 1:39:45 PM7/13/16
to REST assured
I think I read the solution to my own question. I can use RequestSpecBuilder instead of a base RequestSpecifcation, that way calling pathParam() on the child case won't mutate the builder object.

Jeffrey Ji

unread,
Jul 13, 2016, 4:53:22 PM7/13/16
to REST assured
Actually I've encountered another problem. How do I save POST payloads?

RequestSpecBuilder base = new RequestSpecBuilder();
base.setBaseUri("http://api.twitter.com/1.1");
base.setPort( 8080 );
base.addHeader( "Content-Type", "application/vnd+twitter.category+json" );
RequestSpecification toSend = given().spec( base.build() ).body( "{ \"payloadkey\": \"payloadValue\" }" );

System.out.println(
    toSend.post( "/statuses/retweet/12345678.json" ).statusCode() );

The above code will print out 400. However, this works fine (returns 2xx code):

RequestSpecification toSend = given().spec( base.build() );
System.out.println(
    toSend.spec( base.build() ).body( "{ \"payloadkey\": \"payloadValue\" }" ).post( "/statuses/retweet/12345678.json" ).statusCode() );

 Do RequestSpecifications not allow saving of payloads?

Johan Haleby

unread,
Jul 15, 2016, 1:13:36 AM7/15/16
to rest-a...@googlegroups.com
What do you mean by "saving of payloads"? I think that both your examples should yield the same outcome otherwise something is fishy.

--
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/d/optout.

Jeffrey Ji

unread,
Jul 15, 2016, 1:40:35 AM7/15/16
to REST assured
Well in that case, something is fishy... I tried it on an actual API myself, and only the 2nd method worked. The 1st method gave me a 4xx code.

Johan Haleby

unread,
Jul 15, 2016, 1:49:12 AM7/15/16
to rest-a...@googlegroups.com
Would it be possible for you to reproduce it in a local test?

Jeffrey Ji

unread,
Jul 15, 2016, 12:35:04 PM7/15/16
to REST assured
As you can see, case2 has the body attached in the saved RequestSpecification, while case1 doesn't. It's not a problem of 1 post being before the other, I tried the same code with case2 posting before case1 and case1 was still 201 while case2 yielded 400. I'm not exactly sure what is going on...
problem.PNG

Johan Haleby

unread,
Jul 17, 2016, 2:11:00 AM7/17/16
to rest-a...@googlegroups.com
Could you please create an issue and attach the image at the issue tracker?
Reply all
Reply to author
Forward
0 new messages