|
29 | 29 |
|
30 | 30 | import java.util.Collections;
|
31 | 31 |
|
| 32 | +import org.junit.jupiter.api.AfterEach; |
| 33 | +import org.junit.jupiter.api.BeforeAll; |
| 34 | +import org.junit.jupiter.api.BeforeEach; |
32 | 35 | import org.junit.jupiter.api.Test;
|
33 | 36 | import org.restlet.Client;
|
34 | 37 | import org.restlet.Component;
|
|
40 | 43 | import org.restlet.data.Method;
|
41 | 44 | import org.restlet.data.Protocol;
|
42 | 45 | import org.restlet.data.Status;
|
43 |
| -import org.restlet.engine.header.HeaderConstants; |
44 |
| -import org.restlet.representation.StringRepresentation; |
45 |
| -import org.restlet.util.Series; |
46 | 46 |
|
47 | 47 | public class HeaderTestCase extends RestletTestCase {
|
48 | 48 |
|
49 |
| - /** |
50 |
| - * Restlet that returns as a new Representation the list of values of |
51 |
| - * "testHeader" header. |
52 |
| - */ |
53 |
| - public static class TestHeaderRestlet extends Restlet { |
54 |
| - @Override |
55 |
| - public void handle(Request request, Response response) { |
56 |
| - StringBuilder stb = new StringBuilder(); |
57 |
| - Series<Header> headers = getHttpHeaders(request); |
58 |
| - |
59 |
| - for (Header header : headers) { |
60 |
| - if (TEST_HEADER.equalsIgnoreCase(header.getName())) { |
61 |
| - stb.append(header.getValue()); |
62 |
| - stb.append('\n'); |
63 |
| - } |
64 |
| - } |
65 |
| - |
66 |
| - response.setEntity(new StringRepresentation(stb, |
67 |
| - MediaType.TEXT_PLAIN)); |
68 |
| - } |
69 |
| - } |
70 |
| - |
71 |
| - /** |
72 |
| - * Name of a test header field |
73 |
| - */ |
74 |
| - private static final String TEST_HEADER = "testHeader"; |
75 |
| - |
76 |
| - /** |
77 |
| - * Returns the list of HTTP headers of a request as a Form. |
78 |
| - * |
79 |
| - * @param request |
80 |
| - * The request. |
81 |
| - * @return The list of headers as a Form object. |
82 |
| - */ |
83 |
| - private static Series<Header> getHttpHeaders(Request request) { |
84 |
| - @SuppressWarnings("unchecked") |
85 |
| - Series<Header> headers = (Series<Header>) request.getAttributes().get( |
86 |
| - HeaderConstants.ATTRIBUTE_HEADERS); |
87 |
| - |
88 |
| - if (headers == null) { |
89 |
| - headers = new Series<>(Header.class); |
90 |
| - request.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, |
91 |
| - headers); |
92 |
| - } |
93 |
| - |
94 |
| - return headers; |
95 |
| - } |
96 |
| - |
97 |
| - private Client client; |
98 |
| - |
99 |
| - private Component component; |
100 |
| - |
101 |
| - /** |
102 |
| - * Handle a new request built according to the parameters and return the |
103 |
| - * response object. |
104 |
| - * |
105 |
| - * @param additionalHeaders |
106 |
| - * The list of header used to build the request. |
107 |
| - * @return The response of the request. |
108 |
| - */ |
109 |
| - private Response getWithParams(Header... additionalHeaders) { |
110 |
| - Request request = new Request(Method.GET, "http://localhost:" |
111 |
| - + TEST_PORT); |
112 |
| - Series<Header> headers = getHttpHeaders(request); |
113 |
| - |
114 |
| - Collections.addAll(headers, additionalHeaders); |
115 |
| - |
116 |
| - return client.handle(request); |
117 |
| - } |
118 |
| - |
119 |
| - @Override |
120 |
| - public void setUp() throws Exception { |
121 |
| - super.setUp(); |
122 |
| - this.client = new Client(Protocol.HTTP); |
123 |
| - |
124 |
| - if (this.component == null) { |
125 |
| - this.component = new Component(); |
126 |
| - this.component.getServers().add(Protocol.HTTP, TEST_PORT); |
127 |
| - this.component.getDefaultHost().attachDefault( |
128 |
| - new TestHeaderRestlet()); |
129 |
| - } |
130 |
| - |
131 |
| - if (!this.component.isStarted()) { |
132 |
| - this.component.start(); |
133 |
| - } |
134 |
| - } |
135 |
| - |
136 |
| - @Override |
137 |
| - public void tearDown() throws Exception { |
138 |
| - this.client.stop(); |
139 |
| - this.component.stop(); |
140 |
| - this.component = null; |
141 |
| - super.tearDown(); |
142 |
| - } |
143 |
| - |
144 |
| - /** test with no test header */ |
145 |
| - @Test |
146 |
| - public void test0() throws Exception { |
147 |
| - Response response = getWithParams(); |
148 |
| - assertEquals(Status.SUCCESS_OK, response.getStatus()); |
149 |
| - assertNull(response.getEntity().getText()); |
150 |
| - } |
151 |
| - |
152 |
| - /** test with one test header */ |
153 |
| - @Test |
154 |
| - public void test1() throws Exception { |
155 |
| - Response response = getWithParams(new Header(TEST_HEADER, "a")); |
156 |
| - assertEquals(Status.SUCCESS_OK, response.getStatus()); |
157 |
| - assertEquals("a\n", response.getEntity().getText()); |
158 |
| - } |
159 |
| - |
160 |
| - /** test with two test headers */ |
161 |
| - @Test |
162 |
| - public void test2() throws Exception { |
163 |
| - Response response = getWithParams(new Header(TEST_HEADER, "a"), |
164 |
| - new Header(TEST_HEADER, "b")); |
165 |
| - assertEquals(Status.SUCCESS_OK, response.getStatus()); |
166 |
| - assertEquals("a\nb\n", response.getEntity().getText()); |
167 |
| - } |
| 49 | + /** |
| 50 | + * Restlet that returns as a new Representation the list of values of |
| 51 | + * "testHeader" header. |
| 52 | + */ |
| 53 | + public static class TestHeaderRestlet extends Restlet { |
| 54 | + @Override |
| 55 | + public void handle(Request request, Response response) { |
| 56 | + StringBuilder stb = new StringBuilder(); |
| 57 | + |
| 58 | + for (Header header : request.getHeaders()) { |
| 59 | + if (TEST_HEADER.equalsIgnoreCase(header.getName())) { |
| 60 | + stb.append(header.getValue()); |
| 61 | + stb.append('\n'); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + response.setEntity(stb.toString(), MediaType.TEXT_PLAIN); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Name of a test header field |
| 71 | + */ |
| 72 | + private static final String TEST_HEADER = "testHeader"; |
| 73 | + |
| 74 | + private Client client; |
| 75 | + |
| 76 | + private Component component; |
| 77 | + |
| 78 | + /** |
| 79 | + * Handle a new request built according to the parameters and return the |
| 80 | + * response object. |
| 81 | + * |
| 82 | + * @param additionalHeaders The list of header used to build the request. |
| 83 | + * @return The response of the request. |
| 84 | + */ |
| 85 | + private Response getWithParams(Header... additionalHeaders) { |
| 86 | + Request request = new Request(Method.GET, "http://localhost:" + TEST_PORT); |
| 87 | + Collections.addAll(request.getHeaders(), additionalHeaders); |
| 88 | + return client.handle(request); |
| 89 | + } |
| 90 | + |
| 91 | + @BeforeEach |
| 92 | + void setUpEach() throws Exception { |
| 93 | + this.client = new Client(Protocol.HTTP); |
| 94 | + component = new Component(); |
| 95 | + component.getServers().add(Protocol.HTTP, TEST_PORT); |
| 96 | + component.getDefaultHost().attachDefault(new TestHeaderRestlet()); |
| 97 | + component.start(); |
| 98 | + } |
| 99 | + |
| 100 | + @AfterEach |
| 101 | + void tearDownEach() throws Exception { |
| 102 | + this.client.stop(); |
| 103 | + this.component.stop(); |
| 104 | + this.component = null; |
| 105 | + } |
| 106 | + |
| 107 | + /** Test with no test header */ |
| 108 | + @Test |
| 109 | + public void test0() throws Exception { |
| 110 | + Response response = getWithParams(); |
| 111 | + assertEquals(Status.SUCCESS_OK, response.getStatus()); |
| 112 | + assertNull(response.getEntity().getText()); |
| 113 | + } |
| 114 | + |
| 115 | + /** Test with one test header */ |
| 116 | + @Test |
| 117 | + public void test1() throws Exception { |
| 118 | + Response response = getWithParams(new Header(TEST_HEADER, "a")); |
| 119 | + assertEquals(Status.SUCCESS_OK, response.getStatus()); |
| 120 | + assertEquals("a\n", response.getEntity().getText()); |
| 121 | + } |
| 122 | + |
| 123 | + /** Test with two test headers */ |
| 124 | + @Test |
| 125 | + public void test2() throws Exception { |
| 126 | + Response response = getWithParams(new Header(TEST_HEADER, "a"), new Header(TEST_HEADER, "b")); |
| 127 | + assertEquals(Status.SUCCESS_OK, response.getStatus()); |
| 128 | + assertEquals("a\nb\n", response.getEntity().getText()); |
| 129 | + } |
168 | 130 | }
|
0 commit comments