001// Copyright 2007, 2008, 2009, 2010, 2011, 2012 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
015package org.apache.tapestry5.internal.test;
016
017import org.apache.tapestry5.commons.util.CollectionFactory;
018import org.apache.tapestry5.http.TapestryHttpSymbolConstants;
019import org.apache.tapestry5.http.services.Session;
020import org.apache.tapestry5.ioc.annotations.Inject;
021import org.apache.tapestry5.ioc.annotations.Symbol;
022import org.apache.tapestry5.ioc.internal.util.InternalUtils;
023
024import java.util.*;
025
026public class TestableRequestImpl implements TestableRequest
027{
028    private final String contextPath;
029
030    private final Map<String, Object> parameters = CollectionFactory.newMap();
031
032    private final Map<String, Object> attributes = CollectionFactory.newMap();
033
034    private Session session;
035
036    private String path = "/";
037
038    private Locale locale = Locale.getDefault();
039
040    @Inject
041    public TestableRequestImpl(@Symbol(TapestryHttpSymbolConstants.CONTEXT_PATH) String contextPath)
042    {
043        this.contextPath = contextPath;
044    }
045
046    public TestableRequest clear()
047    {
048        parameters.clear();
049
050        return this;
051    }
052
053    public TestableRequest setPath(String path)
054    {
055        this.path = path;
056
057        return this;
058    }
059
060    public TestableRequest setLocale(Locale locale)
061    {
062        this.locale = locale;
063
064        return this;
065    }
066
067    public TestableRequest loadParameter(String parameterName, String parameterValue)
068    {
069        Object existing = parameters.get(parameterName);
070
071        if (existing == null)
072        {
073            parameters.put(parameterName, parameterValue);
074            return this;
075        }
076
077        if (existing instanceof List)
078        {
079            ((List) existing).add(parameterValue);
080            return this;
081        }
082
083        // Convert from a single String to a List of Strings.
084
085        List list = new ArrayList();
086        list.add(existing);
087        list.add(parameterValue);
088
089        parameters.put(parameterName, list);
090
091        return this;
092    }
093
094    public TestableRequest overrideParameter(String parameterName, String parameterValue)
095    {
096        parameters.put(parameterName, parameterValue);
097
098        return this;
099    }
100
101    public long getDateHeader(String name)
102    {
103        return 0;
104    }
105
106    /**
107     * Returns null.
108     */
109    public String getHeader(String name)
110    {
111        return null;
112    }
113
114    /**
115     * Returns an empty list.
116     */
117    public List<String> getHeaderNames()
118    {
119        return Collections.emptyList();
120    }
121
122    public Locale getLocale()
123    {
124        return locale;
125    }
126
127    public List<String> getParameterNames()
128    {
129        return InternalUtils.sortedKeys(parameters);
130    }
131
132    public String[] getParameters(String name)
133    {
134        Object value = parameters.get(name);
135
136        if (value == null)
137            return null;
138
139        if (value instanceof String)
140            return new String[]
141            { (String) value };
142
143        List list = (List) value;
144
145        return (String[]) list.toArray(new String[list.size()]);
146    }
147
148    public String getPath()
149    {
150        return path;
151    }
152
153    public String getContextPath()
154    {
155        return contextPath;
156    }
157
158    public String getParameter(String name)
159    {
160        Object value = parameters.get(name);
161
162        if (value == null || value instanceof String)
163            return (String) value;
164
165        List<String> list = (List<String>) value;
166
167        return list.get(0);
168    }
169
170    public Session getSession(boolean create)
171    {
172        if (!create)
173            return session;
174
175        if (session == null)
176            session = new PageTesterSession();
177
178        return session;
179    }
180
181    public void setEncoding(String requestEncoding)
182    {
183    }
184
185    /**
186     * Always returns false. If you need to test Ajax functionality, you need to be using Selenium.
187     */
188    public boolean isXHR()
189    {
190        return false;
191    }
192
193    public boolean isSecure()
194    {
195        return false;
196    }
197
198    /**
199     * Always returns true.
200     */
201    public boolean isRequestedSessionIdValid()
202    {
203        return true;
204    }
205
206    public Object getAttribute(String name)
207    {
208        return attributes.get(name);
209    }
210
211    public List<String> getAttributeNames()
212    {
213        return InternalUtils.sortedKeys(attributes);
214    }
215
216    public void setAttribute(String name, Object value)
217    {
218        attributes.put(name, value);
219    }
220
221    /**
222     * Returns "localhost" which is sufficient for testing purposes.
223     */
224    public String getServerName()
225    {
226        return "localhost";
227    }
228
229    /**
230     * Always returns POST, to keep the Form component happy.
231     */
232    public String getMethod()
233    {
234        return "POST";
235    }
236
237    /**
238     * Always returns 80.
239     */
240    public int getLocalPort()
241    {
242        return 80;
243    }
244
245    /**
246     * Always returns 80.
247     * 
248     * @since 5.2.5
249     */
250    public int getServerPort()
251    {
252        return 80;
253    }
254
255    /**
256     * Always returns "localhost".
257     *
258     * @since 5.3
259     */
260    public String getRemoteHost()
261    {
262        return "localhost";
263    }
264
265    public boolean isSessionInvalidated()
266    {
267        return false;
268    }
269}