org.takes.tk.TkText Java Examples
The following examples show how to use
org.takes.tk.TkText.
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: 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 #2
Source File: HtKeepAliveResponseTest.java From cactoos-http with MIT License | 6 votes |
@Test public void worksFineByInputReq() throws IOException { new FtRemote(new TkText("Hello, world!")).exec( home -> new Assertion<>( "The HTTP response contains 200 status code", new TextOf( new HtKeepAliveResponse( new HtWire(home), 5000, 5, new Get(home) ) ), new TextHasString("HTTP/1.1 200 OK") ).affirm() ); }
Example #3
Source File: HtUpgradeWireTest.java From cactoos-http with MIT License | 6 votes |
/** * Tests if the wire of the response of an 101 code is an * {@link HtSecureWire}. * @throws Exception If Something goes wrong. */ @Test @Ignore("HtUpgradeWire implementation is not ready yet.") public void testUpgrade() throws Exception { new FtRemote(new TkAlways101Mock(new TkText("Hello, world!"))).exec( home -> MatcherAssert.assertThat( "Could not upgrade wire", new ResponseWrap( new HtUpgradeWire( new HtWire( home.getHost(), home.getPort() ) ), home.getHost() ).value(), new IsInstanceOf(HtSecureWire.class) ) ); }
Example #4
Source File: HtUpgradeWireTest.java From cactoos-http with MIT License | 6 votes |
/** * Test of {@link HtUpgradeWire} just to suit coverage standards. * @throws Exception If something goes wrong. */ @Test public void testHtUpgrade() throws Exception { new FtRemote(new TkText("Upgraded wire")).exec( home -> MatcherAssert.assertThat( "Upgrade wire not found", new TextOf( new HtResponse( new HtUpgradeWire( new HtWire( home.getHost(), home.getPort() ) ), home.getHost() ) ).asString(), Matchers.containsString("HTTP/1.1 200") ) ); }
Example #5
Source File: FtBasicTest.java From takes with MIT License | 6 votes |
/** * FtBasic can work with broken pipe if {@link BkSafe} is used. * @throws IOException If some problem inside */ @Test public void gracefullyHandlesBrokenPipe() throws IOException { new FtBasic( new BkSafe( new BkBasic( new TkText("Body") ) ), FtBasicTest.server() ).start( new Exit() { @Override public boolean ready() { return true; } } ); }
Example #6
Source File: HtAutoRedirectTest.java From cactoos-http with MIT License | 6 votes |
@Test public void redirectsRequestAutomatically() throws Exception { new FtRemote(new TkText("redirected ok")).exec( home -> MatcherAssert.assertThat( "Does not redirects automatically", new TextOf( new HtAutoRedirect( new InputOf( new Joined( new TextOf("\r\n"), new TextOf("HTTP/1.1 301"), new FormattedText( "Location: %s", home ) ) ) ) ), new TextHasString("HTTP/1.1 200 ") ) ); }
Example #7
Source File: HtTimedWireTest.java From cactoos-http with MIT License | 6 votes |
@Test public void worksFine() throws Exception { // @checkstyle MagicNumberCheck (1 line) final long timeout = 1000; new FtRemote(new TkText("Hello, world!")).exec( home -> MatcherAssert.assertThat( new TextOf( new HtResponse( new HtTimedWire(new HtWire(home), timeout), new Get(home) ) ), new TextHasString("HTTP/1.1 200 ") ) ); }
Example #8
Source File: HtSecureWireTest.java From cactoos-http with MIT License | 6 votes |
@Test public void worksFineThroughSsl() throws Exception { HtSecureWireTest.secure(new TkText("Hello, world!"), 0).exec( home -> MatcherAssert.assertThat( "Basic ssl request doesn't work for specified host", new TextOf( new HtResponse( new HtSecureWire( home.getHost(), home.getPort() ), new HtSecureWireTest.Request(home.getHost()) ) ).asString(), Matchers.containsString("HTTP/1.1 200") ) ); }
Example #9
Source File: HtSecureWireTest.java From cactoos-http with MIT License | 6 votes |
@Test public void worksFineByUriThroughSsl() throws Exception { HtSecureWireTest.secure(new TkText(), 0).exec( home -> MatcherAssert.assertThat( "Doesn't work through ssl for specified uri", new TextOf( new HtResponse( new HtSecureWire( home ), new HtSecureWireTest.Request(home.getHost()) ) ).asString(), Matchers.containsString("HTTP/1.1 200 OK") ) ); }
Example #10
Source File: TkPreviousTest.java From takes with MIT License | 6 votes |
/** * TkPrevious can redirect. * @throws Exception If some problem inside */ @Test public void redirectsOnCookie() throws Exception { MatcherAssert.assertThat( new RsPrint( new TkPrevious(new TkText("")).act( new RqWithHeader( new RqFake(), "Cookie", "TkPrevious=/home" ) ) ), new StartsWith("HTTP/1.1 303 See Other") ); }
Example #11
Source File: TkAuthTest.java From takes with MIT License | 6 votes |
/** * TkAuth can logout a user when a login cookie is present. * @throws Exception If some problem inside */ @Test public void logsUserOutWithCookiePresent() throws Exception { new Assertion<>( "Response with header setting empty cookie", new RsPrint( new TkAuth( new TkText(), new PsChain( new PsLogout(), new PsCookie(new CcPlain()) ) ).act( new RqWithHeader( new RqFake(), String.format( "Cookie: %s=%s", PsCookie.class.getSimpleName(), "urn%3Atest%3A5" ) ) ) ), new TextHasString("Set-Cookie: PsCookie=") ).affirm(); }
Example #12
Source File: PsBasicTest.java From takes with MIT License | 6 votes |
/** * PsBasic can request authentication. * @throws Exception If some problem inside */ @Test public void requestAuthentication() throws Exception { final Take take = new TkForward( new TkAuth( new TkSecure( new TkText("secured area...") ), new PsBasic( "the realm 5", new PsBasic.Default("bob pwd88 urn:users:bob") ) ) ); new Assertion<>( "Response with 401 Unauthorized status", new RsPrint( take.act(new RqFake()) ), new TextHasString("HTTP/1.1 401 Unauthorized\r\n") ).affirm(); }
Example #13
Source File: PsBasicTest.java From takes with MIT License | 6 votes |
/** * PsBasic can authenticate a user. * @throws Exception If some problem inside */ @Test public void authenticatesUser() throws Exception { final Take take = new TkAuth( new TkSecure( new TkText("secured") ), new PsBasic( "myrealm", new PsBasic.Default("mike secret11 urn:users:michael") ) ); new Assertion<>( "PsBasic should authenticate mike", new RsPrint( take.act( new RqWithHeader( new RqFake(), PsBasicTest.header("mike", "secret11") ) ) ), new TextHasString("HTTP/1.1 200 OK") ).affirm(); }
Example #14
Source File: HtKeepAliveResponseTest.java From cactoos-http with MIT License | 5 votes |
@Test public void worksFineByStringReq() throws IOException { new FtRemote(new TkText("Hello, dude!")).exec( home -> new Assertion<>( "The HTTP response contains 200 status code", new TextOf( new HtKeepAliveResponse(new HtWire(home), 5000, 5, "req1") ), new TextHasString("HTTP/1.1 200 OK") ).affirm() ); }
Example #15
Source File: BkBasicTest.java From takes with MIT License | 5 votes |
/** * BkBasic can handle socket data. * * @throws Exception If some problem inside */ @Test public void handlesSocket() throws Exception { final MkSocket socket = BkBasicTest.createMockSocket(); final ByteArrayOutputStream baos = socket.bufferedOutput(); final String hello = "Hello World"; new BkBasic(new TkText(hello)).accept(socket); MatcherAssert.assertThat( baos.toString(), Matchers.containsString(hello) ); }
Example #16
Source File: TkAuthTest.java From takes with MIT License | 5 votes |
/** * TkAuth can login a user via cookie. * @throws Exception If some problem inside */ @Test @SuppressWarnings("unchecked") public void logsInUserViaCookie() throws Exception { new Assertion<>( "Response with header Set-Cookie", new RsPrint( new TkAuth( new TkText(), new PsChain( new PsCookie(new CcPlain()), new PsLogout() ) ).act( new RqWithHeader( new RqFake(), new FormattedText( "Cookie: %s=%s", PsCookie.class.getSimpleName(), "urn%3Atest%3A0" ).asString() ) ) ), new AllOf<>( new IterableOf<>( new StartsWith("HTTP/1.1 200 "), new TextHasString("Set-Cookie: PsCookie=urn%3Atest%3A0") ) ) ).affirm(); }
Example #17
Source File: TkConsumesTest.java From takes with MIT License | 5 votes |
/** * Checks TkConsumes equals method. * @throws Exception If some problem inside */ @Test @SuppressWarnings("PMD.AvoidDuplicateLiterals") public void equalsAndHashCodeEqualTest() throws Exception { final Take take = new TkText("text"); final String type = "Content-Type: text/plain"; new Assertion<>( "Must evaluate true equality", new TkConsumes(take, type), new IsEqual<>(new TkConsumes(take, type)) ).affirm(); }
Example #18
Source File: FkRegex.java From takes with MIT License | 5 votes |
/** * Ctor. * @param ptn Pattern * @param text Text */ public FkRegex(final String ptn, final String text) { this( Pattern.compile(ptn, Pattern.CASE_INSENSITIVE | Pattern.DOTALL), new TkText(text) ); }
Example #19
Source File: TkRelayTest.java From jare with MIT License | 5 votes |
/** * TkRelay can set cache headers to "forever". * @throws Exception If some problem inside */ @Test public void setsCachingHeaders() throws Exception { final Take target = new TkWithHeaders( new TkText("cacheable forever"), "age: 600", "cache-control: max-age=600", "expires: Thu, 08 Dec 2016 22:51:37 GMT" ); new FtRemote(target).exec( home -> MatcherAssert.assertThat( new RsPrint( new TkRelay(new FkBase()).act( TkRelayTest.fake(home, "/&whatever") ) ), Matchers.allOf( new HmRsHeader("Age", "31536000"), new HmRsHeader("Cache-control", "max-age=31536000"), new HmRsHeader("Expires", "Sun, 19 Jul 2020 18:06:32 GMT"), Matchers.not( new HmRsHeader("Cache-Control", "max-age=600") ) ) ) ); }
Example #20
Source File: HtWireTest.java From cactoos-http with MIT License | 5 votes |
@Test @SuppressWarnings("PMD.AvoidCatchingGenericException") public void closesSocketOnlyAfterResponseIsClosed() throws Exception { new FtRemote(new TkText("Hey")).exec( home -> { @SuppressWarnings("resource") final Socket socket = new Socket( home.getHost(), home.getPort() ); try (InputStream ins = new HtWire(() -> socket).send( new Get(home) ).stream()) { new Assertion<>( "must have a response", new TextOf(new ReadBytes(ins)), new TextHasString("HTTP/1.1 200 OK") ).affirm(); new Assertion<>( "must keep the socket open until response is closed", socket.isClosed(), new IsNot<>(new IsTrue()) ).affirm(); // @checkstyle IllegalCatchCheck (1 line) } catch (final Exception ex) { throw new IOException(ex); } new Assertion<>( "must close the socket once input response is closed", socket.isClosed(), new IsTrue() ).affirm(); } ); }
Example #21
Source File: HtWireTest.java From cactoos-http with MIT License | 5 votes |
@Test public void worksWithProvidedHostNameAndPort() throws IOException { new FtRemote(new TkText("Hello")).exec( home -> MatcherAssert.assertThat( new TextOf( new HtResponse( new HtWire(home.getHost(), home.getPort()), new Get(home) ) ), new TextHasString("HTTP/1.1 200") ) ); }
Example #22
Source File: HtResponseTest.java From cactoos-http with MIT License | 5 votes |
@Test public void worksFineByUri() throws IOException { new FtRemote(new TkText("Hello, dude!")).exec( home -> MatcherAssert.assertThat( new TextOf( new HtResponse(home) ).asString(), Matchers.containsString("HTTP/1.1 200 OK") ) ); }
Example #23
Source File: HtResponseTest.java From cactoos-http with MIT License | 5 votes |
@Test public void worksFine() throws IOException { new FtRemote(new TkText("Hello, world!")).exec( home -> MatcherAssert.assertThat( new TextOf( new HtResponse( new HtWire(home), new Get(home) ) ), new TextHasString("HTTP/1.1 200 OK") ) ); }
Example #24
Source File: HtAutoClosedResponseTest.java From cactoos-http with MIT License | 5 votes |
@Test @SuppressWarnings("PMD.AvoidCatchingGenericException") public void closesResponseAndThusSocketWhenEofIsReached() throws Exception { new FtRemote(new TkText("Hey")).exec( home -> { @SuppressWarnings("resource") final Socket socket = new Socket( home.getHost(), home.getPort() ); try (InputStream ins = new HtAutoClosedResponse( new HtResponse( new HtWire(() -> socket), new Get(home) ) ).stream()) { new Assertion<>( "must have a response", new TextOf(new ReadBytes(ins)), new TextHasString("HTTP/1.1 200 OK") ).affirm(); new Assertion<>( "must close the response, thus the socket, after EOF", socket.isClosed(), new IsTrue() ).affirm(); new Assertion<>( "must behave as closed", ins::available, new Throws<>("Stream closed.", IOException.class) ).affirm(); // @checkstyle IllegalCatchCheck (1 line) } catch (final Exception ex) { throw new IOException(ex); } } ); }
Example #25
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 #26
Source File: BkBasicTest.java From takes with MIT License | 4 votes |
/** * BkBasic can handle two requests in one connection. * * @throws Exception If some problem inside */ @Test public void handlesTwoRequestInOneConnection() throws Exception { final String text = "Hello Twice!"; final ByteArrayOutputStream output = new ByteArrayOutputStream(); try (ServerSocket server = new ServerSocket(0)) { new Thread( new Runnable() { @Override public void run() { try { new BkBasic(new TkText(text)).accept( server.accept() ); } catch (final IOException exception) { throw new IllegalStateException(exception); } } } ).start(); try (Socket socket = new Socket( server.getInetAddress(), server.getLocalPort() ) ) { socket.getOutputStream().write( new Joined( BkBasicTest.CRLF, BkBasicTest.POST, BkBasicTest.HOST, "Content-Length: 11", "", "Hello First", BkBasicTest.POST, BkBasicTest.HOST, "Content-Length: 12", "", "Hello Second" ).asString().getBytes() ); final InputStream input = socket.getInputStream(); // @checkstyle MagicNumber (1 line) final byte[] buffer = new byte[4096]; for (int count = input.read(buffer); count != -1; count = input.read(buffer)) { output.write(buffer, 0, count); } } } MatcherAssert.assertThat( output.toString(), RegexMatchers.containsPattern( String.format("(?s)%s.*?%s", text, text) ) ); }
Example #27
Source File: BkBasicTest.java From takes with MIT License | 4 votes |
/** * BkBasic can return HTTP status 411 when a persistent connection request * has no Content-Length. * * @throws Exception If some problem inside */ @Ignore @Test public void returnsProperResponseCodeOnNoContentLength() throws Exception { final ByteArrayOutputStream output = new ByteArrayOutputStream(); final String text = "Say hello!"; try (ServerSocket server = new ServerSocket(0)) { new Thread( new Runnable() { @Override public void run() { try { new BkBasic(new TkText("411 Test")).accept( server.accept() ); } catch (final IOException exception) { throw new IllegalStateException(exception); } } } ).start(); try (Socket socket = new Socket( server.getInetAddress(), server.getLocalPort() ) ) { socket.getOutputStream().write( new BytesOf( new Joined( BkBasicTest.CRLF, BkBasicTest.POST, BkBasicTest.HOST, "", text ) ).asBytes() ); final InputStream input = socket.getInputStream(); // @checkstyle MagicNumber (1 line) final byte[] buffer = new byte[4096]; for (int count = input.read(buffer); count != -1; count = input.read(buffer)) { output.write(buffer, 0, count); } } } MatcherAssert.assertThat( output.toString(), Matchers.containsString("HTTP/1.1 411 Length Required") ); }
Example #28
Source File: BkBasicTest.java From takes with MIT License | 4 votes |
/** * BkBasic can accept no content-length on closed connection. * * @throws Exception If some problem inside */ @Ignore @Test public void acceptsNoContentLengthOnClosedConnection() throws Exception { final String text = "Close Test"; final ByteArrayOutputStream output = new ByteArrayOutputStream(); final String greetings = "Hi everyone"; try (ServerSocket server = new ServerSocket(0)) { new Thread( new Runnable() { @Override public void run() { try { new BkBasic(new TkText(text)).accept( server.accept() ); } catch (final IOException exception) { throw new IllegalStateException(exception); } } } ).start(); try (Socket socket = new Socket( server.getInetAddress(), server.getLocalPort() ) ) { socket.getOutputStream().write( new BytesOf( new Joined( BkBasicTest.CRLF, BkBasicTest.POST, BkBasicTest.HOST, "Connection: Close", "", greetings ) ).asBytes() ); final InputStream input = socket.getInputStream(); // @checkstyle MagicNumber (1 line) final byte[] buffer = new byte[4096]; for (int count = input.read(buffer); count != -1; count = input.read(buffer)) { output.write(buffer, 0, count); } } } MatcherAssert.assertThat( output.toString(), Matchers.containsString(text) ); }