Search in sources :

Example 1 with HttpHeader

use of com.axway.ats.action.http.HttpHeader in project ats-framework by Axway.

the class HttpOperationTests 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 {
    HttpClient client = new HttpClient(BASE_URI + "simplepage/download");
    HttpResponse response = client.get();
    response.verifyStatusCode(200);
    // check the value of the content disposition header
    HttpHeader 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 = new ByteArrayInputStream(response.getBody());
    // 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 : HttpHeader(com.axway.ats.action.http.HttpHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpClient(com.axway.ats.action.http.HttpClient) HttpResponse(com.axway.ats.action.http.HttpResponse) Test(org.testng.annotations.Test)

Example 2 with HttpHeader

use of com.axway.ats.action.http.HttpHeader in project ats-framework by Axway.

the class HttpOperationTests method get.

/**
 * A simple GET which returns some textual body
 */
@Test
public void get() {
    // create an instance of the HttpClient by providing the URI to work with
    HttpClient client = new HttpClient(BASE_URI + "simplepage/");
    // execute the HTTP method - in this case a GET
    // each HTTP method returns a response object
    HttpResponse response = client.get();
    // verify the response status in any of the following ways
    assertEquals(response.getStatusCode(), 200);
    response.verifyStatusCode(200);
    // this is how you can read the response headers
    log.info("We received the following response headers:");
    for (HttpHeader 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 : HttpHeader(com.axway.ats.action.http.HttpHeader) HttpClient(com.axway.ats.action.http.HttpClient) HttpResponse(com.axway.ats.action.http.HttpResponse) Test(org.testng.annotations.Test)

Aggregations

HttpClient (com.axway.ats.action.http.HttpClient)2 HttpHeader (com.axway.ats.action.http.HttpHeader)2 HttpResponse (com.axway.ats.action.http.HttpResponse)2 Test (org.testng.annotations.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1