View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.access.servlet;
15  
16  import ch.qos.logback.access.dummy.DummyResponse;
17  import ch.qos.logback.access.dummy.DummyServletOutputStream;
18  import org.junit.Test;
19  import org.junit.runner.RunWith;
20  import org.junit.runners.Parameterized;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  import java.util.Arrays;
26  import java.util.Collection;
27  
28  import static org.junit.Assert.assertArrayEquals;
29  
30  @RunWith(Parameterized.class)
31  public class TeeHttpServletResponseTest {
32  
33      String characterEncoding;
34      String testString;
35      byte[] expectedBytes;
36  
37      public TeeHttpServletResponseTest(String characterEncoding, String testString, byte[] expectedBytes) {
38          this.characterEncoding = characterEncoding;
39          this.testString = testString;
40          this.expectedBytes = expectedBytes;
41      }
42  
43      @Parameterized.Parameters
44      public static Collection<?> inputValues() {
45          return Arrays.asList(new Object[][] {
46                  { "utf-8", "G\u00FClc\u00FC",
47                          new byte[] { (byte) 0x47, (byte) 0xC3, (byte) 0xBC, (byte) 0x6C, (byte) 0x63, (byte) 0xC3,
48                                  (byte) 0xBC } },
49                  { "iso-8859-1", "G\u00FClc\u00FC",
50                          new byte[] { (byte) 0x47, (byte) 0xFC, (byte) 0x6C, (byte) 0x63, (byte) 0xFC } } });
51      }
52  
53      @Test
54      public void testWriterEncoding() throws IOException {
55          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
56  
57          DummyResponse dummyResponse = new DummyResponse();
58          dummyResponse.setCharacterEncoding(characterEncoding);
59          dummyResponse.setOutputStream(new DummyServletOutputStream(byteArrayOutputStream));
60  
61          TeeHttpServletResponse teeServletResponse = new TeeHttpServletResponse(dummyResponse);
62  
63          PrintWriter writer = teeServletResponse.getWriter();
64          writer.write(testString);
65          writer.flush();
66  
67          assertArrayEquals(expectedBytes, byteArrayOutputStream.toByteArray());
68      }
69  
70  }