1 package com.atlassian.sal.testresources.net;
2
3 import com.atlassian.sal.api.net.Request;
4 import com.atlassian.sal.api.net.RequestFilePart;
5 import com.atlassian.sal.api.net.Response;
6 import com.atlassian.sal.api.net.ResponseException;
7 import com.atlassian.sal.api.net.ResponseHandler;
8 import com.atlassian.sal.api.net.ReturningResponseHandler;
9
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17
18
19
20
21
22 public class MockRequest implements Request<MockRequest, MockResponse> {
23 private final Request.MethodType methodType;
24 private String url;
25 private int connectionTimeout;
26 private int soTimeout;
27 private String requestBody;
28 private String requestContentType;
29 private final Map<String, List<String>> headers = new HashMap<>();
30 private final List<String> requestParameters = new ArrayList<>();
31 private String basicHost;
32 private String basicUser;
33 private String basicPassword;
34 private MockResponse response;
35 private String responseBody;
36 private boolean followRedirects = true;
37
38 public MockRequest(final MethodType methodType, final String url) {
39 this.methodType = methodType;
40 this.url = url;
41 }
42
43 public MockRequest setConnectionTimeout(final int connectionTimeout) {
44 this.connectionTimeout = connectionTimeout;
45 return this;
46 }
47
48 public MockRequest setSoTimeout(final int soTimeout) {
49 this.soTimeout = soTimeout;
50 return this;
51 }
52
53 public MockRequest setUrl(final String url) {
54 this.url = url;
55 return this;
56 }
57
58 public MockRequest setRequestBody(final String requestBody) {
59 this.requestBody = requestBody;
60 return this;
61 }
62
63 @Override
64 public MockRequest setRequestBody(final String requestBody, final String contentType) {
65 this.requestBody = requestBody;
66 this.requestContentType = contentType;
67 return this;
68 }
69
70 public MockRequest setFiles(final List<RequestFilePart> files) {
71 return null;
72 }
73
74 public MockRequest setEntity(Object entity) {
75 throw new UnsupportedOperationException();
76 }
77
78 public MockRequest setRequestContentType(final String contentType) {
79 this.requestContentType = contentType;
80 return this;
81 }
82
83 public MockRequest addRequestParameters(final String... params) {
84 requestParameters.addAll(Arrays.asList(params));
85 return this;
86 }
87
88 public MockRequest addHeader(final String headerName, final String headerValue) {
89 List<String> list = headers.get(headerName);
90 if (list == null) {
91 list = new ArrayList<>();
92 headers.put(headerName, list);
93 }
94 list.add(headerValue);
95 return this;
96 }
97
98 public MockRequest setHeader(final String headerName, final String headerValue) {
99 headers.put(headerName, new ArrayList<>(Collections.singletonList(headerValue)));
100 return this;
101 }
102
103 public MockRequest setFollowRedirects(boolean follow) {
104 this.followRedirects = follow;
105 return this;
106 }
107
108 public MockRequest addBasicAuthentication(final String host, final String username, final String password) {
109 basicHost = host;
110 basicUser = username;
111 basicPassword = password;
112 return this;
113 }
114
115 public <RET> RET executeAndReturn(ReturningResponseHandler<? super MockResponse, RET> responseHandler) throws ResponseException {
116 if (response == null) {
117 response = new MockResponse();
118 }
119 return responseHandler.handle(response);
120 }
121
122 public void execute(final ResponseHandler<? super MockResponse> responseHandler) throws ResponseException {
123 if (response == null) {
124 response = new MockResponse();
125 }
126 responseHandler.handle(response);
127 }
128
129 public String execute() throws ResponseException {
130 return responseBody;
131 }
132
133 public MethodType getMethodType() {
134 return methodType;
135 }
136
137 public String getUrl() {
138 return url;
139 }
140
141 public int getConnectionTimeout() {
142 return connectionTimeout;
143 }
144
145 public int getSoTimeout() {
146 return soTimeout;
147 }
148
149 public String getRequestBody() {
150 return requestBody;
151 }
152
153 public String getRequestContentType() {
154 return requestContentType;
155 }
156
157 public List<String> getRequestParameters() {
158 return requestParameters;
159 }
160
161 public Map<String, List<String>> getHeaders() {
162 return headers;
163 }
164
165 public List<String> getHeader(final String headerName) {
166 return headers.get(headerName);
167 }
168
169 public String getBasicHost() {
170 return basicHost;
171 }
172
173 public String getBasicUser() {
174 return basicUser;
175 }
176
177 public String getBasicPassword() {
178 return basicPassword;
179 }
180
181 public Response getResponse() {
182 return response;
183 }
184
185 public void setResponse(final MockResponse response) {
186 this.response = response;
187 }
188
189 public void setResponseBody(final String responseBody) {
190 this.responseBody = responseBody;
191 }
192 }