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 package org.apache.hc.core5.testing.framework;
29
30 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.BODY;
31 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.CONTENT_TYPE;
32 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.HEADERS;
33 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.METHOD;
34 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PATH;
35 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.PROTOCOL_VERSION;
36 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.QUERY;
37 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.REQUEST;
38 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.RESPONSE;
39 import static org.apache.hc.core5.testing.framework.ClientPOJOAdapter.STATUS;
40
41 import java.net.URI;
42 import java.net.URISyntaxException;
43 import java.nio.charset.StandardCharsets;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47
48 import org.apache.hc.core5.http.NameValuePair;
49 import org.apache.hc.core5.net.URIBuilder;
50
51 public class FrameworkTest {
52
53 private Map<String, Object> request = new HashMap<>();
54 private Map<String, Object> response = new HashMap<>();
55
56
57
58
59 public FrameworkTest() {
60 this(null);
61 }
62
63
64
65
66
67
68
69
70 @SuppressWarnings("unchecked")
71 public FrameworkTest(final Map<String, Object> test) {
72 if (test != null) {
73 if (test.containsKey(REQUEST)) {
74 request = (Map<String, Object>) test.get(REQUEST);
75 }
76 if (test.containsKey(RESPONSE)) {
77 response = (Map<String, Object>) test.get(RESPONSE);
78 }
79 }
80 }
81
82
83
84
85
86
87
88 public Map<String, Object> initRequest() throws TestingFrameworkException {
89
90 final Map<String, Object> ret = new HashMap<>();
91 ret.put(PATH, TestingFramework.DEFAULT_REQUEST_PATH);
92 ret.put(BODY, TestingFramework.DEFAULT_REQUEST_BODY);
93 ret.put(CONTENT_TYPE, TestingFramework.DEFAULT_REQUEST_CONTENT_TYPE);
94 ret.put(QUERY, new HashMap<>(TestingFramework.DEFAULT_REQUEST_QUERY));
95 ret.put(HEADERS, new HashMap<>(TestingFramework.DEFAULT_REQUEST_HEADERS));
96 ret.put(PROTOCOL_VERSION, TestingFramework.DEFAULT_REQUEST_PROTOCOL_VERSION);
97
98
99 if (! request.containsKey(METHOD)) {
100 request.put(METHOD, "GET");
101 }
102 ret.putAll(request);
103
104 moveAnyParametersInPathToQuery(ret);
105
106 return ret;
107 }
108
109 private void moveAnyParametersInPathToQuery(final Map<String, Object> request) throws TestingFrameworkException {
110 try {
111 final String path = (String) request.get(PATH);
112 if (path != null) {
113 final URI uri = path.startsWith("/") ? new URI("http://localhost:8080" + path) :
114 new URI("http://localhost:8080/");
115 final URIBuilder uriBuilder = new URIBuilder(uri, StandardCharsets.UTF_8);
116 final List<NameValuePair> params = uriBuilder.getQueryParams();
117 @SuppressWarnings("unchecked")
118 final Map<String, Object> queryMap = (Map<String, Object>) request.get(QUERY);
119 for (final NameValuePair param : params) {
120 queryMap.put(param.getName(), param.getValue());
121 }
122 if (! params.isEmpty()) {
123 request.put(PATH, uri.getPath());
124 }
125 }
126 } catch (final URISyntaxException e) {
127 throw new TestingFrameworkException(e);
128 }
129 }
130
131
132
133
134
135
136 public Map<String, Object> initResponseExpectations() {
137
138 if (! response.containsKey(STATUS)) {
139 response.put(STATUS, 200);
140 }
141
142 final Map<String, Object> responseExpectations = new HashMap<>();
143
144 responseExpectations.put(BODY, TestingFramework.DEFAULT_RESPONSE_BODY);
145 responseExpectations.put(CONTENT_TYPE, TestingFramework.DEFAULT_RESPONSE_CONTENT_TYPE);
146 responseExpectations.put(HEADERS, new HashMap<>(TestingFramework.DEFAULT_RESPONSE_HEADERS));
147
148
149 responseExpectations.putAll(response);
150
151 return responseExpectations;
152 }
153
154 @Override
155 public String toString() {
156 return "request: " + request + "\nresponse: " + response;
157 }
158 }