/
ETagResponseFilterTest.java
225 lines (187 loc) · 8.17 KB
/
ETagResponseFilterTest.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
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.everrest;
import org.eclipse.che.api.core.rest.ApiExceptionMapper;
import org.everrest.core.ApplicationContext;
import org.everrest.core.impl.ApplicationProviderBinder;
import org.everrest.core.impl.ContainerRequest;
import org.everrest.core.impl.ContainerResponse;
import org.everrest.core.impl.EverrestConfiguration;
import org.everrest.core.impl.EverrestProcessor;
import org.everrest.core.impl.RequestDispatcher;
import org.everrest.core.impl.RequestHandlerImpl;
import org.everrest.core.impl.ResourceBinderImpl;
import org.everrest.core.tools.DependencySupplierImpl;
import org.everrest.core.tools.ResourceLauncher;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.ws.rs.GET;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.Response.Status.NOT_MODIFIED;
import static javax.ws.rs.core.Response.Status.OK;
import static org.everrest.core.ApplicationContext.anApplicationContext;
import static org.testng.Assert.assertEquals;
/**
* Test the ETag filter
* @author Florent Benoit
*/
public class ETagResponseFilterTest {
/**
* Base URI
*/
private static final String BASE_URI = "http://localhost/service";
/**
* Base Service
*/
private static final String SERVICE_PATH = BASE_URI + "/myservice";
/**
* Dummy JAX-RS POJO
*/
@Path("/myservice")
public static class MyJaxRSService {
@GET
@Path("/list")
@Produces(APPLICATION_JSON)
public List<String> getMembers() {
return Arrays.asList("a", "b", "c");
}
@GET
@Path("/single")
@Produces(APPLICATION_JSON)
public String getMember() {
return "hello";
}
@GET
@Path("/modify")
@Produces(APPLICATION_JSON)
public Response modifyHeader() {
return Response.ok("helloContent")
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=my.json")
.build();
}
}
/**
* Resource Launcher
*/
private ResourceLauncher resourceLauncher;
/**
* Setup env for launching requests
* @throws Exception
*/
@BeforeMethod
public void before() throws Exception {
//set up launcher
final ResourceBinderImpl resources = new ResourceBinderImpl();
resources.addResource(MyJaxRSService.class, null);
final DependencySupplierImpl dependencies = new DependencySupplierImpl();
final ApplicationProviderBinder providers = new ApplicationProviderBinder();
providers.addExceptionMapper(ApiExceptionMapper.class);
providers.addResponseFilter(ETagResponseFilter.class);
final URI uri = new URI(BASE_URI);
final ContainerRequest req = new ContainerRequest(null, uri, uri, null, null, null);
final ApplicationContext contextImpl = anApplicationContext().withRequest(req).withProviders(providers).build();
contextImpl.setDependencySupplier(dependencies);
ApplicationContext.setCurrent(contextImpl);
final EverrestProcessor processor = new EverrestProcessor(new EverrestConfiguration(),
dependencies,
new RequestHandlerImpl(new RequestDispatcher(resources), providers),
null);
resourceLauncher = new ResourceLauncher(processor);
}
/**
* Check if ETag is generated for a list of JSON
*/
@Test
public void filterListEntityTest() throws Exception {
final ContainerResponse response = resourceLauncher.service(HttpMethod.GET, SERVICE_PATH + "/list", BASE_URI, null, null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
// check entity
Assert.assertEquals(response.getEntity(), Arrays.asList("a", "b", "c"));
// Check etag
List<Object> headerTags = response.getHttpHeaders().get("ETag");
Assert.assertNotNull(headerTags);
Assert.assertEquals(headerTags.size(), 1);
Assert.assertEquals(headerTags.get(0), new EntityTag("900150983cd24fb0d6963f7d28e17f72"));
}
/**
* Check if ETag is added in response if we're also using a custom header
*/
@Test
public void useExistingHeaders() throws Exception {
final ContainerResponse response = resourceLauncher.service(HttpMethod.GET, SERVICE_PATH + "/modify", BASE_URI, null, null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
// check entity
Assert.assertEquals(response.getEntity(), "helloContent");
// headers = 2
Assert.assertEquals(response.getHttpHeaders().keySet().size(), 3);
// Check custom header
List<Object> customTags = response.getHttpHeaders().get(HttpHeaders.CONTENT_DISPOSITION);
Assert.assertNotNull(customTags);
Assert.assertEquals(customTags.size(), 1);
Assert.assertEquals(customTags.get(0), "attachment; filename=my.json");
// Check etag
List<Object> headerTags = response.getHttpHeaders().get("ETag");
Assert.assertNotNull(headerTags);
Assert.assertEquals(headerTags.get(0), new EntityTag("77e671575d94cfd400ed26c5ef08e0fd"));
}
/**
* Check if ETag is generated for a simple entity of JSON
*/
@Test
public void filterSingleEntityTest() throws Exception {
final ContainerResponse response = resourceLauncher.service(HttpMethod.GET, SERVICE_PATH + "/single", BASE_URI, null, null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
// check entity
Assert.assertEquals(response.getEntity(), "hello");
// Check etag
List<Object> headerTags = response.getHttpHeaders().get("ETag");
Assert.assertNotNull(headerTags);
Assert.assertEquals(headerTags.size(), 1);
Assert.assertEquals(headerTags.get(0), new EntityTag("5d41402abc4b2a76b9719d911017c592"));
}
/**
* Check if ETag sent with header is redirecting to NOT_MODIFIED
*/
@Test
public void filterListEntityTestWithEtag() throws Exception {
Map<String, List<String>> headers = new HashMap<>();
headers.put("If-None-Match", Collections.singletonList(new EntityTag("900150983cd24fb0d6963f7d28e17f72").toString()));
final ContainerResponse response = resourceLauncher.service(HttpMethod.GET, SERVICE_PATH + "/list", BASE_URI, headers, null, null);
assertEquals(response.getStatus(), NOT_MODIFIED.getStatusCode());
// check null body
Assert.assertNull(response.getEntity());
}
/**
* Check if ETag sent with header is redirecting to NOT_MODIFIED
*/
@Test
public void filterSingleEntityTestWithEtag() throws Exception {
Map<String, List<String>> headers = new HashMap<>();
headers.put("If-None-Match", Collections.singletonList(new EntityTag("5d41402abc4b2a76b9719d911017c592").toString()));
final ContainerResponse response = resourceLauncher.service(HttpMethod.GET, SERVICE_PATH + "/single", BASE_URI, headers, null, null);
assertEquals(response.getStatus(), NOT_MODIFIED.getStatusCode());
// check null body
Assert.assertNull(response.getEntity());
}
}