aboutsummaryrefslogtreecommitdiffstats
path: root/tests/server-side/com/vaadin/server/TestAbstractApplicationServletStaticFilesLocation.java
blob: df16e98bba448057466c93c19f8cf0cd41c12627 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.vaadin.server;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.vaadin.server.AbstractApplicationServlet;
import com.vaadin.server.ApplicationServlet;

import junit.framework.TestCase;

public class TestAbstractApplicationServletStaticFilesLocation extends TestCase {

    ApplicationServlet servlet;

    private Method getStaticFilesLocationMethod;

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        servlet = new ApplicationServlet();

        // Workaround to avoid calling init and creating servlet config
        Field f = AbstractApplicationServlet.class
                .getDeclaredField("applicationProperties");
        f.setAccessible(true);
        f.set(servlet, new Properties());

        getStaticFilesLocationMethod = AbstractApplicationServlet.class
                .getDeclaredMethod(
                        "getStaticFilesLocation",
                        new Class[] { javax.servlet.http.HttpServletRequest.class });
        getStaticFilesLocationMethod.setAccessible(true);

    }

    public class DummyServletConfig implements ServletConfig {

        // public DummyServletConfig(Map<String,String> initParameters, )
        @Override
        public String getInitParameter(String name) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Enumeration<Object> getInitParameterNames() {
            return new Enumeration<Object>() {

                @Override
                public boolean hasMoreElements() {
                    return false;
                }

                @Override
                public Object nextElement() {
                    return null;
                }
            };
        }

        @Override
        public ServletContext getServletContext() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getServletName() {
            // TODO Auto-generated method stub
            return null;
        }

    }

    public void testWidgetSetLocation() throws Exception {
        String location;

        /* SERVLETS */
        // http://dummy.host:8080/contextpath/servlet
        // should return /contextpath
        location = testLocation("http://dummy.host:8080", "/contextpath",
                "/servlet", "");
        assertEquals("/contextpath", location);

        // http://dummy.host:8080/servlet
        // should return ""
        location = testLocation("http://dummy.host:8080", "", "/servlet", "");
        assertEquals("", location);

        // http://dummy.host/contextpath/servlet/extra/stuff
        // should return /contextpath
        location = testLocation("http://dummy.host", "/contextpath",
                "/servlet", "/extra/stuff");
        assertEquals("/contextpath", location);

        // http://dummy.host/context/path/servlet/extra/stuff
        // should return /context/path
        location = testLocation("http://dummy.host", "/context/path",
                "/servlet", "/extra/stuff");
        assertEquals("/context/path", location);

        /* Include requests */
        location = testIncludedLocation("http://my.portlet.server", "/user",
                "/tmpservletlocation1", "");
        assertEquals("Wrong widgetset location", "/user", location);

    }

    private String testLocation(String base, String contextPath,
            String servletPath, String pathInfo) throws Exception {

        HttpServletRequest request = createNonIncludeRequest(base, contextPath,
                servletPath, pathInfo);
        // Set request into replay mode
        replay(request);

        String location = (String) getStaticFilesLocationMethod.invoke(servlet,
                request);
        return location;
    }

    private String testIncludedLocation(String base, String portletContextPath,
            String servletPath, String pathInfo) throws Exception {

        HttpServletRequest request = createIncludeRequest(base,
                portletContextPath, servletPath, pathInfo);
        // Set request into replay mode
        replay(request);

        String location = (String) getStaticFilesLocationMethod.invoke(servlet,
                request);
        return location;
    }

    private HttpServletRequest createIncludeRequest(String base,
            String realContextPath, String realServletPath, String pathInfo)
            throws Exception {
        HttpServletRequest request = createRequest(base, "", "", pathInfo);
        expect(request.getAttribute("javax.servlet.include.context_path"))
                .andReturn(realContextPath).anyTimes();
        expect(request.getAttribute("javax.servlet.include.servlet_path"))
                .andReturn(realServletPath).anyTimes();

        return request;
    }

    private HttpServletRequest createNonIncludeRequest(String base,
            String realContextPath, String realServletPath, String pathInfo)
            throws Exception {
        HttpServletRequest request = createRequest(base, realContextPath,
                realServletPath, pathInfo);
        expect(request.getAttribute("javax.servlet.include.context_path"))
                .andReturn(null).anyTimes();
        expect(request.getAttribute("javax.servlet.include.servlet_path"))
                .andReturn(null).anyTimes();

        return request;
    }

    /**
     * Creates a HttpServletRequest mock using the supplied parameters.
     * 
     * @param base
     *            The base url, e.g. http://localhost:8080
     * @param contextPath
     *            The context path where the application is deployed, e.g.
     *            /mycontext
     * @param servletPath
     *            The servlet path to the servlet we are testing, e.g. /myapp
     * @param pathInfo
     *            Any text following the servlet path in the request, not
     *            including query parameters, e.g. /UIDL/
     * @return A mock HttpServletRequest object useful for testing
     * @throws MalformedURLException
     */
    private HttpServletRequest createRequest(String base, String contextPath,
            String servletPath, String pathInfo) throws MalformedURLException {
        URL url = new URL(base + contextPath + pathInfo);
        HttpServletRequest request = createMock(HttpServletRequest.class);
        expect(request.isSecure()).andReturn(
                url.getProtocol().equalsIgnoreCase("https")).anyTimes();
        expect(request.getServerName()).andReturn(url.getHost()).anyTimes();
        expect(request.getServerPort()).andReturn(url.getPort()).anyTimes();
        expect(request.getRequestURI()).andReturn(url.getPath()).anyTimes();
        expect(request.getContextPath()).andReturn(contextPath).anyTimes();
        expect(request.getPathInfo()).andReturn(pathInfo).anyTimes();
        expect(request.getServletPath()).andReturn(servletPath).anyTimes();

        return request;
    }

}