2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.rest
;
20 import static org
.junit
.Assert
.assertEquals
;
21 import static org
.junit
.Assert
.assertNotNull
;
22 import static org
.mockito
.Mockito
.mock
;
23 import static org
.mockito
.Mockito
.never
;
24 import static org
.mockito
.Mockito
.verify
;
25 import static org
.mockito
.Mockito
.when
;
27 import java
.io
.IOException
;
28 import javax
.servlet
.ServletOutputStream
;
29 import javax
.servlet
.http
.HttpServletResponse
;
30 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
31 import org
.apache
.hadoop
.hbase
.rest
.filter
.GZIPResponseStream
;
32 import org
.apache
.hadoop
.hbase
.rest
.filter
.GZIPResponseWrapper
;
33 import org
.apache
.hadoop
.hbase
.testclassification
.RestTests
;
34 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
35 import org
.junit
.ClassRule
;
36 import org
.junit
.Test
;
37 import org
.junit
.experimental
.categories
.Category
;
39 @Category({RestTests
.class, SmallTests
.class})
40 public class TestGZIPResponseWrapper
{
43 public static final HBaseClassTestRule CLASS_RULE
=
44 HBaseClassTestRule
.forClass(TestGZIPResponseWrapper
.class);
46 private final HttpServletResponse response
= mock(HttpServletResponse
.class);
47 private final GZIPResponseWrapper wrapper
= new GZIPResponseWrapper(response
);
50 * wrapper should set all headers except "content-length"
53 public void testHeader() throws IOException
{
54 wrapper
.setStatus(200);
55 verify(response
).setStatus(200);
56 wrapper
.addHeader("header", "header value");
57 verify(response
).addHeader("header", "header value");
58 wrapper
.addHeader("content-length", "header value2");
59 verify(response
, never()).addHeader("content-length", "header value");
61 wrapper
.setIntHeader("header", 5);
62 verify(response
).setIntHeader("header", 5);
63 wrapper
.setIntHeader("content-length", 4);
64 verify(response
, never()).setIntHeader("content-length", 4);
66 wrapper
.setHeader("set-header", "new value");
67 verify(response
).setHeader("set-header", "new value");
68 wrapper
.setHeader("content-length", "content length value");
69 verify(response
, never()).setHeader("content-length", "content length value");
71 wrapper
.sendRedirect("location");
72 verify(response
).sendRedirect("location");
74 wrapper
.flushBuffer();
75 verify(response
).flushBuffer();
79 public void testResetBuffer() throws IOException
{
80 when(response
.isCommitted()).thenReturn(false);
81 ServletOutputStream out
= mock(ServletOutputStream
.class);
82 when(response
.getOutputStream()).thenReturn(out
);
84 ServletOutputStream servletOutput
= wrapper
.getOutputStream();
85 assertEquals(GZIPResponseStream
.class, servletOutput
.getClass());
86 wrapper
.resetBuffer();
87 verify(response
).setHeader("Content-Encoding", null);
89 when(response
.isCommitted()).thenReturn(true);
90 servletOutput
= wrapper
.getOutputStream();
91 assertEquals(out
.getClass(), servletOutput
.getClass());
92 assertNotNull(wrapper
.getWriter());
96 public void testReset() throws IOException
{
97 when(response
.isCommitted()).thenReturn(false);
98 ServletOutputStream out
= mock(ServletOutputStream
.class);
99 when(response
.getOutputStream()).thenReturn(out
);
101 ServletOutputStream servletOutput
= wrapper
.getOutputStream();
102 verify(response
).addHeader("Content-Encoding", "gzip");
103 assertEquals(GZIPResponseStream
.class, servletOutput
.getClass());
105 verify(response
).setHeader("Content-Encoding", null);
107 when(response
.isCommitted()).thenReturn(true);
108 servletOutput
= wrapper
.getOutputStream();
109 assertEquals(out
.getClass(), servletOutput
.getClass());
113 public void testSendError() throws IOException
{
114 wrapper
.sendError(404);
115 verify(response
).sendError(404);
117 wrapper
.sendError(404, "error message");
118 verify(response
).sendError(404, "error message");