001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.rest; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNotNull; 022import static org.mockito.Mockito.mock; 023import static org.mockito.Mockito.never; 024import static org.mockito.Mockito.verify; 025import static org.mockito.Mockito.when; 026 027import java.io.IOException; 028import javax.servlet.ServletOutputStream; 029import javax.servlet.http.HttpServletResponse; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.rest.filter.GZIPResponseStream; 032import org.apache.hadoop.hbase.rest.filter.GZIPResponseWrapper; 033import org.apache.hadoop.hbase.testclassification.RestTests; 034import org.apache.hadoop.hbase.testclassification.SmallTests; 035import org.junit.ClassRule; 036import org.junit.Test; 037import org.junit.experimental.categories.Category; 038 039@Category({ RestTests.class, SmallTests.class }) 040public class TestGZIPResponseWrapper { 041 042 @ClassRule 043 public static final HBaseClassTestRule CLASS_RULE = 044 HBaseClassTestRule.forClass(TestGZIPResponseWrapper.class); 045 046 private final HttpServletResponse response = mock(HttpServletResponse.class); 047 private final GZIPResponseWrapper wrapper = new GZIPResponseWrapper(response); 048 049 /** 050 * wrapper should set all headers except "content-length" 051 */ 052 @Test 053 public void testHeader() throws IOException { 054 wrapper.setStatus(200); 055 verify(response).setStatus(200); 056 wrapper.addHeader("header", "header value"); 057 verify(response).addHeader("header", "header value"); 058 wrapper.addHeader("content-length", "header value2"); 059 verify(response, never()).addHeader("content-length", "header value"); 060 061 wrapper.setIntHeader("header", 5); 062 verify(response).setIntHeader("header", 5); 063 wrapper.setIntHeader("content-length", 4); 064 verify(response, never()).setIntHeader("content-length", 4); 065 066 wrapper.setHeader("set-header", "new value"); 067 verify(response).setHeader("set-header", "new value"); 068 wrapper.setHeader("content-length", "content length value"); 069 verify(response, never()).setHeader("content-length", "content length value"); 070 071 wrapper.sendRedirect("location"); 072 verify(response).sendRedirect("location"); 073 074 wrapper.flushBuffer(); 075 verify(response).flushBuffer(); 076 } 077 078 @Test 079 public void testResetBuffer() throws IOException { 080 when(response.isCommitted()).thenReturn(false); 081 ServletOutputStream out = mock(ServletOutputStream.class); 082 when(response.getOutputStream()).thenReturn(out); 083 084 ServletOutputStream servletOutput = wrapper.getOutputStream(); 085 assertEquals(GZIPResponseStream.class, servletOutput.getClass()); 086 wrapper.resetBuffer(); 087 verify(response).setHeader("Content-Encoding", null); 088 089 when(response.isCommitted()).thenReturn(true); 090 servletOutput = wrapper.getOutputStream(); 091 assertEquals(out.getClass(), servletOutput.getClass()); 092 assertNotNull(wrapper.getWriter()); 093 } 094 095 @Test 096 public void testReset() throws IOException { 097 when(response.isCommitted()).thenReturn(false); 098 ServletOutputStream out = mock(ServletOutputStream.class); 099 when(response.getOutputStream()).thenReturn(out); 100 101 ServletOutputStream servletOutput = wrapper.getOutputStream(); 102 verify(response).addHeader("Content-Encoding", "gzip"); 103 assertEquals(GZIPResponseStream.class, servletOutput.getClass()); 104 wrapper.reset(); 105 verify(response).setHeader("Content-Encoding", null); 106 107 when(response.isCommitted()).thenReturn(true); 108 servletOutput = wrapper.getOutputStream(); 109 assertEquals(out.getClass(), servletOutput.getClass()); 110 } 111 112 @Test 113 public void testSendError() throws IOException { 114 wrapper.sendError(404); 115 verify(response).sendError(404); 116 117 wrapper.sendError(404, "error message"); 118 verify(response).sendError(404, "error message"); 119 } 120 121}