001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.multipart;
016    
017    import static org.easymock.EasyMock.expect;
018    
019    import java.io.IOException;
020    
021    import org.apache.tapestry.BaseComponentTestCase;
022    import org.apache.tapestry.services.ServletRequestServicer;
023    import org.testng.annotations.Test;
024    
025    import javax.servlet.ServletException;
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * Tests for {@link org.apache.tapestry.multipart.MultipartDecoderFilter}.
031     * 
032     * @author Howard M. Lewis Ship
033     * @since 4.0
034     */
035    @Test
036    public class TestMultipartDecoderFilter extends BaseComponentTestCase
037    {
038        private static class MockServicer implements ServletRequestServicer
039        {
040            HttpServletRequest _request;
041    
042            public void service(HttpServletRequest request, HttpServletResponse response)
043                    throws IOException, ServletException
044            {
045                _request = request;
046            }
047        }
048    
049        private HttpServletRequest newRequest(String contentType)
050        {
051            HttpServletRequest request = newMock(HttpServletRequest.class);
052    
053            expect(request.getContentType()).andReturn(contentType);
054    
055            return request;
056        }
057    
058        private HttpServletResponse newResponse()
059        {
060            return newMock(HttpServletResponse.class);
061        }
062    
063        public void testNormalRequest() throws Exception
064        {
065            HttpServletRequest request = newRequest("application/x-www-form-urlencoded");
066            HttpServletResponse response = newResponse();
067    
068            MockServicer servicer = new MockServicer();
069    
070            replay();
071    
072            MultipartDecoderFilter f = new MultipartDecoderFilter();
073    
074            f.service(request, response, servicer);
075    
076            assertSame(request, servicer._request);
077    
078            verify();
079        }
080    
081        public void testUploadRequest() throws Exception
082        {
083            HttpServletRequest request = newRequest("multipart/form-data");
084            HttpServletResponse response = newResponse();
085            HttpServletRequest decoded = newMock(HttpServletRequest.class);
086            
087            ServletMultipartDecoder decoder = newMock(ServletMultipartDecoder.class);
088    
089            expect(decoder.decode(request)).andReturn(decoded);
090    
091            decoder.cleanup();
092    
093            MockServicer servicer = new MockServicer();
094    
095            replay();
096    
097            MultipartDecoderFilter f = new MultipartDecoderFilter();
098            f.setDecoder(decoder);
099    
100            f.service(request, response, servicer);
101    
102            assertSame(decoded, servicer._request);
103    
104            verify();
105        }
106    }