简体   繁体   中英

Not able to fetch header data in rest assured for rest web service of type GET

I have GET REST API, which sends some information in response header. I am writing test case using rest assured framework, issue I am facing is, in response of GET API, I am not getting header string set by the server in rest API response. I have checked the same API in Rest client and HTTP Resource, there I can see the header information set by server in API response.

But in rest assured Response object, header information set by server is not available.

Rest API code:

 @Path("/download")
    @GET
    @Produces("application/zip")
    public Response downloadContractZipFile(@PathParam("contractId") final String contractId) throws CMException{
        ContractActionRequest contractActionRequest = new ContractActionRequest();
        contractActionRequest.setId(contractId);
        DownloadActionResponse  downloadActionResponse  = (DownloadActionResponse) executeAction(Action.DOWNLOAD, contractActionRequest);

        Response res =  Response
                 .ok(downloadActionResponse.getFilestream(), MediaType.APPLICATION_OCTET_STREAM)
                 .header("content-disposition",downloadActionResponse.getContentDisposition())
                 .header("Expires", "0")
                 .header("Content-Length",  String.valueOf(downloadActionResponse.getContentLength()) )
                 .build();  
        return res;
    }   

Above you can see, API is returning Content-Length in header. But when I am invoking above API using rest assured framework, it does not receive "Content-Length" in header. Assert is getting failed. Rest assured Test case code:

given().get(propertyURI).then().assertThat().header("Content-Length","7562");


java.lang.AssertionError: Expected header "Content-Length" was not "7562", was "null". Headers are:
X-Powered-By=Servlet/3.0
Cache-Control=no-cache, no-store
Cache-directive=no-cache
Pragma=no-cache
Pragma-Directive=no-cache
Expires=Thu, 01 Jan 1970 00:00:00 GMT
Content-Type=text/html;charset=UTF-8
x-authenticated=false
x-location=https://reena:9453/app/login.jsp?targetApp=OM
Content-Language=en-US
Transfer-Encoding=chunked

I suggest you try Karate instead of REST-Assured as it has much better support for validating response headers.

(disclaimer: am Karate dev)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM