This repository was archived by the owner on Oct 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAdvancedMatchingExample.java
More file actions
106 lines (92 loc) · 3.93 KB
/
AdvancedMatchingExample.java
File metadata and controls
106 lines (92 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/******************************************************************************
*
* Copyright (c) 2017 CA. All rights reserved.
*
* This software and all information contained therein is confidential and
* proprietary and shall not be duplicated, used, disclosed or disseminated
* in any way except as authorized by the applicable license agreement,
* without the express written permission of CA. All authorized reproductions
* must be marked with this language.
*
* EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE EXTENT
* PERMITTED BY APPLICABLE LAW, CA PROVIDES THIS SOFTWARE WITHOUT
* WARRANTY OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN
* NO EVENT WILL CA BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY
* LOSS OR DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS SOFTWARE,
* INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS INTERRUPTION,
* GOODWILL, OR LOST DATA, EVEN IF CA IS EXPRESSLY ADVISED OF SUCH LOSS OR
* DAMAGE.
*
******************************************************************************/
package com.ca.codesv.example;
import static com.ca.codesv.protocols.http.fluent.HttpFluentInterface.contains;
import static com.ca.codesv.protocols.http.fluent.HttpFluentInterface.forGet;
import static com.ca.codesv.protocols.http.fluent.HttpFluentInterface.isEqualIgnoringCaseTo;
import static com.ca.codesv.protocols.http.fluent.HttpFluentInterface.okMessage;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import com.ca.codesv.engine.junit4.VirtualServerRule;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Rule;
import org.junit.Test;
/**
* Example showing advanced matching.
*
* @author CA
*/
public class AdvancedMatchingExample {
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}"));
}
}