|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2019 Red Hat, Inc., and individual contributors |
| 4 | + * as indicated by the @author tags. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.undertow.servlet.test.request; |
| 20 | + |
| 21 | +import io.undertow.server.handlers.PathHandler; |
| 22 | +import io.undertow.servlet.api.DeploymentInfo; |
| 23 | +import io.undertow.servlet.api.DeploymentManager; |
| 24 | +import io.undertow.servlet.api.ServletContainer; |
| 25 | +import io.undertow.servlet.test.SimpleServletTestCase; |
| 26 | +import io.undertow.servlet.test.util.TestClassIntrospector; |
| 27 | +import io.undertow.testutils.DefaultServer; |
| 28 | +import io.undertow.testutils.HttpClientUtils; |
| 29 | +import io.undertow.testutils.TestHttpClient; |
| 30 | +import io.undertow.util.StatusCodes; |
| 31 | +import org.apache.http.HttpResponse; |
| 32 | +import org.apache.http.client.methods.HttpGet; |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.BeforeClass; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | + |
| 38 | +import javax.servlet.ServletException; |
| 39 | + |
| 40 | +import static io.undertow.servlet.Servlets.servlet; |
| 41 | + |
| 42 | +/** |
| 43 | + * Tests that ge get ip or host name depending on the method call |
| 44 | + * @author tmiyar |
| 45 | + * |
| 46 | + */ |
| 47 | +@RunWith(DefaultServer.class) |
| 48 | +public class HttpHostValuesTestCase { |
| 49 | + |
| 50 | + @BeforeClass |
| 51 | + public static void setup() throws ServletException { |
| 52 | + |
| 53 | + |
| 54 | + final PathHandler pathHandler = new PathHandler(); |
| 55 | + final ServletContainer container = ServletContainer.Factory.newInstance(); |
| 56 | + DeploymentInfo builder = new DeploymentInfo() |
| 57 | + .setClassLoader(SimpleServletTestCase.class.getClassLoader()) |
| 58 | + .setContextPath("/servletContext") |
| 59 | + .setClassIntrospecter(TestClassIntrospector.INSTANCE) |
| 60 | + .setDeploymentName("servletContext.war") |
| 61 | + .addServlets( |
| 62 | + servlet("request", RequestHostValuesServlet.class) |
| 63 | + .addMapping("/")); |
| 64 | + |
| 65 | + DeploymentManager manager = container.addDeployment(builder); |
| 66 | + manager.deploy(); |
| 67 | + try { |
| 68 | + pathHandler.addPrefixPath(builder.getContextPath(), manager.start()); |
| 69 | + } catch (ServletException e) { |
| 70 | + throw new RuntimeException(e); |
| 71 | + } |
| 72 | + DefaultServer.setRootHandler(pathHandler); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testRequestSpec() throws Exception { |
| 78 | + //test request values |
| 79 | + TestHttpClient client = new TestHttpClient(); |
| 80 | + try { |
| 81 | + HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/"); |
| 82 | + HttpResponse result = client.execute(get); |
| 83 | + Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); |
| 84 | + final String response = HttpClientUtils.readResponse(result); |
| 85 | + |
| 86 | + if(System.getProperty("os.name").toLowerCase().contains("windows") || System.getSecurityManager() != null) { |
| 87 | + Assert.assertTrue(String.format("hostName: %s , response: %s", DefaultServer.getDefaultServerAddress().toString(), response), DefaultServer.getDefaultServerAddress().toString().contains(response)); |
| 88 | + } else { |
| 89 | + Assert.assertTrue(String.format("hostName: %s , response: %s", DefaultServer.getHostAddress(), response), DefaultServer.getHostAddress().equals(response)); |
| 90 | + } |
| 91 | + |
| 92 | + } finally { |
| 93 | + client.close(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments