CodeSVby CA Technologies

HTTP GET and POST

< Back

Prerequisites

GET METHOD

This service is configured to respond to GET requests that are defined using the forGet(String url) method with the following:

  • Custom status code
    • Defined by the CUSTOM_STATUS_CODE variable.
    • Value: 258 in this example
  • Predefined String response body
    • Defined by the RESPONSE_BODY_GET variable

Create GET request:

private static final String URL = "http://www.ca.com/portfolio";

private static String RESPONSE_BODY_GET = "Response body from virtualized service.";
private static int CUSTOM_STATUS_CODE = 258;

@Rule
public VirtualServerRule vs = new VirtualServerRule();

@Test
public void testSimpleHttpGetWithResponseCodeAndStringBody() throws IOException {
forGet(URL).doReturn(
aMessage(CUSTOM_STATUS_CODE)
.withStringBody(RESPONSE_BODY_GET)
);

HttpGet httpGet = new HttpGet(URL);
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse httpResponse = httpClient.execute(httpGet);

assertEquals(CUSTOM_STATUS_CODE, httpResponse.getStatusLine().getStatusCode());

BufferedReader reader = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
reader.close();

assertEquals(response.toString(), RESPONSE_BODY_GET);
}

POST METHOD

This service is configured to respond to POST requests as defined using the forPost(String url) method. This message has the following arguments:

  • Predefined String response body
    • Defined by RESPONSE_BODY_POST variable.
    • Note: This variable includes two parameters which will be replaced by the following post request arguments in the response:
      • ${argument.id}
        Value: 5
      • ${argument.filter}
        Value: ALL

Create POST request:

private static final String URL = "http://www.ca.com/portfolio";

private static String RESPONSE_BODY_POST =
"Response for id ${argument.id} with configured filter for ${argument.filter}.";
private static int CUSTOM_STATUS_CODE = 258;

@Rule
public VirtualServerRule vs = new VirtualServerRule();


@Test
public void testSimpleHttpPost() throws IOException {
forPost(URL).doReturn(
okMessage()
.withStringBody(RESPONSE_BODY_POST)
.withContentType(HttpConstants.PLAIN_TEXT)
.enableMagicStrings()
);

HttpPost httpPost = new HttpPost(URL);
List parameters = new ArrayList(2);
parameters.add(new BasicNameValuePair("id", "5"));
parameters.add(new BasicNameValuePair("filter", "ALL"));
httpPost.setEntity(new UrlEncodedFormEntity(parameters));

HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse httpResponse = httpClient.execute(httpPost);

BufferedReader reader = new BufferedReader(
new InputStreamReader(httpResponse.getEntity().getContent()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
}
reader.close();

assertTrue(response.toString().contains("Response for id 5"));
assertTrue(response.toString().contains("filter for ALL."));
}

Complete example is here.