Search in sources :

Example 1 with TestableResponse

use of org.apache.tapestry5.internal.test.TestableResponse in project tapestry-5 by apache.

the class PageTesterModule method setupTestableOverrides.

@Contribute(ServiceOverride.class)
public static void setupTestableOverrides(MappedConfiguration<Class, Object> configuration, @Local TestableRequest request, @Local TestableResponse response, final ObjectLocator locator) {
    configuration.add(Request.class, request);
    configuration.add(Response.class, response);
    TestableCookieSinkSource cookies = new TestableCookieSinkSource();
    configuration.add(CookieSink.class, cookies);
    configuration.add(CookieSource.class, cookies);
    // With the significant changes to the handling of assets in 5.4, we introduced a problem:
    // We were checking at page render time whether to generate URLs for normal or compressed
    // assets and that peeked at the HttpServletRequest global, which isn't set up by PageTester.
    // What we're doing here is using a hacked version of that code to force GZip support
    // on.
    configuration.add(ResponseCompressionAnalyzer.class, new ResponseCompressionAnalyzer() {

        public boolean isGZipEnabled(ContentType contentType) {
            return locator.getObject(CompressionAnalyzer.class, null).isCompressable(contentType.getMimeType());
        }

        public boolean isGZipSupported() {
            return true;
        }
    });
}
Also used : ContentType(org.apache.tapestry5.http.ContentType) ResponseCompressionAnalyzer(org.apache.tapestry5.http.services.ResponseCompressionAnalyzer) Contribute(org.apache.tapestry5.ioc.annotations.Contribute)

Example 2 with TestableResponse

use of org.apache.tapestry5.internal.test.TestableResponse in project tapestry-5 by apache.

the class TestableResponseImplTest method http_headers.

@Test
public void http_headers() {
    Document document = tester.renderPage(TestPageForHttpHeaders.class.getSimpleName());
    assertTrue(document.toString().contains("Test page for HTTP headers"));
    TestableResponse response = tester.getService(TestableResponse.class);
    assertEquals(response.getHeader(TestPageForHttpHeaders.DATE_HEADER_NAME), 12345L);
    assertEquals(response.getHeader(TestPageForHttpHeaders.INT_HEADER_NAME), 6789);
    assertEquals(response.getHeader(TestPageForHttpHeaders.STRING_HEADER_NAME), "foo-bar-baz-barney");
}
Also used : TestPageForHttpHeaders(org.apache.tapestry5.integration.app2.pages.TestPageForHttpHeaders) Document(org.apache.tapestry5.dom.Document) Test(org.testng.annotations.Test)

Example 3 with TestableResponse

use of org.apache.tapestry5.internal.test.TestableResponse in project tapestry-5 by apache.

the class PageTester method runComponentEventRequest.

private TestableResponse runComponentEventRequest() {
    while (true) {
        response.clear();
        try {
            boolean handled = requestHandler.service(request, response);
            if (!handled)
                throw new RuntimeException(String.format("Request for path '%s' was not handled by Tapestry.", request.getPath()));
            Link link = response.getRedirectLink();
            if (link != null) {
                setupRequestFromLink(link);
                continue;
            }
            return response;
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            registry.cleanupThread();
        }
    }
}
Also used : IOException(java.io.IOException) Link(org.apache.tapestry5.http.Link)

Example 4 with TestableResponse

use of org.apache.tapestry5.internal.test.TestableResponse in project tapestry-5 by apache.

the class PageTester method renderPageAndReturnResponse.

/**
 * Renders a page specified by its name and returns the response.
 *
 * @param pageName
 *         The name of the page to be rendered.
 * @return The response object to assert against
 * @since 5.2.3
 */
public TestableResponse renderPageAndReturnResponse(String pageName) {
    request.clear().setPath("/" + pageName);
    while (true) {
        try {
            response.clear();
            boolean handled = requestHandler.service(request, response);
            if (!handled) {
                throw new RuntimeException(String.format("Request was not handled: '%s' may not be a valid page name.", pageName));
            }
            Link link = response.getRedirectLink();
            if (link != null) {
                setupRequestFromLink(link);
                continue;
            }
            return response;
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            registry.cleanupThread();
        }
    }
}
Also used : IOException(java.io.IOException) Link(org.apache.tapestry5.http.Link)

Example 5 with TestableResponse

use of org.apache.tapestry5.internal.test.TestableResponse in project tapestry-5 by apache.

the class PageTester method clickSubmitAndReturnResponse.

/**
 * Simulates a submission of the form by clicking the specified submit button. The caller can
 * specify values for the
 * form fields.
 *
 * @param submitButton
 *         the submit button to be clicked.
 * @param fieldValues
 *         the field values keyed on field names.
 * @return The response object to assert against.
 * @since 5.2.3
 */
public TestableResponse clickSubmitAndReturnResponse(Element submitButton, Map<String, String> fieldValues) {
    assert submitButton != null;
    assertIsSubmit(submitButton);
    Element form = getFormAncestor(submitButton);
    request.clear().setPath(stripContextFromPath(extractNonBlank(form, "action")));
    pushFieldValuesIntoRequest(form);
    overrideParameters(fieldValues);
    String value = submitButton.getAttribute("value");
    if (value == null)
        value = DEFAULT_SUBMIT_VALUE_ATTRIBUTE;
    request.overrideParameter(extractNonBlank(submitButton, "name"), value);
    return runComponentEventRequest();
}
Also used : Element(org.apache.tapestry5.dom.Element)

Aggregations

IOException (java.io.IOException)2 Document (org.apache.tapestry5.dom.Document)2 Element (org.apache.tapestry5.dom.Element)2 Link (org.apache.tapestry5.http.Link)2 Test (org.testng.annotations.Test)2 List (java.util.List)1 ContentType (org.apache.tapestry5.http.ContentType)1 ResponseCompressionAnalyzer (org.apache.tapestry5.http.services.ResponseCompressionAnalyzer)1 TestPageForActionLinkWithStream (org.apache.tapestry5.integration.app2.pages.TestPageForActionLinkWithStream)1 TestPageForHttpHeaders (org.apache.tapestry5.integration.app2.pages.TestPageForHttpHeaders)1 TestableResponse (org.apache.tapestry5.internal.test.TestableResponse)1 Contribute (org.apache.tapestry5.ioc.annotations.Contribute)1 PageTester (org.apache.tapestry5.test.PageTester)1 Test (org.junit.Test)1