org.takes.facets.fork.FkRegex Java Examples

The following examples show how to use org.takes.facets.fork.FkRegex. 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: FtBasicTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * FtBasic can work.
 * @throws Exception If some problem inside
 */
@Test
public void justWorks() throws Exception {
    new FtRemote(
        new TkFork(new FkRegex(FtBasicTest.ROOT_PATH, "hello, world!"))
    ).exec(
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                new JdkRequest(home)
                    .fetch()
                    .as(RestResponse.class)
                    .assertStatus(HttpURLConnection.HTTP_OK)
                    .assertBody(Matchers.startsWith("hello"));
            }
        }
    );
}
 
Example #2
Source File: BkBasicTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * BkBasic can return HTTP status 404 when accessing invalid URL.
 *
 * @throws Exception if any I/O error occurs.
 */
@Test
public void returnsProperResponseCodeOnInvalidUrl() throws Exception {
    new FtRemote(
        new TkFork(
            new FkRegex("/path/a", new TkText("a")),
            new FkRegex("/path/b", new TkText("b"))
        )
    ).exec(
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                new JdkRequest(String.format("%s/path/c", home))
                    .fetch()
                    .as(RestResponse.class)
                    .assertStatus(HttpURLConnection.HTTP_NOT_FOUND);
            }
        }
    );
}
 
Example #3
Source File: TakesApp.java    From tutorials with MIT License 6 votes vote down vote up
public static void main(final String... args) throws IOException, SQLException {
    new FtBasic(
        new TkFallback(
            new TkFork(
                new FkRegex("/", new TakesHelloWorld()),
                new FkRegex("/index", new TakesIndex()),
                new FkRegex("/contact", new TakesContact())
                ),
            new FbChain(
                new FbStatus(404, new RsText("Page Not Found")),
                new FbStatus(405, new RsText("Method Not Allowed")),
                new Fallback() {
                    @Override
                    public Opt<Response> route(final RqFallback req) {
                        return new Opt.Single<Response>(new RsText(req.throwable().getMessage()));
                    }
                })
            ), 6060
        ).start(Exit.NEVER);
}
 
Example #4
Source File: WebServerOperator.java    From java-operator-sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    log.info("WebServer Operator starting!");

    Config config = new ConfigBuilder().withNamespace(null).build();
    KubernetesClient client = new DefaultKubernetesClient(config);
    Operator operator = new Operator(client);
    operator.registerControllerForAllNamespaces(new WebServerController(client));

    new FtBasic(
            new TkFork(new FkRegex("/health", "ALL GOOD!")), 8080
    ).start(Exit.NEVER);
}
 
Example #5
Source File: MySQLSchemaOperator.java    From java-operator-sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    log.info("MySQL Schema Operator starting");

    Config config = new ConfigBuilder().withNamespace(null).build();
    KubernetesClient client = new DefaultKubernetesClient(config);
    Operator operator = new Operator(client);
    operator.registerControllerForAllNamespaces(new SchemaController(client));

    new FtBasic(
            new TkFork(new FkRegex("/health", "ALL GOOD!")), 8080
    ).start(Exit.NEVER);
}
 
Example #6
Source File: TkRelayTest.java    From jare with MIT License 5 votes vote down vote up
/**
 * TkRelay can send the request through.
 * @throws Exception If some problem inside
 */
@Test
public void sendsRequestThroughToHome() throws Exception {
    final Take target = new TkFork(
        new FkRegex(
            "/alpha/.*",
            (Take) req -> new RsText(
                new RqHref.Base(req).href().toString()
            )
        )
    );
    new FtRemote(target).exec(
        home -> MatcherAssert.assertThat(
            new RsPrint(
                new TkRelay(new FkBase()).act(
                    TkRelayTest.fake(
                        home, "/alpha/%D0%B4%D0%B0?abc=cde"
                    )
                )
            ).printBody(),
            Matchers.equalTo(
                String.format(
                    "%s/alpha/%%D0%%B4%%D0%%B0?abc=cde",
                    home
                )
            )
        )
    );
}
 
Example #7
Source File: App.java    From takes with MIT License 5 votes vote down vote up
@Override
public Response act(final Request request) throws Exception {
    return new TkFork(
        new FkRegex("/", new TkRedirect("/f")),
        new FkRegex(
            "/about",
            new TkHtml(App.class.getResource("about.html"))
        ),
        new FkRegex("/robots.txt", ""),
        new FkRegex("/f(.*)", new TkDir(this.home))
    ).act(request);
}
 
Example #8
Source File: PsLinkedinTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * PsLinkedin can login.
 * @throws Exception If some problem inside
 */
@Test
public void logins() throws Exception {
    final String tokenpath = "/uas/oauth2/accessToken";
    final String firstname = "firstName";
    final String lastname = "lastName";
    final String frodo = "Frodo";
    final String baggins = "Baggins";
    // @checkstyle MagicNumber (4 lines)
    final String code = RandomStringUtils.randomAlphanumeric(10);
    final String lapp = RandomStringUtils.randomAlphanumeric(10);
    final String lkey = RandomStringUtils.randomAlphanumeric(10);
    final String identifier = RandomStringUtils.randomAlphanumeric(10);
    final Take take = new TkFork(
        new FkRegex(
            tokenpath,
            new TokenTake(code, lapp, lkey, tokenpath)
        ),
        new FkRegex(
            "/v1/people",
            new PeopleTake(identifier, firstname, lastname, frodo, baggins)
        )
    );
    new FtRemote(take).exec(
        new LinkedinScript(
            code, lapp, lkey, identifier,
            firstname, lastname, frodo, baggins
        )
    );
}
 
Example #9
Source File: FtBasicTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * FtBasic can consume twice the input stream in case of a RsText.
 * @throws Exception If some problem inside
 */
@Test
public void consumesTwiceInputStreamWithRsText() throws Exception {
    final String result = "Hello RsText!";
    new FtRemote(
        new TkFork(
            new FkRegex(
                FtBasicTest.ROOT_PATH,
                new RsText(
                    new InputStreamOf(result)
                )
            )
        )
    ).exec(
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                new JdkRequest(home)
                    .fetch()
                    .as(RestResponse.class)
                    .assertStatus(HttpURLConnection.HTTP_OK)
                    .assertBody(Matchers.equalTo(result));
                new JdkRequest(home)
                    .fetch()
                    .as(RestResponse.class)
                    .assertStatus(HttpURLConnection.HTTP_OK)
                    .assertBody(Matchers.equalTo(result));
            }
        }
    );
}
 
Example #10
Source File: FtBasicTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * FtBasic can consume twice the input stream in case of a RsHTML.
 * @throws Exception If some problem inside
 */
@Test
public void consumesTwiceInputStreamWithRsHtml() throws Exception {
    final String result = "Hello RsHTML!";
    new FtRemote(
        new TkFork(
            new FkRegex(
                FtBasicTest.ROOT_PATH,
                new RsHtml(
                    new InputStreamOf(result)
                )
            )
        )
    ).exec(
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                new JdkRequest(home)
                    .fetch()
                    .as(RestResponse.class)
                    .assertStatus(HttpURLConnection.HTTP_OK)
                    .assertBody(Matchers.equalTo(result));
                new JdkRequest(home)
                    .fetch()
                    .as(RestResponse.class)
                    .assertStatus(HttpURLConnection.HTTP_OK)
                    .assertBody(Matchers.equalTo(result));
            }
        }
    );
}
 
Example #11
Source File: TkApp.java    From jpeek with MIT License 4 votes vote down vote up
/**
 * Ctor.
 * @param home Home directory
 * @return The take
 * @throws IOException If fails
 */
private static Take make(final Path home) throws IOException {
    final Futures futures = new Futures(
        new Reports(home)
    );
    final BiFunc<String, String, Func<String, Response>> reports =
        new AsyncReports(
            // @checkstyle MagicNumber (1 line)
            new StickyFutures(futures, 100)
        );
    return new TkSslOnly(
        new TkFallback(
            new TkForward(
                new TkFork(
                    new FkRegex("/", new TkIndex()),
                    new FkRegex("/robots.txt", new TkText("")),
                    new FkRegex("/mistakes", new TkMistakes()),
                    new FkRegex(
                        "/flush",
                        (Take) req -> new RsText(
                            String.format("%d flushed", new Results().flush())
                        )
                    ),
                    new FkRegex(
                        "/upload",
                        (Take) req -> new RsPage(req, "upload")
                    ),
                    new FkRegex("/do-upload", new TkUpload(reports)),
                    new FkRegex("/all", new TkAll()),
                    new FkRegex("/queue", new TkQueue(futures)),
                    new FkRegex(
                        ".+\\.xsl",
                        new TkWithType(
                            new TkClasspath(),
                            "text/xsl"
                        )
                    ),
                    new FkRegex(
                        "/jpeek\\.css",
                        new TkWithType(
                            new TkText(
                                new TextOf(
                                    new ResourceOf("org/jpeek/jpeek.css")
                                ).asString()
                            ),
                            "text/css"
                        )
                    ),
                    new FkRegex(
                        "/([^/]+)/([^/]+)(.*)",
                        new TkReport(reports, new Results())
                    )
                )
            ),
            new FbChain(
                new FbStatus(
                    HttpURLConnection.HTTP_NOT_FOUND,
                    (Fallback) req -> new Opt.Single<>(
                        new RsWithStatus(
                            new RsText(req.throwable().getMessage()),
                            req.code()
                        )
                    )
                ),
                req -> {
                    Sentry.capture(req.throwable());
                    return new Opt.Single<>(
                        new RsWithStatus(
                            new RsText(
                                new TextOf(req.throwable()).asString()
                            ),
                            HttpURLConnection.HTTP_INTERNAL_ERROR
                        )
                    );
                }
            )
        )
    );
}
 
Example #12
Source File: PsGoogleTest.java    From takes with MIT License 4 votes vote down vote up
/**
 * PsGoogle login.
 * @checkstyle MultipleStringLiteralsCheck (100 lines)
 * @throws Exception If some problem inside
 */
@Test
public void logsIn() throws Exception {
    final String octocat = "octocat";
    final String urn = "urn:google:1";
    final Take take = new TkFork(
        this.requestToken(),
        new FkRegex(
            PsGoogleTest.REGEX_PATTERN,
            // @checkstyle AnonInnerLengthCheck (1 line)
            new Take() {
                @Override
                public Response act(final Request req) throws IOException {
                    MatcherAssert.assertThat(
                        new RqPrint(req).printHead(),
                        Matchers.containsString(
                            PsGoogleTest.ACT_HEAD
                        )
                    );
                    MatcherAssert.assertThat(
                        new RqHref.Base(req).href()
                            .param(PsGoogleTest.ACCESS_TOKEN)
                            .iterator().next(),
                        Matchers.containsString(PsGoogleTest.GOOGLE_TOKEN)
                    );
                    return new RsJson(
                        Json.createObjectBuilder()
                            .add("displayName", octocat)
                            .add("id", "1")
                            .add(
                                PsGoogleTest.IMAGE,
                                Json.createObjectBuilder()
                                    .add(
                                        PsGoogleTest.URL,
                                        PsGoogleTest.AVATAR
                                    )
                            )
                            .build()
                    );
                }
            }
        )
    );
    new FtRemote(take).exec(
        // @checkstyle AnonInnerLengthCheck (100 lines)
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                final Identity identity = new PsGoogle(
                    PsGoogleTest.APP,
                    PsGoogleTest.KEY,
                    PsGoogleTest.ACCOUNT,
                    home.toString(),
                    home.toString()
                ).enter(
                    new RqFake(PsGoogleTest.GET, PsGoogleTest.CODE_PARAM)
                ).get();
                MatcherAssert.assertThat(
                    identity.urn(),
                    Matchers.equalTo(urn)
                );
                MatcherAssert.assertThat(
                    identity.properties().get(PsGoogleTest.NAME),
                    Matchers.equalTo(octocat)
                );
                MatcherAssert.assertThat(
                    identity.properties().get(PsGoogleTest.PICTURE),
                    Matchers.equalTo(PsGoogleTest.AVATAR)
                );
            }
        }
    );
}
 
Example #13
Source File: PsGoogleTest.java    From takes with MIT License 4 votes vote down vote up
/**
 * PsGoogle login with fail due a bad response from google.
 * @checkstyle MultipleStringLiteralsCheck (100 lines)
 * @throws Exception If some problem inside
 */
@Test(expected = IOException.class)
public void badGoogleResponse() throws Exception {
    final Take take = new TkFork(
        this.requestToken(),
        new FkRegex(
            PsGoogleTest.REGEX_PATTERN,
            // @checkstyle AnonInnerLengthCheck (1 line)
            new Take() {
                @Override
                public Response act(final Request req) throws IOException {
                    MatcherAssert.assertThat(
                        new RqPrint(req).printHead(),
                        Matchers.containsString(
                            PsGoogleTest.ACT_HEAD
                        )
                    );
                    MatcherAssert.assertThat(
                        new RqHref.Base(req).href()
                            .param(PsGoogleTest.ACCESS_TOKEN)
                            .iterator().next(),
                        Matchers.containsString(PsGoogleTest.GOOGLE_TOKEN)
                    );
                    return createErrorJson();
                }
            }
        )
    );
    new FtRemote(take).exec(
        // @checkstyle AnonInnerLengthCheck (100 lines)
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                new PsGoogle(
                    PsGoogleTest.APP,
                    PsGoogleTest.KEY,
                    PsGoogleTest.ACCOUNT,
                    home.toString(),
                    home.toString()
                ).enter(
                    new RqFake(PsGoogleTest.GET, PsGoogleTest.CODE_PARAM)
                ).get();
            }
        }
    );
}
 
Example #14
Source File: PsGoogleTest.java    From takes with MIT License 4 votes vote down vote up
/**
 * Test a google response without the displayName property.
 * @checkstyle MultipleStringLiteralsCheck (100 lines)
 * @throws Exception If some problem inside
 */
@Test
public void noDisplayNameResponse() throws Exception {
    final String urn = "urn:google:2";
    final Take take = new TkFork(
        this.requestToken(),
        new FkRegex(
            PsGoogleTest.REGEX_PATTERN,
            // @checkstyle AnonInnerLengthCheck (1 line)
            new Take() {
                @Override
                public Response act(final Request req) throws IOException {
                    MatcherAssert.assertThat(
                        new RqPrint(req).printHead(),
                        Matchers.containsString(
                            PsGoogleTest.ACT_HEAD
                        )
                    );
                    MatcherAssert.assertThat(
                        new RqHref.Base(req).href()
                            .param(PsGoogleTest.ACCESS_TOKEN)
                            .iterator().next(),
                        Matchers.containsString(PsGoogleTest.GOOGLE_TOKEN)
                    );
                    return new RsJson(
                        Json.createObjectBuilder()
                            .add("id", "2")
                            .add(
                                PsGoogleTest.IMAGE,
                                Json.createObjectBuilder()
                                    .add(
                                        PsGoogleTest.URL,
                                        PsGoogleTest.AVATAR
                                    )
                            )
                            .build()
                    );
                }
            }
        )
    );
    new FtRemote(take).exec(
        // @checkstyle AnonInnerLengthCheck (100 lines)
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                final Identity identity = new PsGoogle(
                    PsGoogleTest.APP,
                    PsGoogleTest.KEY,
                    PsGoogleTest.ACCOUNT,
                    home.toString(),
                    home.toString()
                ).enter(
                    new RqFake(PsGoogleTest.GET, PsGoogleTest.CODE_PARAM)
                ).get();
                MatcherAssert.assertThat(
                    identity.urn(),
                    Matchers.equalTo(urn)
                );
                MatcherAssert.assertThat(
                    identity.properties().get(PsGoogleTest.NAME),
                    Matchers.equalTo("unknown")
                );
                MatcherAssert.assertThat(
                    identity.properties().get(PsGoogleTest.PICTURE),
                    Matchers.equalTo(PsGoogleTest.AVATAR)
                );
            }
        }
    );
}
 
Example #15
Source File: PsGoogleTest.java    From takes with MIT License 4 votes vote down vote up
/**
 * Build a token request fork.
 * @return Returns the token request
 */
private FkRegex requestToken() {
    return new FkRegex(
        "/o/oauth2/token",
        // @checkstyle AnonInnerLengthCheck (1 line)
        new Take() {
            @Override
            public Response act(final Request req) throws IOException {
                MatcherAssert.assertThat(
                    new RqPrint(req).printHead(),
                    Matchers.containsString("POST /o/oauth2/token")
                );
                final Request greq = new RqGreedy(req);
                PsGoogleTest.assertParam(
                    greq,
                    "client_id",
                    PsGoogleTest.APP
                );
                PsGoogleTest.assertParam(
                    greq,
                    "redirect_uri",
                    PsGoogleTest.ACCOUNT
                );
                PsGoogleTest.assertParam(
                    greq,
                    "client_secret",
                    PsGoogleTest.KEY
                );
                PsGoogleTest.assertParam(
                    greq,
                    "grant_type",
                    "authorization_code"
                );
                PsGoogleTest.assertParam(
                    greq,
                    PsGoogleTest.CODE,
                    PsGoogleTest.CODE
                );
                return new RsJson(
                    Json.createObjectBuilder()
                        .add(
                            PsGoogleTest.ACCESS_TOKEN,
                            PsGoogleTest.GOOGLE_TOKEN
                         )
                        .add("expires_in", 1)
                        .add("token_type", "Bearer")
                        .build()
                );
            }
        }
    );
}
 
Example #16
Source File: PsGithubTest.java    From takes with MIT License 4 votes vote down vote up
/**
 * Performs the basic login.
 * @param directive The directive object.
 * @throws Exception If some problem inside.
 */
private void performLogin(final Directives directive) throws Exception {
    final String app = "app";
    final String key = "key";
    final Take take = new TkFork(
        new FkRegex(
            "/login/oauth/access_token",
            new Take() {
                @Override
                public Response act(final Request req) throws IOException {
                    final Request greq = new RqGreedy(req);
                    final String code = "code";
                    PsGithubTest.assertParam(greq, code, code);
                    PsGithubTest.assertParam(greq, "client_id", app);
                    PsGithubTest.assertParam(greq, "client_secret", key);
                    return new RsXembly(
                        new XeDirectives(directive.toString())
                    );
                }
            }
        ),
        new FkRegex(
            "/user",
            new TkFakeLogin()
        )
    );
    new FtRemote(take).exec(
        // @checkstyle AnonInnerLengthCheck (100 lines)
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                final Identity identity = new PsGithub(
                    app,
                    key,
                    home.toString(),
                    home.toString()
                ).enter(new RqFake("GET", "?code=code")).get();
                MatcherAssert.assertThat(
                    identity.urn(),
                    Matchers.equalTo("urn:github:1")
                );
                MatcherAssert.assertThat(
                    identity.properties().get(PsGithubTest.LOGIN),
                    Matchers.equalTo(PsGithubTest.OCTOCAT)
                );
                MatcherAssert.assertThat(
                    identity.properties().get("avatar"),
                    Matchers.equalTo(PsGithubTest.OCTOCAT_GIF_URL)
                );
            }
        }
    );
}