This repository was archived by the owner on Oct 9, 2025. It is now read-only.
-
Couldn't load subscription status.
- Fork 12
Encoding
Roman Jakubco edited this page Jan 11, 2018
·
2 revisions
Prerequisite
CodeSV supports encoded requests and can work with them effortlessly as shown in the example below. The only requirement for working with compressed payloads is to define the header Content-Enconding with an algorithm. At the moment supported algorithms are deflate and gzip. This means that even when a compressed payload is sent in the request, it is still possible to use body matchers e.g. JSONPath as shown in the example below.
The same is also applied to responses. Define the Content-Enconding header to return the response with a compressed body.
Compressed Payloads Example:
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 testEncodings() throws IOException {
HttpFluentInterface.forPost(URL)
.matchesHeader("Content-Encoding", "deflate")
.matchesBodyPayload(matchesJsonPath("$.portfolio.id"))
.doReturn(
okMessage()
.withHeader("Content-Encoding", "gzip")
.withBody(JSON_EXAMPLES_PORTFOLIO.getBytes())
);
// We need to disable automatic compression/decompression on client
HttpClient client = HttpClientBuilder.create().disableContentCompression().build();
HttpPost request = new HttpPost(URL);
request.setEntity(new ByteArrayEntity(compressPayload(JSON_EXAMPLES_PORTFOLIO)));
request.setHeader("Content-Type", "application/json");
request.setHeader("Content-Encoding", "deflate");
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);
assertNotEquals(body, JSON_EXAMPLES_PORTFOLIO);
// We will execute it one more time and now we will decompress the entity
response = client.execute(request);
body = decompressPayload(response);
assertEquals(JSON_EXAMPLES_PORTFOLIO, body);
}For a complete example see: EncodingExample