Skip to content

Commit a3b24ab

Browse files
authored
add possibility to use entity with http method Options in requests according to the RFC 7231 (#4837)
* add possibility to use entity with http method Options in requests according to the RFC 7231 Signed-off-by: aserkes <andrii.serkes@oracle.com>
1 parent dfd0ee7 commit a3b24ab

File tree

8 files changed

+209
-7
lines changed

8 files changed

+209
-7
lines changed

connectors/apache-connector/src/test/java/org/glassfish/jersey/apache/connector/HttpMethodTest.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -42,6 +42,7 @@
4242

4343
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
4444
import org.junit.Test;
45+
4546
import static org.junit.Assert.assertEquals;
4647
import static org.junit.Assert.assertFalse;
4748
import static org.junit.Assert.assertTrue;
@@ -146,6 +147,14 @@ public void testOptions() {
146147
cr.close();
147148
}
148149

150+
@Test
151+
public void testOptionsWithEntity() {
152+
WebTarget r = getWebTarget();
153+
Response response = r.request().build("OPTIONS", Entity.text("OPTIONS")).invoke();
154+
assertEquals(200, response.getStatus());
155+
response.close();
156+
}
157+
149158
@Test
150159
public void testGet() {
151160
WebTarget r = getWebTarget();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.grizzly.connector;
18+
19+
import org.glassfish.jersey.client.ClientConfig;
20+
import org.glassfish.jersey.server.ResourceConfig;
21+
import org.glassfish.jersey.test.JerseyTest;
22+
import org.junit.Test;
23+
24+
import javax.ws.rs.GET;
25+
import javax.ws.rs.Path;
26+
import javax.ws.rs.client.Client;
27+
import javax.ws.rs.client.ClientBuilder;
28+
import javax.ws.rs.client.Entity;
29+
import javax.ws.rs.client.WebTarget;
30+
import javax.ws.rs.core.Application;
31+
import javax.ws.rs.core.Response;
32+
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertTrue;
35+
36+
/**
37+
* Tests the Http methods.
38+
*/
39+
public class HttpMethodTest extends JerseyTest {
40+
41+
@Override
42+
protected Application configure() {
43+
return new ResourceConfig(HttpMethodResource.class);
44+
}
45+
46+
protected Client createClient() {
47+
ClientConfig cc = new ClientConfig();
48+
cc.connectorProvider(new GrizzlyConnectorProvider());
49+
return ClientBuilder.newClient(cc);
50+
}
51+
52+
private WebTarget getWebTarget(final Client client) {
53+
return client.target(getBaseUri()).path("test");
54+
}
55+
56+
private WebTarget getWebTarget() {
57+
return getWebTarget(createClient());
58+
}
59+
60+
@Path("/test")
61+
public static class HttpMethodResource {
62+
@GET
63+
public String get() {
64+
return "GET";
65+
}
66+
}
67+
68+
@Test
69+
public void testOptionsWithEntity() {
70+
WebTarget r = getWebTarget();
71+
Response response = r.request().build("OPTIONS", Entity.text("OPTIONS")).invoke();
72+
assertEquals(200, response.getStatus());
73+
response.close();
74+
}
75+
76+
@Test
77+
public void testGet() {
78+
WebTarget r = getWebTarget();
79+
assertEquals("GET", r.request().get(String.class));
80+
81+
Response cr = r.request().get();
82+
assertTrue(cr.hasEntity());
83+
cr.close();
84+
}
85+
}

connectors/helidon-connector/src/test/java/org/glassfish/jersey/helidon/connector/BasicHelidonConnectorTest.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -55,6 +55,8 @@
5555
import java.util.concurrent.Future;
5656
import java.util.concurrent.TimeUnit;
5757

58+
import static org.junit.Assert.assertEquals;
59+
5860
@RunWith(Parameterized.class)
5961
public class BasicHelidonConnectorTest extends JerseyTest {
6062

@@ -305,4 +307,11 @@ public void testOneClientsTwoReqestsAsync() throws ExecutionException, Interrupt
305307
Assert.assertEquals("long", longResponse.readEntity(String.class));
306308
}
307309
}
310+
311+
@Test
312+
public void testOptionsWithEntity() {
313+
Response response = target("basic").path("get").request().build("OPTIONS", Entity.text("OPTIONS")).invoke();
314+
assertEquals(200, response.getStatus());
315+
response.close();
316+
}
308317
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.jdk.connector;
18+
19+
import org.glassfish.jersey.client.ClientConfig;
20+
import org.glassfish.jersey.server.ResourceConfig;
21+
import org.glassfish.jersey.test.JerseyTest;
22+
import org.junit.Test;
23+
24+
import javax.ws.rs.GET;
25+
import javax.ws.rs.Path;
26+
import javax.ws.rs.client.Client;
27+
import javax.ws.rs.client.ClientBuilder;
28+
import javax.ws.rs.client.Entity;
29+
import javax.ws.rs.client.WebTarget;
30+
import javax.ws.rs.core.Application;
31+
import javax.ws.rs.core.Response;
32+
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertTrue;
35+
36+
/**
37+
* Tests the Http methods.
38+
*/
39+
public class HttpMethodTest extends JerseyTest {
40+
41+
@Override
42+
protected Application configure() {
43+
return new ResourceConfig(HttpMethodResource.class);
44+
}
45+
46+
protected Client createClient() {
47+
ClientConfig cc = new ClientConfig();
48+
cc.connectorProvider(new JdkConnectorProvider());
49+
return ClientBuilder.newClient(cc);
50+
}
51+
52+
private WebTarget getWebTarget(final Client client) {
53+
return client.target(getBaseUri()).path("test");
54+
}
55+
56+
private WebTarget getWebTarget() {
57+
return getWebTarget(createClient());
58+
}
59+
60+
@Path("/test")
61+
public static class HttpMethodResource {
62+
@GET
63+
public String get() {
64+
return "GET";
65+
}
66+
}
67+
68+
@Test
69+
public void testOptionsWithEntity() {
70+
WebTarget r = getWebTarget();
71+
Response response = r.request().build("OPTIONS", Entity.text("OPTIONS")).invoke();
72+
assertEquals(200, response.getStatus());
73+
response.close();
74+
}
75+
76+
@Test
77+
public void testGet() {
78+
WebTarget r = getWebTarget();
79+
assertEquals("GET", r.request().get(String.class));
80+
81+
Response cr = r.request().get();
82+
assertTrue(cr.hasEntity());
83+
cr.close();
84+
}
85+
}

connectors/jetty-connector/src/test/java/org/glassfish/jersey/jetty/connector/MethodTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -143,4 +143,11 @@ public void testPatch() {
143143
Response response = target(PATH).request().method("PATCH", Entity.entity("PATCH", MediaType.TEXT_PLAIN));
144144
assertEquals("PATCH", response.readEntity(String.class));
145145
}
146+
147+
@Test
148+
public void testOptionsWithEntity() {
149+
Response response = target(PATH).request().build("OPTIONS", Entity.text("OPTIONS")).invoke();
150+
assertEquals(200, response.getStatus());
151+
response.close();
152+
}
146153
}

connectors/netty-connector/src/test/java/org/glassfish/jersey/netty/connector/MethodTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2018 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -98,4 +98,11 @@ public void testDelete() {
9898
Response response = target(PATH).request().delete();
9999
assertEquals("DELETE", response.readEntity(String.class));
100100
}
101+
102+
@Test
103+
public void testOptionsWithEntity() {
104+
Response response = target(PATH).request().build("OPTIONS", Entity.text("OPTIONS")).invoke();
105+
assertEquals(200, response.getStatus());
106+
response.close();
107+
}
101108
}

core-client/src/main/java/org/glassfish/jersey/client/JerseyInvocation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static Map<String, EntityPresence> initializeMap() {
123123
map.put("DELETE", EntityPresence.MUST_BE_NULL);
124124
map.put("GET", EntityPresence.MUST_BE_NULL);
125125
map.put("HEAD", EntityPresence.MUST_BE_NULL);
126-
map.put("OPTIONS", EntityPresence.MUST_BE_NULL);
126+
map.put("OPTIONS", EntityPresence.OPTIONAL);
127127
map.put("PATCH", EntityPresence.MUST_BE_PRESENT);
128128
map.put("POST", EntityPresence.OPTIONAL); // we allow to post null instead of entity
129129
map.put("PUT", EntityPresence.MUST_BE_PRESENT);

tests/e2e-client/src/test/java/org/glassfish/jersey/tests/e2e/client/HttpMethodEntityTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -85,7 +85,7 @@ public void testHead() {
8585

8686
@Test
8787
public void testOptions() {
88-
_test("OPTIONS", true, true);
88+
_test("OPTIONS", true, false);
8989
_test("OPTIONS", false, false);
9090
}
9191

0 commit comments

Comments
 (0)