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ülcü", new byte[] { (byte) 0x47, (byte) 0xC3, (byte) 0xBC, (byte) 0x6C, (byte) 0x63, (byte) 0xC3, (byte) 0xBC } },
47                  { "iso-8859-1", "Gülcü", new byte[] { (byte) 0x47, (byte) 0xFC, (byte) 0x6C, (byte) 0x63, (byte) 0xFC } } });
48      }
49  
50      @Test
51      public void testWriterEncoding() throws IOException {
52          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
53  
54          DummyResponse dummyResponse = new DummyResponse();
55          dummyResponse.setCharacterEncoding(characterEncoding);
56          dummyResponse.setOutputStream(new DummyServletOutputStream(byteArrayOutputStream));
57  
58          TeeHttpServletResponse teeServletResponse = new TeeHttpServletResponse(dummyResponse);
59  
60          PrintWriter writer = teeServletResponse.getWriter();
61          writer.write(testString);
62          writer.flush();
63  
64          assertArrayEquals(expectedBytes, byteArrayOutputStream.toByteArray());
65      }
66  
67  }