1 /** 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 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 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. 17 */ 18 19 package org.apache.hadoop.fs.http.server; 20 21 import javax.servlet.Filter; 22 import javax.servlet.FilterChain; 23 import javax.servlet.http.HttpServletRequest; 24 import javax.servlet.http.HttpServletResponse; 25 26 import org.apache.hadoop.fs.http.client.HttpFSFileSystem; 27 import org.junit.Test; 28 import org.mockito.Mockito; 29 30 public class TestCheckUploadContentTypeFilter { 31 32 @Test putUpload()33 public void putUpload() throws Exception { 34 test("PUT", HttpFSFileSystem.Operation.CREATE.toString(), "application/octet-stream", true, false); 35 } 36 37 @Test postUpload()38 public void postUpload() throws Exception { 39 test("POST", HttpFSFileSystem.Operation.APPEND.toString(), "APPLICATION/OCTET-STREAM", true, false); 40 } 41 42 @Test putUploadWrong()43 public void putUploadWrong() throws Exception { 44 test("PUT", HttpFSFileSystem.Operation.CREATE.toString(), "plain/text", false, false); 45 test("PUT", HttpFSFileSystem.Operation.CREATE.toString(), "plain/text", true, true); 46 } 47 48 @Test postUploadWrong()49 public void postUploadWrong() throws Exception { 50 test("POST", HttpFSFileSystem.Operation.APPEND.toString(), "plain/text", false, false); 51 test("POST", HttpFSFileSystem.Operation.APPEND.toString(), "plain/text", true, true); 52 } 53 54 @Test getOther()55 public void getOther() throws Exception { 56 test("GET", HttpFSFileSystem.Operation.GETHOMEDIRECTORY.toString(), "plain/text", false, false); 57 } 58 59 @Test putOther()60 public void putOther() throws Exception { 61 test("PUT", HttpFSFileSystem.Operation.MKDIRS.toString(), "plain/text", false, false); 62 } 63 test(String method, String operation, String contentType, boolean upload, boolean error)64 private void test(String method, String operation, String contentType, 65 boolean upload, boolean error) throws Exception { 66 HttpServletRequest request = Mockito.mock(HttpServletRequest.class); 67 HttpServletResponse response = Mockito.mock(HttpServletResponse.class); 68 Mockito.reset(request); 69 Mockito.when(request.getMethod()).thenReturn(method); 70 Mockito.when(request.getParameter(HttpFSFileSystem.OP_PARAM)).thenReturn(operation); 71 Mockito.when(request.getParameter(HttpFSParametersProvider.DataParam.NAME)). 72 thenReturn(Boolean.toString(upload)); 73 Mockito.when(request.getContentType()).thenReturn(contentType); 74 75 FilterChain chain = Mockito.mock(FilterChain.class); 76 77 Filter filter = new CheckUploadContentTypeFilter(); 78 79 filter.doFilter(request, response, chain); 80 81 if (error) { 82 Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_BAD_REQUEST), 83 Mockito.contains("Data upload")); 84 } 85 else { 86 Mockito.verify(chain).doFilter(request, response); 87 } 88 } 89 90 91 } 92