/** * @param name * @param date */ public void addDateHeader(String name, long date) { DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); String dateString = df.format(new Date(date)); addHeader(name, dateString); }
/** * Return the length of the content. This is always -1 except if useMultiPartContentType set as * true. Then the length will be the length of the generated request. * * @return -1 if useMultiPartContentType is false. Else the length of the generated request. */ public int getContentLength() { if (useMultiPartContentType) { byte[] request = buildRequest(); return request.length; } return -1; }
/** * Returns context path and servlet path concatenated, typically * /applicationClassName/applicationClassName * * @return The path value * @see javax.servlet.http.HttpServletRequest#getRequestURI() */ public String getRequestURI() { if (url == null) { return getContextPath() + getServletPath(); } return url; }
/** * Try to build a rough URL. * * @return The url */ public StringBuffer getRequestURL() { final StringBuffer buf = new StringBuffer(); buf.append("http://localhost"); buf.append(getContextPath()); if (getPathInfo() != null) { buf.append(getPathInfo()); } final String query = getQueryString(); if (query != null) { buf.append('?'); buf.append(query); } return buf; }
/** * Return all the accepted locales. This implementation always returns just one. * * @return The locales */ public Enumeration<Locale> getLocales() { List<Locale> list = new ArrayList<Locale>(); final String header = getHeader("Accept-Language"); if (header != null) { final String[] locales = header.split(","); for (String value : locales) { Locale locale = getLocale(value); if (locale != null) { list.add(locale); } } } if (list.size() == 0) { list.add(Locale.getDefault()); } return Collections.enumeration(list); }
servletRequest.initialize(); servletResponse.initialize(); servletRequest.addCookies(cookiesOfThisSession); servletRequest.setParameters(parametersForNextRequest); if (isAjax) servletRequest.addHeader("Wicket-Ajax", "Yes");
String redirectLocation = httpResponse.getRedirectLocation(); cycle = setupRequestAndResponse(false); servletRequest.setRequestToRedirectString(redirectLocation); cycle.request(); if (url != null) MockHttpServletRequest newHttpRequest = new MockHttpServletRequest(application, servletSession, application.getServletContext()); newHttpRequest.setRequestToRedirectString(url); wicketRequest = application.newWebRequest(newHttpRequest);
/** * Reset the request and the response back to a starting state and recreate the necessary wicket * request, response and session objects. The request and response objects can be accessed and * Initialized at this point. * * @param isAjax * indicates whether the request should be initialized as an ajax request (ajax * header "Wicket-Ajax" is set) */ public WebRequestCycle setupRequestAndResponse(boolean isAjax) { servletRequest.initialize(); servletResponse.initialize(); servletRequest.setParameters(parametersForNextRequest); if (isAjax) { servletRequest.addHeader("Wicket-Ajax", "Yes"); } parametersForNextRequest.clear(); wicketRequest = application.newWebRequest(servletRequest); wicketResponse = application.newWebResponse(servletResponse); WebRequestCycle requestCycle = createRequestCycle(); if (!initializeHttpSessionAsTemporary()) application.getSessionStore().bind(wicketRequest, wicketSession); wicketResponse.setAjax(wicketRequest.isAjax()); return requestCycle; }
url = getContextPath() + getServletPath() + "/" + url; if (url.startsWith(getContextPath())) url = url.substring(getContextPath().length()); if (url.startsWith(getServletPath())) url = url.substring(getServletPath().length()); if (index == -1) setPath(url); setPath(url.substring(0, index));
/** * Adds an additional <code>FormComponent</code>'s value into request parameter -- this method * retains existing parameters but removes any duplicate parameters. * * @param formComponent * a <code>FormComponent</code> * @param value * a value to add */ private void addFormComponentValue(FormComponent<?> formComponent, String value) { if (parameterExist(formComponent)) { String[] values = baseWicketTester.getServletRequest().getParameterValues( formComponent.getInputName()); // remove duplicated HashSet<String> all = new HashSet<String>(Arrays.asList(values)); all.add(value); Map<String, String[]> newParameters = new HashMap<String, String[]>(); newParameters.put(formComponent.getInputName(), all.toArray(new String[all.size()])); baseWicketTester.getServletRequest().setParameters(newParameters); } else { setFormComponentValue(formComponent, value); } }
/** * Get the name of the remote user from the REMOTE_USER header. * * @return The name of the remote user */ public String getRemoteUser() { return getHeader("REMOTE_USER"); }
/** * Builds and processes a request suitable for executing an <code>AbstractAjaxBehavior</code>. * * @param behavior * an <code>AbstractAjaxBehavior</code> to execute */ public void executeBehavior(final AbstractAjaxBehavior behavior) { CharSequence url = behavior.getCallbackUrl(false); WebRequestCycle cycle = setupRequestAndResponse(true); getServletRequest().setRequestToRedirectString(url.toString()); processRequestCycle(cycle); }
/** * The servlet path may either be the application name or /. For test purposes we always return * the servlet name. * * @return The servlet path */ public String getServletPath() { return getContextPath(); }
/** * Returns <code>true</code> if the parameter exists in the <code>FormComponent</code>. * * @param formComponent * a <code>FormComponent</code> * @return <code>true</code> if the parameter exists in the <code>FormComponent</code> */ private boolean parameterExist(FormComponent<?> formComponent) { String parameter = baseWicketTester.getServletRequest().getParameter( formComponent.getInputName()); return parameter != null && parameter.trim().length() > 0; }
/** * Sets the <code>File</code> on a {@link FileUploadField}. * * @param formComponentId * relative path (from <code>Form</code>) to the selectable * <code>FormComponent</code>. The <code>FormComponent</code> must be of a type * <code>FileUploadField</code>. * @param file * the <code>File</code> to upload. * @param contentType * the content type of the file. Must be a valid mime type. */ public void setFile(final String formComponentId, final File file, final String contentType) { checkClosed(); FormComponent formComponent = (FormComponent)workingForm.get(formComponentId); if (formComponent instanceof FileUploadField == false) { throw new IllegalArgumentException("'" + formComponentId + "' is not " + "a FileUploadField. You can only attach a file to form " + "component of this type."); } MockHttpServletRequest servletRequest = baseWicketTester.getServletRequest(); servletRequest.addFile(formComponent.getInputName(), file, contentType); }
servletRequest = new MockHttpServletRequest(this.application, servletSession, context); servletResponse = new MockHttpServletResponse(servletRequest)
/** * Return all the accepted locales. This implementation always returns just one. * * @return The locales */ public Enumeration getLocales() { List list = new ArrayList(1); list.add(getLocale()); return Collections.enumeration(list); }
/** * Try to build a rough URL. * * @return The url */ public StringBuffer getRequestURL() { final StringBuffer buf = new StringBuffer(); buf.append("http://localhost"); buf.append(getContextPath()); if (getPathInfo() != null) { buf.append(getPathInfo()); } final String query = getQueryString(); if (query != null) { buf.append('?'); buf.append(query); } return buf; }
MockHttpServletRequest newHttpRequest = new MockHttpServletRequest(application, servletSession, application.getServletContext()); newHttpRequest.setRequestToRedirectString(httpResponse.getRedirectLocation()); wicketRequest = application.newWebRequest(newHttpRequest);
/** * Adds an additional <code>FormComponent</code>'s value into request parameter -- this * method retains existing parameters but removes any duplicate parameters. * * @param formComponent * a <code>FormComponent</code> * @param value * a value to add */ private void addFormComponentValue(FormComponent formComponent, String value) { if (parameterExist(formComponent)) { String[] values = baseWicketTester.getServletRequest().getParameterValues( formComponent.getInputName()); // remove duplicated HashSet all = new HashSet(Arrays.asList(values)); all.add(value); Map newParameters = new HashMap(); newParameters.put(formComponent.getInputName(), all.toArray(new String[all.size()])); baseWicketTester.getServletRequest().setParameters(newParameters); } else { setFormComponentValue(formComponent, value); } }