1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.IOException;
23  
24  import javax.servlet.ServletOutputStream;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.hadoop.hbase.MediumTests;
28  import org.apache.hadoop.hbase.rest.filter.GZIPResponseStream;
29  import org.apache.hadoop.hbase.rest.filter.GZIPResponseWrapper;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  import static org.mockito.Mockito.*;
34  import static org.junit.Assert.*;
35  
36  @Category(MediumTests.class)
37  public class TestGZIPResponseWrapper {
38  
39    /**
40     * headers function should be called in response except header "content-length"
41     * 
42     * @throws IOException
43     */
44    @Test
45    public void testHeader() throws IOException {
46  
47      HttpServletResponse response = mock(HttpServletResponse.class);
48  
49      GZIPResponseWrapper test = new GZIPResponseWrapper(response);
50      test.setStatus(200);
51      verify(response).setStatus(200);
52      test.addHeader("header", "header value");
53      verify(response).addHeader("header", "header value");
54      test.addHeader("content-length", "header value2");
55      verify(response, never()).addHeader("content-length", "header value");
56  
57      test.setIntHeader("header", 5);
58      verify(response).setIntHeader("header", 5);
59      test.setIntHeader("content-length", 4);
60      verify(response, never()).setIntHeader("content-length", 4);
61  
62      test.setHeader("set-header", "new value");
63      verify(response).setHeader("set-header", "new value");
64      test.setHeader("content-length", "content length value");
65      verify(response, never()).setHeader("content-length", "content length value");
66  
67      test.sendRedirect("location");
68      verify(response).sendRedirect("location");
69      
70      test.flushBuffer();
71      verify(response).flushBuffer();
72  
73    }
74  
75    @Test
76    public void testResetBuffer() throws IOException {
77      HttpServletResponse response = mock(HttpServletResponse.class);
78      when(response.isCommitted()).thenReturn(false);
79      ServletOutputStream out = mock(ServletOutputStream.class);
80      when(response.getOutputStream()).thenReturn(out);
81      GZIPResponseWrapper test = new GZIPResponseWrapper(response);
82  
83      ServletOutputStream servletOutput = test.getOutputStream();
84      assertEquals(org.apache.hadoop.hbase.rest.filter.GZIPResponseStream.class,
85          servletOutput.getClass());
86      test.resetBuffer();
87      verify(response).setHeader("Content-Encoding", null);
88  
89      when(response.isCommitted()).thenReturn(true);
90      servletOutput = test.getOutputStream();
91      assertEquals(out.getClass(), servletOutput.getClass());
92      assertNotNull(test.getWriter());
93  
94    }
95  
96    @Test
97    public void testReset() throws IOException {
98      HttpServletResponse response = mock(HttpServletResponse.class);
99      when(response.isCommitted()).thenReturn(false);
100     ServletOutputStream out = mock(ServletOutputStream.class);
101     when(response.getOutputStream()).thenReturn(out);
102     GZIPResponseWrapper test = new GZIPResponseWrapper(response);
103 
104     ServletOutputStream servletOutput = test.getOutputStream();
105     assertEquals(org.apache.hadoop.hbase.rest.filter.GZIPResponseStream.class,
106         servletOutput.getClass());
107     test.reset();
108     verify(response).setHeader("Content-Encoding", null);
109 
110     when(response.isCommitted()).thenReturn(true);
111     servletOutput = test.getOutputStream();
112     assertEquals(out.getClass(), servletOutput.getClass());
113   }
114 
115   @Test
116   public void testSendError() throws IOException {
117     HttpServletResponse response = mock(HttpServletResponse.class);
118     GZIPResponseWrapper test = new GZIPResponseWrapper(response);
119 
120     test.sendError(404);
121     verify(response).sendError(404);
122 
123     test.sendError(404, "error message");
124     verify(response).sendError(404, "error message");
125 
126   }
127 
128   @Test
129   public void testGZIPResponseStream() throws IOException {
130     HttpServletResponse httpResponce = mock(HttpServletResponse.class);
131     ServletOutputStream out = mock(ServletOutputStream.class);
132 
133     when(httpResponce.getOutputStream()).thenReturn(out);
134     GZIPResponseStream test = new GZIPResponseStream(httpResponce);
135     verify(httpResponce).addHeader("Content-Encoding", "gzip");
136 
137     test.close();
138 
139     test.resetBuffer();
140     verify(httpResponce).setHeader("Content-Encoding", null);
141   }
142 }