|
| 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 | +} |
0 commit comments