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