org.takes.facets.fork.TkFork Java Examples
The following examples show how to use
org.takes.facets.fork.TkFork.
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: TakesApp.java From tutorials with MIT License | 6 votes |
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 #2
Source File: TkProxyTest.java From takes with MIT License | 6 votes |
/** * TkProxy can just work. * @throws Exception If some problem inside */ @Test public void justWorks() throws Exception { new FtRemote( new TkFork( new FkMethods(this.method, new TkFixed(this.expected)) ) ).exec( new FtRemote.Script() { @Override public void exec(final URI home) throws Exception { MatcherAssert.assertThat( new RsPrint( new TkProxy(home).act( new RqFake(TkProxyTest.this.method) ) ), new TextHasString( TkProxyTest.this.expected ) ); } } ); }
Example #3
Source File: TkRefresh.java From jare with MIT License | 6 votes |
/** * Ctor. * @param path Path of files * @throws IOException If fails */ TkRefresh(final String path) throws IOException { super( new TkFork( new FkHitRefresh( new File(path), () -> new VerboseProcess( new ProcessBuilder( "mvn", "generate-resources" ) ).stdout(), new TkFiles("./target/classes") ), new FkFixed(new TkClasspath()) ) ); }
Example #4
Source File: BkBasicTest.java From takes with MIT License | 6 votes |
/** * 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 #5
Source File: FtBasicTest.java From takes with MIT License | 6 votes |
/** * 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 #6
Source File: MySQLSchemaOperator.java From java-operator-sdk with Apache License 2.0 | 5 votes |
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 #7
Source File: TkProxyTest.java From takes with MIT License | 5 votes |
/** * TkProxy can correctly maps path string. * @throws Exception If some problem inside * @checkstyle AnonInnerLengthCheck (100 lines) */ @Test public void correctlyMapsPathString() throws Exception { final Take take = new Take() { @Override public Response act(final Request req) throws IOException { return new RsText(new RqHref.Base(req).href().toString()); } }; new FtRemote( new TkFork( new FkMethods(this.method, take) ) ).exec( new FtRemote.Script() { @Override public void exec(final URI home) throws Exception { MatcherAssert.assertThat( new RsPrint( new TkProxy(home).act( new RqFake( TkProxyTest.this.method, "/a/%D0%B0/c?%D0%B0=1#%D0%B0" ) ) ).printBody(), Matchers.equalTo( String.format( "http://%s:%d/a/%%D0%%B0/c?%%D0%%B0=1", home.getHost(), home.getPort() ) ) ); } } ); }
Example #8
Source File: FtBasicTest.java From takes with MIT License | 5 votes |
/** * 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 #9
Source File: FtBasicTest.java From takes with MIT License | 5 votes |
/** * 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: PsLinkedinTest.java From takes with MIT License | 5 votes |
/** * 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 #11
Source File: App.java From takes with MIT License | 5 votes |
@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 #12
Source File: TkRelayTest.java From jare with MIT License | 5 votes |
/** * 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 #13
Source File: WebServerOperator.java From java-operator-sdk with Apache License 2.0 | 5 votes |
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 #14
Source File: PsGoogleTest.java From takes with MIT License | 4 votes |
/** * 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: PsGithubTest.java From takes with MIT License | 4 votes |
/** * 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) ); } } ); }
Example #16
Source File: PsGoogleTest.java From takes with MIT License | 4 votes |
/** * 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 #17
Source File: PsGoogleTest.java From takes with MIT License | 4 votes |
/** * 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 #18
Source File: TkAppAuth.java From jare with MIT License | 4 votes |
/** * Authenticated. * @param take Takes * @return Authenticated takes */ private static Take make(final Take take) { return new TkAuth( new TkFork( new FkParams( PsByFlag.class.getSimpleName(), Pattern.compile(".+"), new TkRedirect() ), new FkFixed(take) ), new PsChain( new PsFake( Manifests.read("Jare-DynamoKey").startsWith("AAAA") ), new PsByFlag( new PsByFlag.Pair( PsGithub.class.getSimpleName(), new PsGithub( Manifests.read("Jare-GithubId"), Manifests.read("Jare-GithubSecret") ) ), new PsByFlag.Pair( PsLogout.class.getSimpleName(), new PsLogout() ) ), new PsCookie( new CcSafe( new CcHex( new CcAes( new CcSalted(new CcCompact()), Manifests.read("Jare-SecurityKey") ) ) ) ) ) ); }
Example #19
Source File: TkApp.java From jpeek with MIT License | 4 votes |
/** * 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 #20
Source File: TkProxyTest.java From takes with MIT License | 4 votes |
/** * TkProxy can properly modifies the host header. * @throws Exception If some problem inside */ @Test public void modifiesHost() throws Exception { new FtRemote( new TkFork( new FkMethods(this.method, TkProxyTest.ECHO) ) ).exec( // @checkstyle AnonInnerLengthCheck (100 lines) new FtRemote.Script() { @Override public void exec(final URI home) throws Exception { MatcherAssert.assertThat( new RsPrint(new TkProxy( home.toURL().toString() ).act( new RqFake( Arrays.asList( String.format( "%s /f?%%D0%%B0=3&b-6", TkProxyTest.this.method ), "Host: example.com", "Accept: text/xml", "Accept: text/html" ), "" ) )).printBody(), Matchers.containsString( String.format( "Host: %s:%d", home.getHost(), home.getPort() ) ) ); } } ); }
Example #21
Source File: TkProxyTest.java From takes with MIT License | 4 votes |
/** * TkProxy can add its specific header. * @throws Exception If some problem inside */ @Test public void addsSpecificHeader() throws Exception { final String mark = "foo"; new FtRemote( new TkFork( new FkMethods(this.method, TkProxyTest.ECHO) ) ).exec( // @checkstyle AnonInnerLengthCheck (100 lines) new FtRemote.Script() { @Override public void exec(final URI home) throws Exception { MatcherAssert.assertThat( new RsPrint(new TkProxy( home.toURL().toString(), mark ).act( new RqFake( Arrays.asList( String.format( "%s /%%D0%%B0", TkProxyTest.this.method ), "Host: www.bar.com" ), "" ) )).printHead(), Matchers.containsString( String.format( // @checkstyle LineLengthCheck (1 line) "X-Takes-TkProxy: from /%%D0%%B0 to %s/%%D0%%B0 by %s", home, mark ) ) ); } } ); }
Example #22
Source File: TkProxyTest.java From takes with MIT License | 4 votes |
/** * TkProxy can add all initial headers. * @throws Exception If some problem inside */ @Test public void addsAllInitialHeaders() throws Exception { final String body = "Hello World !"; new FtRemote( new TkFork( new FkMethods("POST", TkProxyTest.ECHO) ) ).exec( // @checkstyle AnonInnerLengthCheck (100 lines) new FtRemote.Script() { @Override public void exec(final URI home) throws Exception { MatcherAssert.assertThat( new RsPrint( new TkProxy(home).act( new RqFake( Arrays.asList( "POST /%D0%B0", String.format( "Content-Length: %s", body.length() ), "Content-Type: text/plain", "Accept: text/json", "Cookie: a=45", "Cookie: ttt=ALPHA", "Accept-Encoding: gzip", "Host: www.bar-foo.com" ), body ) ) ).printBody(), Matchers.allOf( Matchers.containsString("Content-Length:"), Matchers.containsString("Content-Type:"), Matchers.containsString("Accept:"), Matchers.containsString("Cookie: a"), Matchers.containsString("Cookie: ttt"), Matchers.containsString("Accept-Encoding:") ) ); } } ); }