sahaya / rest-assured

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

New ContentType.EXCLUDE_HEADER functionality #226

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This is an enhancement request that can be treated as a low priority since 
there is a workaround by using HttpClient. 

The request is for a functional expansion of the ContentType enumeration where 
a new value ContentType.EXCLUDE_HEADER causes the API to omit the transmission 
of the content-type header.

Below is a summary of the issue as originally posted to the Google Groups list.

I am using REST assured to test against a vendor's REST interface.  There is a 
particular operation that requires submitting some custom headers via a PUT 
operation with a zero content length body.  When I attempt to do this, no 
message is transmitted and I encounter an 
org.apache.http.client.ClientProtocolException,  I believe that I narrowed it 
down to the presence of the Content-Type=*/* header which I think is being 
injected automatically by REST assured.  I came to this conclusion by coding 
the REST call directly using Apache HTTP client and the call acted as expected. 
Additionally, I used WireShark to verify that the HttpClient code was not 
transmitting Content-Type, and that it was transmitting Content-Length: 0.  Is 
there a way to prohibit the Content-Type header from being submitted?  Below I 
included the REST assured code that threw the exception, the HttpClient code 
that was successful, and lastly the stack trace.

REST assured code
        response = RestAssured.
                with().
                    log().all().
                    header("customHeader1", "SomeValue1").
                    header("customHeader2", "SomeValue2").
                    header("Content-Length", "0").
                expect().
                    log().all().
                    statusCode(201).
                when().
                    put(url);
Apache Http Client code
        AbstractHttpClient httpClient = new DefaultHttpClient();
        HttpPut httpPut = new HttpPut(url);
        httpPut.addHeader("customHeader1", "SomeValue1");
        httpPut.addHeader("customHeader2", "SomeValue2");

        HttpResponse httpResponse = null;
        httpResponse = httpClient.execute(httpPut);

        int statusCode = httpResponse.getStatusLine().getStatusCode();
        assertThat(statusCode, equalTo(HttpStatus.SC_CREATED));

Stack Trace
org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:909)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.http.client.HttpClient$execute.call(Unknown Source)
    at com.jayway.restassured.internal.RequestSpecificationImpl$RestAssuredHttpBuilder.doRequest(RequestSpecificationImpl.groovy:1350)
    at com.jayway.restassured.internal.http.HTTPBuilder.doRequest(HTTPBuilder.java:489)
    at com.jayway.restassured.internal.http.HTTPBuilder.request(HTTPBuilder.java:438)
    at com.jayway.restassured.internal.http.HTTPBuilder$request.call(Unknown Source)
    at com.jayway.restassured.internal.RequestSpecificationImpl.sendHttpRequest(RequestSpecificationImpl.groovy:926)
    at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$sendHttpRequest(RequestSpecificationImpl.groovy)
    at com.jayway.restassured.internal.RequestSpecificationImpl$this$2$sendHttpRequest.callCurrent(Unknown Source)
    at com.jayway.restassured.internal.RequestSpecificationImpl.sendRequest(RequestSpecificationImpl.groovy:793)
    at com.jayway.restassured.internal.RequestSpecificationImpl.this$2$sendRequest(RequestSpecificationImpl.groovy)
    at com.jayway.restassured.internal.RequestSpecificationImpl$this$2$sendRequest.call(Unknown Source)
    at com.jayway.restassured.internal.filter.RootFilter.filter(RootFilter.groovy:28)
    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:47)
    at com.jayway.restassured.filter.log.StatusCodeBasedLoggingFilter.filter(StatusCodeBasedLoggingFilter.java:95)
    at com.jayway.restassured.filter.log.ResponseLoggingFilter.filter(ResponseLoggingFilter.java:31)
    at com.jayway.restassured.filter.Filter$filter$0.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:47)
    at com.jayway.restassured.filter.log.RequestLoggingFilter.filter(RequestLoggingFilter.java:134)
    at com.jayway.restassured.filter.Filter$filter$0.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at com.jayway.restassured.filter.Filter$filter.call(Unknown Source)
    at com.jayway.restassured.internal.filter.FilterContextImpl.next(FilterContextImpl.groovy:47)
    at com.jayway.restassured.filter.FilterContext$next.call(Unknown Source)
    at com.jayway.restassured.internal.RequestSpecificationImpl.invokeFilterChain(RequestSpecificationImpl.groovy:731)
    at com.jayway.restassured.internal.RequestSpecificationImpl$invokeFilterChain.callCurrent(Unknown Source)
    at com.jayway.restassured.internal.RequestSpecificationImpl.applyPathParamsAndSendRequest(RequestSpecificationImpl.groovy:1115)
    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.put(RequestSpecificationImpl.groovy:137)
    at com.jayway.restassured.specification.RequestSender$put.call(Unknown Source)
    at com.jayway.restassured.internal.ResponseSpecificationImpl.put(ResponseSpecificationImpl.groovy:232)

Original issue reported on code.google.com by jmeri...@innoventsolutions.com on 16 Apr 2013 at 11:06

GoogleCodeExporter commented 9 years ago

Original comment by johan.ha...@gmail.com on 14 May 2013 at 7:04

GoogleCodeExporter commented 9 years ago
Issue 341 has been merged into this issue.

Original comment by johan.ha...@gmail.com on 5 Jul 2014 at 5:08

GoogleCodeExporter commented 9 years ago
This feature is much needed. We need to support requests without content type 
header in our product and we need to be able to test it. In addition, being 
Rest Assured a test library, it should have the ability to send requests 
without certain headers just to check the behavior of your poduct in such 
scenario.

Original comment by agusti.s...@gmail.com on 25 Jan 2015 at 12:22

GoogleCodeExporter commented 9 years ago
Finally someone agree with me :)

Original comment by Janiszew...@gmail.com on 25 Jan 2015 at 12:32

GoogleCodeExporter commented 9 years ago
I definitely agree that it should be possible. It just haven't been a priority 
to implement since it's a pretty small corner case. Don't know how much effort 
that may go in to fix it. I'm on parental leave right now so pull requests are 
very welcome.

Original comment by johan.ha...@gmail.com on 25 Jan 2015 at 5:46

GoogleCodeExporter commented 9 years ago
What I noticed in my case (I'm using version 1.7.1) is that, if no content-type 
is specified, Rest Assured tries to guess it and adds a content-type header.

But the guessing is wrong as it guessed "text/plain"  when it actually was a 
JSON payload.

I would add an option to disable content-type guessing altogether. If the user 
hasn't specified any content type header, no content-type header should be sent 
at all.

I'll work on that in a fork and let you know what I get.

Original comment by agusti.s...@gmail.com on 26 Jan 2015 at 10:15