|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, 2019 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.netty.httpserver; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | +import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.logging.Logger; |
| 24 | +import javax.ws.rs.GET; |
| 25 | +import javax.ws.rs.Path; |
| 26 | +import javax.ws.rs.Produces; |
| 27 | +import javax.ws.rs.client.InvocationCallback; |
| 28 | +import javax.ws.rs.core.Application; |
| 29 | +import javax.ws.rs.core.MediaType; |
| 30 | +import javax.ws.rs.core.Response; |
| 31 | +import org.glassfish.jersey.client.ClientConfig; |
| 32 | +import org.glassfish.jersey.client.ClientProperties; |
| 33 | +import org.glassfish.jersey.logging.LoggingFeature; |
| 34 | +import org.glassfish.jersey.netty.connector.NettyConnectorProvider; |
| 35 | +import org.glassfish.jersey.server.ResourceConfig; |
| 36 | +import org.glassfish.jersey.test.JerseyTest; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author Pavel Bucek |
| 41 | + */ |
| 42 | +public class HelloWorldTest extends JerseyTest { |
| 43 | + |
| 44 | + private static final Logger LOGGER = Logger.getLogger(HelloWorldTest.class.getName()); |
| 45 | + private static final String ROOT_PATH = "helloworld"; |
| 46 | + |
| 47 | + public HelloWorldTest() { |
| 48 | + super(new NettyTestContainerFactory()); |
| 49 | + } |
| 50 | + |
| 51 | + @Path("helloworld") |
| 52 | + public static class HelloWorldResource { |
| 53 | + public static final String CLICHED_MESSAGE = "Hello World!"; |
| 54 | + |
| 55 | + @GET |
| 56 | + @Produces("text/plain") |
| 57 | + public String getHello() { |
| 58 | + return CLICHED_MESSAGE; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected Application configure() { |
| 64 | + ResourceConfig config = new ResourceConfig(HelloWorldResource.class); |
| 65 | + config.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.PAYLOAD_ANY)); |
| 66 | + return config; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void configureClient(ClientConfig config) { |
| 71 | + config.property(ClientProperties.ASYNC_THREADPOOL_SIZE, 20); |
| 72 | + config.connectorProvider(new NettyConnectorProvider()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testConnection() { |
| 77 | + Response response = target().path(ROOT_PATH).request("text/plain").get(); |
| 78 | + assertEquals(200, response.getStatus()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testClientStringResponse() { |
| 83 | + String s = target().path(ROOT_PATH).request().get(String.class); |
| 84 | + assertEquals(HelloWorldResource.CLICHED_MESSAGE, s); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testAsyncClientRequests() throws InterruptedException { |
| 89 | + final int REQUESTS = 20; |
| 90 | + final CountDownLatch latch = new CountDownLatch(REQUESTS); |
| 91 | + final long tic = System.currentTimeMillis(); |
| 92 | + for (int i = 0; i < REQUESTS; i++) { |
| 93 | + final int id = i; |
| 94 | + target().path(ROOT_PATH).request().async().get(new InvocationCallback<Response>() { |
| 95 | + @Override |
| 96 | + public void completed(Response response) { |
| 97 | + try { |
| 98 | + final String result = response.readEntity(String.class); |
| 99 | + assertEquals(HelloWorldResource.CLICHED_MESSAGE, result); |
| 100 | + } finally { |
| 101 | + latch.countDown(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void failed(Throwable error) { |
| 107 | + error.printStackTrace(); |
| 108 | + latch.countDown(); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + assertTrue(latch.await(10 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS)); |
| 113 | + final long toc = System.currentTimeMillis(); |
| 114 | + Logger.getLogger(HelloWorldTest.class.getName()).info("Executed in: " + (toc - tic)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testHead() { |
| 119 | + Response response = target().path(ROOT_PATH).request().head(); |
| 120 | + assertEquals(200, response.getStatus()); |
| 121 | + assertEquals(MediaType.TEXT_PLAIN_TYPE, response.getMediaType()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testFooBarOptions() { |
| 126 | + Response response = target().path(ROOT_PATH).request().header("Accept", "foo/bar").options(); |
| 127 | + assertEquals(200, response.getStatus()); |
| 128 | + final String allowHeader = response.getHeaderString("Allow"); |
| 129 | + _checkAllowContent(allowHeader); |
| 130 | + assertEquals("foo/bar", response.getMediaType().toString()); |
| 131 | + assertEquals(0, response.getLength()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testTextPlainOptions() { |
| 136 | + Response response = target().path(ROOT_PATH).request().header("Accept", MediaType.TEXT_PLAIN).options(); |
| 137 | + assertEquals(200, response.getStatus()); |
| 138 | + final String allowHeader = response.getHeaderString("Allow"); |
| 139 | + _checkAllowContent(allowHeader); |
| 140 | + assertEquals(MediaType.TEXT_PLAIN_TYPE, response.getMediaType()); |
| 141 | + final String responseBody = response.readEntity(String.class); |
| 142 | + _checkAllowContent(responseBody); |
| 143 | + } |
| 144 | + |
| 145 | + public void testJson() { |
| 146 | + Response response = target().path(ROOT_PATH).request().header("Accept", MediaType.APPLICATION_JSON).options(); |
| 147 | + assertEquals(200, response.getStatus()); |
| 148 | + final String allowHeader = response.getHeaderString("Allow"); |
| 149 | + _checkAllowContent(allowHeader); |
| 150 | + assertEquals(MediaType.APPLICATION_JSON, response.getMediaType()); |
| 151 | + final String responseBody = response.readEntity(String.class); |
| 152 | + _checkAllowContent(responseBody); |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + private void _checkAllowContent(final String content) { |
| 157 | + assertTrue(content.contains("GET")); |
| 158 | + assertTrue(content.contains("HEAD")); |
| 159 | + assertTrue(content.contains("OPTIONS")); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testMissingResourceNotFound() { |
| 164 | + Response response; |
| 165 | + |
| 166 | + response = target().path(ROOT_PATH + "arbitrary").request().get(); |
| 167 | + assertEquals(404, response.getStatus()); |
| 168 | + response.close(); |
| 169 | + |
| 170 | + response = target().path(ROOT_PATH).path("arbitrary").request().get(); |
| 171 | + assertEquals(404, response.getStatus()); |
| 172 | + response.close(); |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments