| 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 | |
package com.jcabi.http.request; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.aspects.Loggable; |
| 34 | |
import com.jcabi.http.ImmutableHeader; |
| 35 | |
import com.jcabi.http.Request; |
| 36 | |
import com.jcabi.http.RequestBody; |
| 37 | |
import com.jcabi.http.RequestURI; |
| 38 | |
import com.jcabi.http.Response; |
| 39 | |
import com.jcabi.http.Wire; |
| 40 | |
import com.jcabi.immutable.Array; |
| 41 | |
import java.io.IOException; |
| 42 | |
import java.io.InputStream; |
| 43 | |
import java.net.HttpURLConnection; |
| 44 | |
import java.nio.charset.Charset; |
| 45 | |
import java.util.Collection; |
| 46 | |
import java.util.Collections; |
| 47 | |
import java.util.Map; |
| 48 | |
import lombok.EqualsAndHashCode; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
@Immutable |
| 62 | 1 | @EqualsAndHashCode(of = { "base", "code", "phrase", "hdrs", "content" }) |
| 63 | |
@Loggable(Loggable.DEBUG) |
| 64 | |
@SuppressWarnings("PMD.TooManyMethods") |
| 65 | 124 | public final class FakeRequest implements Request { |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 1 | private static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 1 | private static final Charset CHARSET = Charset.forName("UTF-8"); |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
private final transient Request base; |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
private final transient int code; |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
private final transient String phrase; |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
private final transient Array<Map.Entry<String, String>> hdrs; |
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
@Immutable.Array |
| 102 | |
private final transient byte[] content; |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
public FakeRequest() { |
| 108 | 37 | this( |
| 109 | |
HttpURLConnection.HTTP_OK, |
| 110 | |
"OK", |
| 111 | |
Collections.<Map.Entry<String, String>>emptyList(), |
| 112 | |
FakeRequest.EMPTY_BYTE_ARRAY |
| 113 | |
); |
| 114 | 37 | } |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public FakeRequest(final int status, final String reason, |
| 125 | |
final Collection<Map.Entry<String, String>> headers, |
| 126 | 75 | final byte[] body) { |
| 127 | 75 | this.code = status; |
| 128 | 75 | this.phrase = reason; |
| 129 | 75 | this.hdrs = new Array<>(headers); |
| 130 | 75 | this.content = body.clone(); |
| 131 | 75 | this.base = new BaseRequest( |
| 132 | 75 | new Wire() { |
| 133 | |
@Override |
| 134 | |
|
| 135 | |
public Response send(final Request req, final String home, |
| 136 | |
final String method, |
| 137 | |
final Collection<Map.Entry<String, String>> headers, |
| 138 | |
final InputStream text, |
| 139 | |
final int connect, |
| 140 | |
final int read) { |
| 141 | 31 | return new DefaultResponse( |
| 142 | |
req, |
| 143 | |
FakeRequest.this.code, |
| 144 | |
FakeRequest.this.phrase, |
| 145 | |
FakeRequest.this.hdrs, |
| 146 | |
FakeRequest.this.content |
| 147 | |
); |
| 148 | |
} |
| 149 | |
}, |
| 150 | |
"http://localhost:12345/see-FakeRequest-class" |
| 151 | |
); |
| 152 | 75 | } |
| 153 | |
|
| 154 | |
@Override |
| 155 | |
public String toString() { |
| 156 | 0 | return this.base.toString(); |
| 157 | |
} |
| 158 | |
|
| 159 | |
@Override |
| 160 | |
public RequestURI uri() { |
| 161 | 6 | return this.base.uri(); |
| 162 | |
} |
| 163 | |
|
| 164 | |
@Override |
| 165 | |
public Request header(final String name, final Object value) { |
| 166 | 2 | return this.base.header(name, value); |
| 167 | |
} |
| 168 | |
|
| 169 | |
@Override |
| 170 | |
public Request reset(final String name) { |
| 171 | 0 | return this.base.reset(name); |
| 172 | |
} |
| 173 | |
|
| 174 | |
@Override |
| 175 | |
public RequestBody body() { |
| 176 | 1 | return this.base.body(); |
| 177 | |
} |
| 178 | |
|
| 179 | |
@Override |
| 180 | |
public RequestBody multipartBody() { |
| 181 | 0 | return this.base.multipartBody(); |
| 182 | |
} |
| 183 | |
|
| 184 | |
@Override |
| 185 | |
public Request method(final String method) { |
| 186 | 0 | return this.base.method(method); |
| 187 | |
} |
| 188 | |
|
| 189 | |
@Override |
| 190 | |
public Request timeout(final int connect, final int read) { |
| 191 | 0 | return this.base.timeout(connect, read); |
| 192 | |
} |
| 193 | |
|
| 194 | |
@Override |
| 195 | |
public Response fetch() throws IOException { |
| 196 | 25 | return this.base.fetch(); |
| 197 | |
} |
| 198 | |
|
| 199 | |
@Override |
| 200 | |
public Response fetch(final InputStream stream) throws IOException { |
| 201 | 1 | if (this.content.length > 0) { |
| 202 | 1 | throw new IllegalStateException( |
| 203 | |
"Request Body is not empty, use fetch() instead" |
| 204 | |
); |
| 205 | |
} |
| 206 | 0 | return this.base.fetch(stream); |
| 207 | |
} |
| 208 | |
|
| 209 | |
@Override |
| 210 | |
public <T extends Wire> Request through(final Class<T> type, |
| 211 | |
final Object... args) { |
| 212 | 0 | return this.base.through(type, args); |
| 213 | |
} |
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
public FakeRequest withStatus(final int status) { |
| 221 | 4 | return new FakeRequest( |
| 222 | |
status, |
| 223 | |
this.phrase, |
| 224 | |
this.hdrs, |
| 225 | |
this.content |
| 226 | |
); |
| 227 | |
} |
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
public FakeRequest withReason(final String reason) { |
| 235 | 2 | return new FakeRequest( |
| 236 | |
this.code, |
| 237 | |
reason, |
| 238 | |
this.hdrs, |
| 239 | |
this.content |
| 240 | |
); |
| 241 | |
} |
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
public FakeRequest withHeader(final String name, final String value) { |
| 250 | 7 | return new FakeRequest( |
| 251 | |
this.code, |
| 252 | |
this.phrase, |
| 253 | |
this.hdrs.with(new ImmutableHeader(name, value)), |
| 254 | |
this.content |
| 255 | |
); |
| 256 | |
} |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
public FakeRequest withBody(final String text) { |
| 264 | 24 | return this.withBody(text.getBytes(FakeRequest.CHARSET)); |
| 265 | |
} |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
public FakeRequest withBody(final byte[] body) { |
| 273 | 25 | return new FakeRequest( |
| 274 | |
this.code, |
| 275 | |
this.phrase, |
| 276 | |
this.hdrs, |
| 277 | |
body |
| 278 | |
); |
| 279 | |
} |
| 280 | |
|
| 281 | |
} |