-
Notifications
You must be signed in to change notification settings - Fork 12
Matching
Prerequisite
This service is configured to respond to GET requests that are defined using the forGet(String url)
method) with specific query parameters and headers:
Headers
-
Custom-Header
Value: CustomValue -
Accept-Language
Value: Contains the string "us"
Query Parameters
-
tokenQuery
Example: x4sphj15wqe
Note: Not case sensitive -
Year
Value: 2016
Note: You can also use references for the given query parameter values from the request in the response body.
As shown below, you can use predefined methods with only string values, or use predefined matchers for more complex matching.
Advanced Matching:
private static final String URL = "http://www.ca.com/portfolio?year=2016&tokenQuery=X4sPhj15WQE";
private static final String JSON_EXAMPLES_PORTFOLIO = "{"
+ "\"portfolio\": {\n"
+ " \"id\": \"1\",\n"
+ " \"year\": \"${argument.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)
.matchesHeader("Custom-Header", "CustomValue")
.matchesHeader("Accept-Language", contains("us"))
.matchesQuery("tokenQuery", isEqualIgnoringCaseTo("x4sphj15wqe"))
.matchesQuery("year", "2016")
.doReturn(
okMessage()
.withJsonBody(JSON_EXAMPLES_PORTFOLIO)
.enableMagicStrings()
);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(URL);
request.addHeader("Accept-Language", "en_us");
request.addHeader("Custom-Header", "CustomValue");
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
String body = result.toString();
assertEquals(200, response.getStatusLine().getStatusCode());
assertNotNull(body);
assertTrue(body.contains("2016"));
assertFalse(body.contains("${argument.year}"));
}
For a complete example see: AdvancedMatchingExample
Using special matchers matchesHeaderFromFile()
and matchesBodyFromFile
it is possible to match an incoming request against a real request stored on the file system. It is required to provide the absolute or relative path to a HTTP request text file. This file needs to be a valid HTTP request.
Example of HTTP Request Stored in a File
POST /text HTTP/1.1
Accept: text/plain
Accept-Language: zh-cn
Content-Type: text/plain
UA-CPU: x86
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET4.0C; .NET4.0E; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: localhost
Content-Length: 198
Connection: Keep-Alive
Cache-Control: no-cache
CustomHeader: Header Value
Success
Example of Matching Request Against Request File
private static final String URL = "http://www.ca.com/portfolio";
private static final String JSON_EXAMPLES_PORTFOLIO = "{"
+ "\"portfolio\": {\n"
+ " \"id\": \"1\",\n"
+ " \"year\": \"2017\",\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 testImportOfRequest() throws Exception {
forPost(URL)
.matchesBodyFromFile("requests/txt_request.txt")
.matchesHeaderFromFile("requests/txt_request.txt", "CustomHeader")
.matchesHeaderFromFile("requests/txt_request.txt", "Accept-Language")
.doReturn(
okMessage()
.withJsonBody(JSON_EXAMPLES_PORTFOLIO)
);
HttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(URL);
/*
Both header and values are the same as in referenced file
*/
request.addHeader("Accept-Language", "zh-cn");
request.addHeader("CustomHeader", "Header Value");
request.setEntity(new StringEntity("Success"));
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
String body = result.toString().replaceAll("\\s+", "");
assertEquals(200, response.getStatusLine().getStatusCode());
assertNotNull(body);
assertEquals(JSON_EXAMPLES_PORTFOLIO.replaceAll("\\s+", ""), body);
}