|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2012 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.response.contenttype; |
| 20 | + |
| 21 | +import java.net.URLEncoder; |
| 22 | + |
| 23 | +import javax.servlet.ServletException; |
| 24 | + |
| 25 | +import io.undertow.server.handlers.PathHandler; |
| 26 | +import io.undertow.servlet.api.DeploymentInfo; |
| 27 | +import io.undertow.servlet.api.DeploymentManager; |
| 28 | +import io.undertow.servlet.api.ServletContainer; |
| 29 | +import io.undertow.servlet.api.ServletInfo; |
| 30 | +import io.undertow.servlet.test.SimpleServletTestCase; |
| 31 | +import io.undertow.servlet.test.util.TestClassIntrospector; |
| 32 | +import io.undertow.servlet.test.util.TestResourceLoader; |
| 33 | +import io.undertow.test.utils.DefaultServer; |
| 34 | +import io.undertow.test.utils.HttpClientUtils; |
| 35 | +import io.undertow.util.TestHttpClient; |
| 36 | +import org.apache.http.HttpResponse; |
| 37 | +import org.apache.http.client.methods.HttpGet; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.BeforeClass; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | + |
| 43 | +/** |
| 44 | + * @author Stuart Douglas |
| 45 | + */ |
| 46 | +@RunWith(DefaultServer.class) |
| 47 | +public class ContentTypeCharsetTestCase { |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + public static void setup() throws ServletException { |
| 51 | + |
| 52 | + final PathHandler root = new PathHandler(); |
| 53 | + final ServletContainer container = ServletContainer.Factory.newInstance(); |
| 54 | + |
| 55 | + ServletInfo m = new ServletInfo("charset", ContentTypeServlet.class) |
| 56 | + .addMapping("/*"); |
| 57 | + |
| 58 | + DeploymentInfo builder = new DeploymentInfo() |
| 59 | + .setClassLoader(SimpleServletTestCase.class.getClassLoader()) |
| 60 | + .setContextPath("/servletContext") |
| 61 | + .setClassIntrospecter(TestClassIntrospector.INSTANCE) |
| 62 | + .setDeploymentName("servletContext.war") |
| 63 | + .setResourceLoader(TestResourceLoader.NOOP_RESOURCE_LOADER) |
| 64 | + .addServlets(m); |
| 65 | + |
| 66 | + DeploymentManager manager = container.addDeployment(builder); |
| 67 | + manager.deploy(); |
| 68 | + root.addPath(builder.getContextPath(), manager.start()); |
| 69 | + |
| 70 | + DefaultServer.setRootHandler(root); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testCharsetAndContentType() throws Exception { |
| 75 | + runtest("text/html", "UTF8", "text/html;charset=UTF8", "text/html;charset=UTF8\nUTF8"); |
| 76 | + runtest("text/html", "", "text/html", "text/html\nISO-8859-1"); |
| 77 | + runtest("text/html; charset=UTF8", "", "text/html;charset=UTF8", "text/html;charset=UTF8\nUTF8"); |
| 78 | + runtest("text/html; charset=UTF8; boundary=someString;", "", "text/html; boundary=someString;charset=UTF8", "text/html; boundary=someString;charset=UTF8\nUTF8"); |
| 79 | + runtest("text/html; charset=UTF8; boundary=someString; ", "", "text/html; boundary=someString;charset=UTF8", "text/html; boundary=someString;charset=UTF8\nUTF8"); |
| 80 | + runtest("multipart/related; type=\"text/xml\"; boundary=\"uuid:ce7d652a-d035-42fa-962c-5b8315084e32\"; start=\"<root.message@cxf.apache.org>\"; start-info=\"text/xml\"", "", "multipart/related; type=\"text/xml\"; boundary=\"uuid:ce7d652a-d035-42fa-962c-5b8315084e32\"; start=\"<root.message@cxf.apache.org>\"; start-info=\"text/xml\"", "multipart/related; type=\"text/xml\"; boundary=\"uuid:ce7d652a-d035-42fa-962c-5b8315084e32\"; start=\"<root.message@cxf.apache.org>\"; start-info=\"text/xml\"\nISO-8859-1"); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + private void runtest(String contentType, String charset, String expectedContentType, String expectedBody) throws Exception { |
| 85 | + TestHttpClient client = new TestHttpClient(); |
| 86 | + try { |
| 87 | + HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/test?contentType=" + URLEncoder.encode(contentType) + "&charset=" + URLEncoder.encode(charset)); |
| 88 | + HttpResponse result = client.execute(get); |
| 89 | + Assert.assertEquals(200, result.getStatusLine().getStatusCode()); |
| 90 | + final String response = HttpClientUtils.readResponse(result); |
| 91 | + Assert.assertEquals(expectedContentType, result.getHeaders("Content-Type")[0].getValue()); |
| 92 | + Assert.assertEquals(expectedBody, response); |
| 93 | + } finally { |
| 94 | + client.getConnectionManager().shutdown(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments