/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.coyote;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Assert;
import org.junit.Test;

import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.ByteChunk;

public class TestResponse extends TomcatBaseTest {

    /*
     * https://bz.apache.org/bugzilla/show_bug.cgi?id=61197
     */
    @Test
    public void testUserCharsetIsRetained() throws Exception {
        Tomcat tomcat = getTomcatInstance();

        // No file system docBase required
        Context ctx = tomcat.addContext("", null);

        // Add  servlet
        Tomcat.addServlet(ctx, "CharsetServlet", new CharsetServlet());
        ctx.addServletMappingDecoded("/*", "CharsetServlet");

        tomcat.start();

        ByteChunk responseBody = new ByteChunk();
        Map<String,List<String>> responseHeaders = new HashMap<>();
        int rc = getUrl("http://localhost:" + getPort() + "/test?charset=uTf-8", responseBody,
                responseHeaders);

        Assert.assertEquals(HttpServletResponse.SC_OK, rc);
        Assert.assertTrue(responseHeaders.containsKey("Content-Type"));
        List<String> contentType = responseHeaders.get("Content-Type");
        Assert.assertEquals(1, contentType.size());
        Assert.assertEquals("text/plain;charset=uTf-8", contentType.get(0));
    }


    private static class CharsetServlet extends HttpServlet {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setContentType("text/plain");
            resp.setCharacterEncoding(req.getParameter("charset"));

            resp.getWriter().print("OK");
        }
    }
}

@@ -1132,16 +1132,41 @@
 Assert;%0A
+import org.junit.Ignore;%0A
 import o
@@ -2856,24 +2856,2242 @@
 harset%22));%0A%0A
+            resp.getWriter().print(%22OK%22);%0A        %7D%0A    %7D%0A%0A%0A    @Test%0A    public void testContentTypeWithSpace() throws Exception %7B%0A        doTestContentTypeSpacing(true);%0A    %7D%0A%0A%0A    @Ignore // Disabled until Bug 62912 is addressed%0A    @Test%0A    public void testContentTypeWithoutSpace() throws Exception %7B%0A        doTestContentTypeSpacing(false);%0A    %7D%0A%0A%0A    private void doTestContentTypeSpacing(boolean withSpace) throws Exception %7B%0A        Tomcat tomcat = getTomcatInstance();%0A%0A        // No file system docBase required%0A        Context ctx = tomcat.addContext(%22%22, null);%0A%0A        // Add  servlet%0A        Tomcat.addServlet(ctx, %22ContentTypeServlet%22, new ContentTypeServlet());%0A        ctx.addServletMappingDecoded(%22/*%22, %22ContentTypeServlet%22);%0A%0A        tomcat.start();%0A%0A        ByteChunk responseBody = new ByteChunk();%0A        Map%3CString,List%3CString%3E%3E responseHeaders = new HashMap%3C%3E();%0A        StringBuilder uri = new StringBuilder(%22http://localhost:%22);%0A        uri.append(getPort());%0A        uri.append(%22/test%22);%0A        if (withSpace) %7B%0A            uri.append(%22?withSpace=true%22);%0A        %7D%0A        int rc = getUrl(uri.toString(), responseBody, responseHeaders);%0A%0A        Assert.assertEquals(HttpServletResponse.SC_OK, rc);%0A        Assert.assertTrue(responseHeaders.containsKey(%22Content-Type%22));%0A        List%3CString%3E contentType = responseHeaders.get(%22Content-Type%22);%0A        Assert.assertEquals(1, contentType.size());%0A        StringBuilder expected = new StringBuilder(%22text/plain;%22);%0A        if (withSpace) %7B%0A            expected.append(%22 %22);%0A        %7D%0A        expected.append(%22v=1;charset=UTF-8%22);%0A        Assert.assertEquals(expected.toString() , contentType.get(0));%0A    %7D%0A%0A%0A    private static class ContentTypeServlet extends HttpServlet %7B%0A%0A        private static final long serialVersionUID = 1L;%0A%0A        @Override%0A        protected void doGet(HttpServletRequest req, HttpServletResponse resp)%0A                throws ServletException, IOException %7B%0A%0A            if (req.getParameter(%22withSpace%22) == null) %7B%0A                resp.setContentType(%22text/plain;v=1%22);%0A            %7D else %7B%0A                resp.setContentType(%22text/plain; v=1%22);%0A            %7D%0A            resp.setCharacterEncoding(%22UTF-8%22);%0A%0A
             

Add a test case for BZ 62912