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.request;
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.Tapestry;
023    import org.apache.tapestry.services.ServletRequestServicer;
024    import org.apache.tapestry.spec.ILibrarySpecification;
025    import org.testng.annotations.Test;
026    
027    import javax.servlet.ServletException;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    /**
032     * Tests for {@link org.apache.tapestry.request.DecodedRequestInjector}.
033     * 
034     * @author Howard M. Lewis Ship
035     * @since 4.0
036     */
037    @Test
038    public class TestDecodedRequestInjector extends BaseComponentTestCase
039    {
040        private static class ServicerFixture implements ServletRequestServicer
041        {
042            HttpServletRequest _request;
043    
044            public void service(HttpServletRequest request, HttpServletResponse response)
045                    throws IOException, ServletException
046            {
047                _request = request;
048            }
049        }
050    
051        private HttpServletRequest newHttpRequest()
052        {
053            return newMock(HttpServletRequest.class);
054        }
055    
056        private HttpServletResponse newResponse()
057        {
058            return newMock(HttpServletResponse.class);
059        }
060    
061        private ILibrarySpecification newSpec(boolean exists, IRequestDecoder decoder)
062        {
063            ILibrarySpecification spec = newMock(ILibrarySpecification.class);
064    
065            expect(spec.checkExtension(Tapestry.REQUEST_DECODER_EXTENSION_NAME)).andReturn(exists);
066    
067            if (exists)
068            {
069                expect(spec.getExtension(Tapestry.REQUEST_DECODER_EXTENSION_NAME, IRequestDecoder.class))
070                .andReturn(decoder);
071            }
072    
073            return spec;
074        }
075    
076        public void testNoExtension() throws Exception
077        {
078            HttpServletRequest request = newHttpRequest();
079            HttpServletResponse response = newResponse();
080            ILibrarySpecification spec = newSpec(false, null);
081    
082            ServletRequestServicer servicer = newMock(ServletRequestServicer.class);
083    
084            servicer.service(request, response);
085    
086            replay();
087    
088            DecodedRequestInjector dri = new DecodedRequestInjector();
089    
090            dri.setApplicationSpecification(spec);
091            dri.initializeService();
092    
093            dri.service(request, response, servicer);
094    
095            verify();
096        }
097    
098        public void testWithExtension() throws Exception
099        {
100            HttpServletRequest request = newHttpRequest();
101            HttpServletResponse response = newResponse();
102            
103            IRequestDecoder decoder = newMock(IRequestDecoder.class);
104            ILibrarySpecification spec = newSpec(true, decoder);
105    
106            ServicerFixture servicer = new ServicerFixture();
107    
108            DecodedRequest decoded = new DecodedRequest();
109            decoded.setRequestURI("/foo/bar/baz");
110    
111            expect(decoder.decodeRequest(request)).andReturn(decoded);
112    
113            replay();
114    
115            DecodedRequestInjector dri = new DecodedRequestInjector();
116    
117            dri.setApplicationSpecification(spec);
118            dri.initializeService();
119    
120            dri.service(request, response, servicer);
121    
122            // Prove that the request passed down the pipeline is a wrapper
123    
124            assertEquals("/foo/bar/baz", servicer._request.getRequestURI());
125    
126            verify();
127        }
128    }