Java Code Examples for org.takes.Take#act()

The following examples show how to use org.takes.Take#act() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TkAppTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void pingsSimplePages(@TempDir final Path temp) throws Exception {
    final String[] pages = {
        "/org/jpeek/web/layout.xsl",
        "/org/jpeek/web/index.xsl",
        "/jpeek.css",
        "/",
        "/mistakes",
        "/robots.txt",
    };
    final Take app = new TkApp(temp);
    for (final String page : pages) {
        final Response response = app.act(new RqFake("GET", page));
        new Assertion<>(
            new RsPrint(response).print(),
            response,
            new HmRsStatus(HttpURLConnection.HTTP_OK)
        ).affirm();
    }
}
 
Example 2
Source File: TkVerbose.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param take Original take
 */
public TkVerbose(final Take take) {
    super(
        new Take() {
            @Override
            public Response act(final Request request) throws Exception {
                try {
                    return take.act(request);
                } catch (final HttpException ex) {
                    throw new HttpException(
                        ex.code(),
                        String.format(
                            "%s %s",
                            new RqMethod.Base(request).method(),
                            new RqHref.Base(request).href()
                        ),
                        ex
                    );
                }
            }
        }
    );
}
 
Example 3
Source File: TkGzip.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param take Original take
 */
public TkGzip(final Take take) {
    super(
        new Take() {
            @Override
            public Response act(final Request req) throws Exception {
                final Response response = take.act(req);
                return new RsFork(
                    req,
                    new FkEncoding("gzip", new RsGzip(response)),
                    new FkEncoding("", response)
                );
            }
        }
    );
}
 
Example 4
Source File: TkMeasured.java    From takes with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param take Original take
 * @param header Header to add
 */
public TkMeasured(final Take take, final String header) {
    super(
        new Take() {
            @Override
            public Response act(final Request req) throws Exception {
                final long start = System.currentTimeMillis();
                final Response res = take.act(req);
                return new RsWithHeader(
                    res, header,
                    Long.toString(System.currentTimeMillis() - start)
                );
            }
        }
    );
}
 
Example 5
Source File: FkRegex.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param ptn Pattern
 * @param tke Take
 */
public FkRegex(final Pattern ptn, final Take tke) {
    this(
        ptn,
        new TkRegex() {
            @Override
            public Response act(final RqRegex req) throws Exception {
                return tke.act(req);
            }
        }
    );
}
 
Example 6
Source File: FkFixed.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param take Take
 */
public FkFixed(final Take take) {
    super(
        new Fork() {
            @Override
            public Opt<Response> route(final Request req) throws Exception {
                return new Opt.Single<>(take.act(req));
            }
        }
    );
}
 
Example 7
Source File: FbFixed.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param take Take to use
 * @since 0.14
 */
public FbFixed(final Take take) {
    super(
        new Fallback() {
            @Override
            public Opt<Response> route(final RqFallback req)
                throws Exception {
                return new Opt.Single<>(take.act(req));
            }
        }
    );
}
 
Example 8
Source File: FbStatus.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param code HTTP status code
 * @param take Take
 */
public FbStatus(final int code, final Take take) {
    this(
        code,
        new Fallback() {
            @Override
            public Opt<Response> route(final RqFallback req)
                throws Exception {
                return new Opt.Single<>(take.act(req));
            }
        }
    );
}
 
Example 9
Source File: TkWithType.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param take Original take
 * @param type Content type
 */
public TkWithType(final Take take, final String type) {
    super(
        new Take() {
            @Override
            public Response act(final Request req) throws Exception {
                return new RsWithType(take.act(req), type);
            }
        }
    );
}
 
Example 10
Source File: TkOnce.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param take Original take
 */
public TkOnce(final Take take) {
    super(
        new Take() {
            @Override
            public Response act(final Request request) throws Exception {
                return take.act(new RqGreedy(request));
            }
        }
    );
}
 
Example 11
Source File: TkWithHeader.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param take Original
 * @param header Header
 */
public TkWithHeader(final Take take, final String header) {
    super(
        new Take() {
            @Override
            public Response act(final Request req) throws Exception {
                return new RsWithHeader(take.act(req), header);
            }
        }
    );
}
 
Example 12
Source File: TkGreedy.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param take Original take
 */
public TkGreedy(final Take take) {
    super(
        new Take() {
            @Override
            public Response act(final Request request) throws Exception {
                return take.act(new RqGreedy(request));
            }
        }
    );
}
 
Example 13
Source File: TkVersioned.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param take Original take
 * @param header Header to add
 */
public TkVersioned(final Take take, final String header) {
    super(
        new Take() {
            @Override
            public Response act(final Request req) throws Exception {
                return new RsWithHeader(
                    take.act(req), header, TkVersioned.VERSION
                );
            }
        }
    );
}
 
Example 14
Source File: TkWithHeaders.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param take Original
 * @param headers Headers
 */
public TkWithHeaders(final Take take, final Collection<String> headers) {
    super(
        new Take() {
            @Override
            public Response act(final Request req) throws Exception {
                return new RsWithHeaders(take.act(req), headers);
            }
        }
    );
}
 
Example 15
Source File: TkConsumesTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * TkConsumes can accept request with certain Content-Type.
 * @throws Exception If some problem inside
 */
@Test
public void acceptsCorrectContentTypeRequest() throws Exception {
    final String contenttype = "Content-Type: application/json";
    final Take consumes = new TkConsumes(
        new TkFixed(new RsJson(new RsEmpty())),
        TkConsumesTest.APPLICATION_JSON
    );
    final Response response = consumes.act(
        new RqFake(
            Arrays.asList(
                "GET /?TkConsumes",
                contenttype
            ),
            ""
        )
    );
    MatcherAssert.assertThat(
        new RsPrint(response),
        new StartsWith(
            new Joined(
                "\r\n",
                "HTTP/1.1 200 OK",
                contenttype
            )
        )
    );
}
 
Example 16
Source File: TkProducesTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * TkProduce can produce correct type response.
 * @throws Exception If some problem inside
 */
@Test
public void producesCorrectContentTypeResponse() throws Exception {
    final Take produces = new TkProduces(
        new TkFixed(new RsJson(new RsEmpty())),
        "text/json"
    );
    final Response response = produces.act(
        new RqFake(
            Arrays.asList(
                "GET /hz09",
                "Host: as.example.com",
                "Accept: text/json"
            ),
            ""
        )
    );
    MatcherAssert.assertThat(
        new RsPrint(response),
        new TextIs(
            new Joined(
                "\r\n",
                "HTTP/1.1 200 OK",
                "Content-Type: application/json",
                "",
                ""
            )
        )
    );
}