Search in sources :

Example 1 with RestHeader

use of com.axway.ats.action.rest.RestHeader in project ats-framework by Axway.

the class RestOperationTests method get.

/**
 * A simple GET which returns some textual body
 */
@Test
public void get() {
    // create an instance of the RestClient by providing the URI to work with
    RestClient client = new RestClient(BASE_URI + "simplepage/");
    // Optionally set the expected media type of the response.
    // If receive response with different media type, the returned status code will be "406 NOT ACCEPTABLE".
    // You can try this by specifying a different type
    client.setResponseMediaType(RestMediaType.TEXT_HTML);
    // execute the HTTP method - in this case a GET
    // each HTTP method returns a response object
    RestResponse response = client.get();
    // verify the response status in any of the following ways
    assertEquals(response.getStatusCode(), 200);
    response.verifyStatusCode(200);
    assertEquals(response.getStatusMessage(), "OK");
    response.verifyStatusMessage("OK");
    // this is how you can read the response headers
    log.info("We received the following response headers:");
    for (RestHeader header : response.getHeaders()) {
        log.info(header.getKey() + ":" + header.getValue());
    }
    // there are different ways to get the body, here we get it as a simple text
    String responseBody = response.getBodyAsString();
    assertEquals(responseBody, "Some simple page response");
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) RestClient(com.axway.ats.action.rest.RestClient) RestHeader(com.axway.ats.action.rest.RestHeader) Test(org.testng.annotations.Test)

Example 2 with RestHeader

use of com.axway.ats.action.rest.RestHeader in project ats-framework by Axway.

the class RestOperationTests method get_downloadFileAsStream.

/**
 * A GET which returns a stream with data.
 *
 * This particular body is small, so we can save the content into a String,
 * but usual case would be to stream it on the file system
 */
@Test
public void get_downloadFileAsStream() throws IOException {
    RestClient client = new RestClient(BASE_URI + "simplepage/download");
    client.setResponseMediaType(RestMediaType.MULTIPART_FORM_DATA);
    RestResponse response = client.get();
    response.verifyStatusCode(200);
    // check the value of the content disposition header
    RestHeader contentDispositionHeader = response.getHeader("Content-Disposition");
    assertNotNull(contentDispositionHeader);
    assertTrue(contentDispositionHeader.getValue().startsWith("attachment"));
    // Read the returned body as an stream
    // In the usual case this could be a significant amount of data
    InputStream responseStream = response.getBodyAsInputStream();
    // The following method reads into a String and closes the stream
    // Note: this method is not a public one. It can be change at any moment without notice
    String fileContent = IoUtils.streamToString(responseStream);
    assertTrue(fileContent.contains("file line 1"));
}
Also used : RestResponse(com.axway.ats.action.rest.RestResponse) InputStream(java.io.InputStream) RestClient(com.axway.ats.action.rest.RestClient) RestHeader(com.axway.ats.action.rest.RestHeader) Test(org.testng.annotations.Test)

Aggregations

RestClient (com.axway.ats.action.rest.RestClient)2 RestHeader (com.axway.ats.action.rest.RestHeader)2 RestResponse (com.axway.ats.action.rest.RestResponse)2 Test (org.testng.annotations.Test)2 InputStream (java.io.InputStream)1