Search in sources :

Example 1 with HttpClient

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

the class HttpOperationTests method get_json.

/**
 * A GET which returns some JSON body.
 *
 * We read the response body as JsonText and use its common methods to retrieve the needed data
 */
@Test
public void get_json() {
    createPersonToManipulateInTheTest();
    HttpClient client = new HttpClient(BASE_URI + "peoplepage/get_jsonPojo");
    client.addResourcePath("Chuck");
    HttpResponse response = client.get();
    // get the body as JsonText
    JsonText jsonResponse = response.getBodyAsJsonText();
    // now call the different getter methods to retrieve the needed data
    String firstName = jsonResponse.getString("firstName");
    String lastName = jsonResponse.getString("lastName");
    int age = jsonResponse.getInt("age");
    log.info("Got back a JSON body saying that " + firstName + " " + lastName + " is " + age + " old");
    log.info("A JSON body looks like that:\n" + jsonResponse.toString());
    log.info("A well formatter JSON body looks like that:\n" + jsonResponse.toFormattedString());
}
Also used : JsonText(com.axway.ats.action.json.JsonText) HttpClient(com.axway.ats.action.http.HttpClient) HttpResponse(com.axway.ats.action.http.HttpResponse) Test(org.testng.annotations.Test)

Example 2 with HttpClient

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

the class HttpOperationTests method put_usingFormParameters.

/**
 * A PUT which sends form parameters.
 *
 * The PUT is usually used to modify an existing resource
 * Thus this test first issues a POST to create the resource which will
 * be modified(updated) by the following PUT
 */
@Test
public void put_usingFormParameters() {
    createPersonToManipulateInTheTest();
    // update the already existing person using PUT
    HttpClient client = new HttpClient(BASE_URI + "peoplepage/put");
    client.setRequestMediaType("application/x-www-form-urlencoded");
    client.addRequestParameter("firstName", "Chuck");
    client.addRequestParameter("lastName", "Norris");
    client.addRequestParameter("age", "71");
    HttpResponse response = client.put();
    assertEquals(response.getStatusCode(), 200);
    // verify Chuck Norris grows up as expected
    assertTrue(response.getBodyAsString().startsWith("Chuck Norris was 70 years old, but now is 71 years old"));
}
Also used : HttpClient(com.axway.ats.action.http.HttpClient) HttpResponse(com.axway.ats.action.http.HttpResponse) Test(org.testng.annotations.Test)

Example 3 with HttpClient

use of com.axway.ats.action.http.HttpClient 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 4 with HttpClient

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

the class HttpOperationTests method get_usingPathParams.

/**
 * A GET using some resource path parameters.
 *
 * The application we work with returns a body with the path parameters we have sent
 */
@Test
public void get_usingPathParams() {
    final String pathParam1 = "value1";
    final String pathParam2 = "value2";
    HttpClient client = new HttpClient(BASE_URI + "simplepage/");
    // add as many as needed path parameters
    client.addResourcePath(pathParam1);
    client.addResourcePath(pathParam2);
    HttpResponse response = client.get();
    assertEquals(response.getBodyAsString(), "You passed " + pathParam1 + " and " + pathParam2 + " path params");
}
Also used : HttpClient(com.axway.ats.action.http.HttpClient) HttpResponse(com.axway.ats.action.http.HttpResponse) Test(org.testng.annotations.Test)

Example 5 with HttpClient

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

the class HttpOperationTests method get_usingRequestParams.

/**
 * A GET using some resource request(query) parameters.
 *
 * The application we work with returns a body with the request parameters we have sent
 */
@Test
public void get_usingRequestParams() {
    final String requestParam1Name = "from";
    final String requestParam1Value = "10";
    final String requestParam2Name = "to";
    final String requestParam2Value = "20";
    HttpClient client = new HttpClient(BASE_URI + "simplepage/query/");
    // add request parameters by specifying their name and value
    client.addRequestParameter(requestParam1Name, requestParam1Value);
    client.addRequestParameter(requestParam2Name, requestParam2Value);
    HttpResponse response = client.get();
    assertEquals(response.getBodyAsString(), "You passed " + requestParam1Name + " " + requestParam1Value + " " + requestParam2Name + " " + requestParam2Value + " request params");
}
Also used : 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)13 HttpResponse (com.axway.ats.action.http.HttpResponse)11 Test (org.testng.annotations.Test)11 HttpHeader (com.axway.ats.action.http.HttpHeader)2 BeforeClass (org.testng.annotations.BeforeClass)2 FileSystemOperations (com.axway.ats.action.filesystem.FileSystemOperations)1 JsonText (com.axway.ats.action.json.JsonText)1 XmlText (com.axway.ats.action.xml.XmlText)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1