CodeSVby CA Technologies

URL Parameters

< Back

Prerequisites

This service is configured to respond to GET requests. These are defined using the forGet(String url) method with a URL that contains a parameter {year}, that is matched against the real request URL. You can reference this parameter in the response body using the annotation ${attribute.year}, which is replaced with a real parameter from the request.

URL Parameter Matching

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

private static final String JSON_EXAMPLES_PORTFOLIO = "{"
+ "\"portfolio\": {\n"
+ " \"id\": \"1\",\n"
+ " \"year\": \"${attribute.year}\",\n"
+ " \"productNamesList\": [\n"
+ " \"CA Server Automation\",\n"
+ " \"CA Service Catalog\",\n"
+ " \"CA Service Desk Manager\",\n"
+ " \"CA Service Management\",\n"
+ " \"CA Service Operations Insight\",\n"
+ " \"CA Service Virtualization\"\n"
+ " ]\n"
+ "}}";

@Rule
public VirtualServerRule vs = new VirtualServerRule();


@Test
public void testAdvancedHttpUsage() throws Exception {
forGet(URL)
.doReturn(
okMessage()
.withJsonBody(JSON_EXAMPLES_PORTFOLIO)
);

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("http://www.ca.com/portfolio/2017");
HttpResponse response;

response = client.execute(request);

BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}

String body = result.toString();

assertEquals(200, response.getStatusLine().getStatusCode());
assertNotNull(body);
assertTrue(body.contains("2017"));
assertFalse(body.contains("${attribute.year}"));
}

For a complete example see: UrlParameterExample