Search in sources :

Example 1 with Form

use of com.adobe.acs.commons.forms.Form in project acs-aem-commons by Adobe-Consulting-Services.

the class PostRedirectGetFormHelperImplTest method shouldRedirectForRenderForm.

@Test
public void shouldRedirectForRenderForm() throws Exception {
    request.setParameterMap(ImmutableMap.<String, Object>of(":form", "x"));
    final SlingHttpServletResponse spiedResponse = spy(response);
    final Form form = formHelper.getForm("x", request, response);
    formHelper.renderForm(form, requestResource, request, spiedResponse);
    // check a redirect was requested
    verify(spiedResponse).sendRedirect(redirectCaptor.capture());
    PathInfo redirectPathInfo = new PathInfo(redirectCaptor.getValue());
    // check where we are being redirected to
    assertThat(redirectPathInfo.getExtension(), is(equalTo("html")));
    assertThat(redirectPathInfo.getResourcePath(), is(equalTo("/test")));
    assertThat(redirectPathInfo.getSuffix(), startsWith(SUFFIX));
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Form(com.adobe.acs.commons.forms.Form) PathInfo(com.day.cq.commons.PathInfo) Test(org.junit.Test)

Example 2 with Form

use of com.adobe.acs.commons.forms.Form in project acs-aem-commons by Adobe-Consulting-Services.

the class PostRedirectGetFormHelperImplTest method shouldMapSingleParam.

@Test
public void shouldMapSingleParam() throws Exception {
    request.setParameterMap(ImmutableMap.<String, Object>of(":form", "x", "hello", "world"));
    final Form form = formHelper.getForm("x", request, response);
    assertThat(form.get("hello"), is(equalTo("world")));
}
Also used : Form(com.adobe.acs.commons.forms.Form) Test(org.junit.Test)

Example 3 with Form

use of com.adobe.acs.commons.forms.Form in project acs-aem-commons by Adobe-Consulting-Services.

the class PostRedirectGetFormHelperImplTest method shouldMapFormName.

@Test
public void shouldMapFormName() throws Exception {
    request.setParameterMap(ImmutableMap.<String, Object>of(":form", "demo"));
    final Form form = formHelper.getForm("demo", request, response);
    assertThat(form.getName(), is(equalTo("demo")));
}
Also used : Form(com.adobe.acs.commons.forms.Form) Test(org.junit.Test)

Example 4 with Form

use of com.adobe.acs.commons.forms.Form in project acs-aem-commons by Adobe-Consulting-Services.

the class PostRedirectGetFormHelperImplTest method shouldMapFirstValueOfMultiParams.

@Test
public void shouldMapFirstValueOfMultiParams() throws Exception {
    request.setParameterMap(ImmutableMap.<String, Object>of(":form", "x", "hello", new String[] { "world", "mundo" }));
    final Form form = formHelper.getForm("x", request, response);
    assertThat(form.get("hello"), is(equalTo("world")));
}
Also used : Form(com.adobe.acs.commons.forms.Form) Test(org.junit.Test)

Example 5 with Form

use of com.adobe.acs.commons.forms.Form in project acs-aem-commons by Adobe-Consulting-Services.

the class PostRedirectGetFormHelperImplTest method shouldSerialiseFormInRedirectUrlParameter.

@Test
public void shouldSerialiseFormInRedirectUrlParameter() throws Exception {
    request.setParameterMap(ImmutableMap.<String, Object>of(":form", "x", "hello", "world"));
    final SlingHttpServletResponse spiedResponse = spy(response);
    final Form form = formHelper.getForm("x", request, response);
    form.setError("hello", "Error1");
    formHelper.renderForm(form, requestResource, request, spiedResponse);
    verify(spiedResponse).sendRedirect(redirectCaptor.capture());
    String redirectLocation = redirectCaptor.getValue();
    final String formString = decode(getFormParameter(redirectLocation));
    final JSONObject formObject = toJSONObject(formString);
    // form name
    assertThat(formObject.getString(PostRedirectGetFormHelperImpl.KEY_FORM_NAME), is(equalTo("x")));
    // errors
    final JSONObject formErrors = formObject.getJSONObject(PostRedirectGetFormHelperImpl.KEY_ERRORS);
    assertThat(formErrors.getString("hello"), is(equalTo("Error1")));
    // form data
    final JSONObject formData = formObject.getJSONObject(PostRedirectGetFormHelperImpl.KEY_FORM);
    assertThat(formData.getString("hello"), is(equalTo("world")));
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) JSONObject(org.apache.sling.commons.json.JSONObject) Form(com.adobe.acs.commons.forms.Form) Test(org.junit.Test)

Aggregations

Form (com.adobe.acs.commons.forms.Form)6 Test (org.junit.Test)6 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)3 MockSlingHttpServletResponse (org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse)3 JSONObject (org.apache.sling.commons.json.JSONObject)2 PathInfo (com.day.cq.commons.PathInfo)1 Cookie (javax.servlet.http.Cookie)1