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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http;
import com.yahoo.jdisc.Response;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
/**
* @author Simon Thoresen Hult
*/
public class HttpResponseTestCase {
@Test
void requireThatAccessorsWork() throws Exception {
final HttpResponse response = newResponse(6);
assertEquals(6, response.getStatus());
assertEquals("foo", response.getMessage());
assertNull(response.getError());
assertTrue(response.isChunkedEncodingEnabled());
response.setStatus(9);
assertEquals(9, response.getStatus());
response.setMessage("bar");
assertEquals("bar", response.getMessage());
final Throwable err = new Throwable();
response.setError(err);
assertSame(err, response.getError());
response.setChunkedEncodingEnabled(false);
assertFalse(response.isChunkedEncodingEnabled());
}
@Test
void requireThatStatusCodesDoNotChange() {
assertEquals(HttpResponse.Status.CREATED, 201);
assertEquals(HttpResponse.Status.ACCEPTED, 202);
assertEquals(HttpResponse.Status.NON_AUTHORITATIVE_INFORMATION, 203);
assertEquals(HttpResponse.Status.NO_CONTENT, 204);
assertEquals(HttpResponse.Status.RESET_CONTENT, 205);
assertEquals(HttpResponse.Status.PARTIAL_CONTENT, 206);
assertEquals(HttpResponse.Status.MULTIPLE_CHOICES, 300);
assertEquals(HttpResponse.Status.SEE_OTHER, 303);
assertEquals(HttpResponse.Status.NOT_MODIFIED, 304);
assertEquals(HttpResponse.Status.USE_PROXY, 305);
assertEquals(HttpResponse.Status.PAYMENT_REQUIRED, 402);
assertEquals(HttpResponse.Status.PROXY_AUTHENTICATION_REQUIRED, 407);
assertEquals(HttpResponse.Status.CONFLICT, 409);
assertEquals(HttpResponse.Status.GONE, 410);
assertEquals(HttpResponse.Status.LENGTH_REQUIRED, 411);
assertEquals(HttpResponse.Status.PRECONDITION_FAILED, 412);
assertEquals(HttpResponse.Status.REQUEST_ENTITY_TOO_LARGE, 413);
assertEquals(HttpResponse.Status.REQUEST_URI_TOO_LONG, 414);
assertEquals(HttpResponse.Status.UNSUPPORTED_MEDIA_TYPE, 415);
assertEquals(HttpResponse.Status.REQUEST_RANGE_NOT_SATISFIABLE, 416);
assertEquals(HttpResponse.Status.EXPECTATION_FAILED, 417);
assertEquals(HttpResponse.Status.BAD_GATEWAY, 502);
assertEquals(HttpResponse.Status.GATEWAY_TIMEOUT, 504);
}
@Test
void requireThat5xxIsServerError() {
for (int i = 0; i < 999; ++i) {
assertEquals(i >= 500 && i < 600, HttpResponse.isServerError(new Response(i)));
}
}
@Test
void requireThatCookieHeaderCanBeEncoded() throws Exception {
final HttpResponse response = newResponse(69);
final List<Cookie> cookies = Collections.singletonList(new Cookie("foo", "bar"));
response.encodeSetCookieHeader(cookies);
final List<String> headers = response.headers().get(HttpHeaders.Names.SET_COOKIE);
assertEquals(1, headers.size());
assertEquals(Cookie.toSetCookieHeaders(cookies), headers);
}
@Test
void requireThatMultipleCookieHeadersCanBeEncoded() throws Exception {
final HttpResponse response = newResponse(69);
final List<Cookie> cookies = Arrays.asList(new Cookie("foo", "bar"), new Cookie("baz", "cox"));
response.encodeSetCookieHeader(cookies);
final List<String> headers = response.headers().get(HttpHeaders.Names.SET_COOKIE);
assertEquals(2, headers.size());
assertEquals(Cookie.toSetCookieHeaders(Arrays.asList(new Cookie("foo", "bar"), new Cookie("baz", "cox"))),
headers);
}
@Test
void requireThatCookieHeaderCanBeDecoded() throws Exception {
final HttpResponse response = newResponse(69);
final List<Cookie> cookies = Collections.singletonList(new Cookie("foo", "bar"));
response.encodeSetCookieHeader(cookies);
assertEquals(cookies, response.decodeSetCookieHeader());
}
@Test
void requireThatMultipleCookieHeadersCanBeDecoded() throws Exception {
final HttpResponse response = newResponse(69);
final List<Cookie> cookies = Arrays.asList(new Cookie("foo", "bar"), new Cookie("baz", "cox"));
response.encodeSetCookieHeader(cookies);
assertEquals(cookies, response.decodeSetCookieHeader());
}
private static HttpResponse newResponse(final int status) {
return HttpResponse.newInstance(status, "foo");
}
}
|