use of com.axway.ats.action.rest.RestResponse in project ats-framework by Axway.
the class RestOperationTests 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";
RestClient client = new RestClient(BASE_URI + "simplepage/query/");
// add request parameters by specifying their name and value
client.addRequestParameter(requestParam1Name, requestParam1Value);
client.addRequestParameter(requestParam2Name, requestParam2Value);
RestResponse response = client.get();
assertEquals(response.getBodyAsString(), "You passed " + requestParam1Name + " " + requestParam1Value + " " + requestParam2Name + " " + requestParam2Value + " request params");
}
use of com.axway.ats.action.rest.RestResponse 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"));
}
use of com.axway.ats.action.rest.RestResponse in project ats-framework by Axway.
the class RestOperationTests method delete_usingRequestParam.
/**
* A DELETE using request parameters.
*
* Our server expects the the last name of the person to be deleted.
*/
@Test
public void delete_usingRequestParam() {
createPersonToManipulateInTheTest();
final String lastNameParam = "lastName";
final String lastNameParamValue = "Norris";
RestClient client = new RestClient(BASE_URI + "peoplepage/delete");
// add the request parameter
client.addRequestParameter(lastNameParam, lastNameParamValue);
RestResponse response = client.delete();
assertEquals(response.getStatusCode(), 200);
assertTrue(response.getBodyAsString().startsWith("Deleted person with last name " + lastNameParamValue));
}
use of com.axway.ats.action.rest.RestResponse in project ats-framework by Axway.
the class RestOperationTests 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
RestClient client = new RestClient(BASE_URI + "peoplepage/put");
RestResponse response = client.putForm(new RestForm().addParameter("firstName", "Chuck").addParameter("lastName", "Norris").addParameter("age", "71"));
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"));
}
use of com.axway.ats.action.rest.RestResponse in project ats-framework by Axway.
the class RestOperationTests method get_jsonToJavaObject.
/**
* A GET which returns some JSON body.
*
* We serialize the response body to a java instance and use its particular class methods to retrieve the needed data
*/
@Test
public void get_jsonToJavaObject() {
createPersonToManipulateInTheTest();
RestClient client = new RestClient(BASE_URI + "peoplepage/get_jsonPojo");
client.addResourcePath("Chuck");
client.setResponseMediaType(RestMediaType.APPLICATION_JSON);
RestResponse response = client.get();
// get the body as a java object
PersonPojo person = response.getBodyAsObject(PersonPojo.class);
// now call the different java methods to retrieve the needed data
assertEquals(person.getFirstName(), "Chuck");
assertEquals(person.getLastName(), "Norris");
assertEquals(person.getAge(), 70);
log.info("Got back a java object which is: " + person.toString());
}
Aggregations