/
ResponseDefinition.java
333 lines (284 loc) · 8.9 KB
/
ResponseDefinition.java
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright (C) 2011 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.tomakehurst.wiremock.http;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
import com.github.tomakehurst.wiremock.common.Json;
import java.nio.charset.Charset;
import static com.google.common.base.Charsets.UTF_8;
import static java.net.HttpURLConnection.*;
import static javax.xml.bind.DatatypeConverter.parseBase64Binary;
import static javax.xml.bind.DatatypeConverter.printBase64Binary;
@JsonSerialize(include=Inclusion.NON_NULL)
public class ResponseDefinition {
private int status;
private byte[] body;
private boolean isBinaryBody = false;
private String bodyFileName;
private HttpHeaders headers;
private Integer fixedDelayMilliseconds;
private String proxyBaseUrl;
private String browserProxyUrl;
private Fault fault;
private boolean wasConfigured = true;
private Request originalRequest;
public static ResponseDefinition copyOf(ResponseDefinition original) {
ResponseDefinition newResponseDef = new ResponseDefinition();
newResponseDef.status = original.status;
newResponseDef.body = original.body;
newResponseDef.isBinaryBody = original.isBinaryBody;
newResponseDef.bodyFileName = original.bodyFileName;
newResponseDef.headers = original.headers;
newResponseDef.fixedDelayMilliseconds = original.fixedDelayMilliseconds;
newResponseDef.proxyBaseUrl = original.proxyBaseUrl;
newResponseDef.fault = original.fault;
newResponseDef.wasConfigured = original.wasConfigured;
return newResponseDef;
}
public HttpHeaders getHeaders() {
return headers;
}
public void setHeaders(final HttpHeaders headers) {
this.headers = headers;
}
public ResponseDefinition(final int statusCode, final String bodyContent) {
this.status = statusCode;
this.body = (bodyContent==null) ? null : bodyContent.getBytes(Charset.forName(UTF_8.name()));
}
public ResponseDefinition(final int statusCode, final byte[] bodyContent) {
this.status = statusCode;
this.body = bodyContent;
isBinaryBody = true;
}
public ResponseDefinition() {
this.status = HTTP_OK;
}
public static ResponseDefinition notFound() {
return new ResponseDefinition(HTTP_NOT_FOUND, (byte[])null);
}
public static ResponseDefinition ok() {
return new ResponseDefinition(HTTP_OK, (byte[])null);
}
public static ResponseDefinition created() {
return new ResponseDefinition(HTTP_CREATED, (byte[])null);
}
public static ResponseDefinition notConfigured() {
final ResponseDefinition response = new ResponseDefinition(HTTP_NOT_FOUND, (byte[])null);
response.wasConfigured = false;
return response;
}
public static ResponseDefinition browserProxy(Request originalRequest) {
final ResponseDefinition response = new ResponseDefinition();
response.browserProxyUrl = originalRequest.getAbsoluteUrl();
return response;
}
public int getStatus() {
return status;
}
public String getBody() {
return (!isBinaryBody && body!=null) ? new String(body,Charset.forName(UTF_8.name())) : null;
}
@JsonIgnore
public byte[] getByteBody() {
return body;
}
public String getBase64Body() {
if (isBinaryBody && body != null) {
return printBase64Binary(body);
}
return null;
}
public void setBase64Body(String base64Body) {
isBinaryBody = true;
body = parseBase64Binary(base64Body);
}
// Needs to be explicitly marked as a property, since an overloaded setter with the same
// name is marked as ignored (see currently open JACKSON-783 bug)
@JsonProperty
public void setBody(final String body) {
this.body = (body!=null) ? body.getBytes(Charset.forName(UTF_8.name())) : null;
isBinaryBody = false;
}
@JsonIgnore
public void setBody(final byte[] body) {
this.body = body;
isBinaryBody = true;
}
public void setStatus(final int status) {
if (status == 0) {
this.status = HTTP_OK;
} else {
this.status = status;
}
}
public void setFixedDelayMilliseconds(final Integer fixedDelayMilliseconds) {
this.fixedDelayMilliseconds = fixedDelayMilliseconds;
}
public String getBodyFileName() {
return bodyFileName;
}
public void setBodyFileName(final String bodyFileName) {
this.bodyFileName = bodyFileName;
}
public boolean wasConfigured() {
return wasConfigured;
}
public Integer getFixedDelayMilliseconds() {
return fixedDelayMilliseconds;
}
@JsonIgnore
public String getProxyUrl() {
if (browserProxyUrl != null) {
return browserProxyUrl;
}
return proxyBaseUrl + originalRequest.getUrl();
}
public String getProxyBaseUrl() {
return proxyBaseUrl;
}
public void setProxyBaseUrl(final String proxyBaseUrl) {
this.proxyBaseUrl = proxyBaseUrl;
}
@JsonIgnore
public boolean specifiesBodyFile() {
return bodyFileName != null;
}
@JsonIgnore
public boolean specifiesBodyContent() {
return body != null;
}
@JsonIgnore
public boolean specifiesBinaryBodyContent() {
return (body!=null && isBinaryBody);
}
@JsonIgnore
public boolean isProxyResponse() {
return browserProxyUrl != null || proxyBaseUrl != null;
}
public Request getOriginalRequest() {
return originalRequest;
}
public void setOriginalRequest(final Request originalRequest) {
this.originalRequest = originalRequest;
}
public Fault getFault() {
return fault;
}
public void setFault(final Fault fault) {
this.fault = fault;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((body == null) ? 0 : body.hashCode());
result = prime * result
+ ((bodyFileName == null) ? 0 : bodyFileName.hashCode());
result = prime * result + ((fault == null) ? 0 : fault.hashCode());
result = prime
* result
+ ((fixedDelayMilliseconds == null) ? 0
: fixedDelayMilliseconds.hashCode());
result = prime * result + ((headers == null) ? 0 : headers.hashCode());
result = prime * result
+ ((originalRequest == null) ? 0 : originalRequest.hashCode());
result = prime * result
+ ((proxyBaseUrl == null) ? 0 : proxyBaseUrl.hashCode());
result = prime * result + status;
result = prime * result + (wasConfigured ? 1231 : 1237);
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ResponseDefinition other = (ResponseDefinition) obj;
if (body == null) {
if (other.body != null) {
return false;
}
} else if (!byteBodyEquals(body, other.body)) {
return false;
}
if (bodyFileName == null) {
if (other.bodyFileName != null) {
return false;
}
} else if (!bodyFileName.equals(other.bodyFileName)) {
return false;
}
if (fault != other.fault) {
return false;
}
if (fixedDelayMilliseconds == null) {
if (other.fixedDelayMilliseconds != null) {
return false;
}
} else if (!fixedDelayMilliseconds.equals(other.fixedDelayMilliseconds)) {
return false;
}
if (headers == null) {
if (other.headers != null) {
return false;
}
} else if (!headers.equals(other.headers)) {
return false;
}
if (proxyBaseUrl == null) {
if (other.proxyBaseUrl != null) {
return false;
}
} else if (!proxyBaseUrl.equals(other.proxyBaseUrl)) {
return false;
}
if (status != other.status) {
return false;
}
if (wasConfigured != other.wasConfigured) {
return false;
}
return true;
}
private static boolean byteBodyEquals(byte[] expecteds, byte[] actuals)
{
if (expecteds == actuals) return true;
if (expecteds == null) return false;
if (actuals == null) return false;
int actualsLength= actuals.length;
int expectedsLength= expecteds.length;
if (actualsLength != expectedsLength)
return false;
for (int i= 0; i < expectedsLength; i++) {
byte expected= expecteds[i];
byte actual= actuals[i];
if(expected!=actual) return false;
}
return true;
}
@Override
public String toString() {
return Json.write(this);
}
}