org.llorllale.cactoos.matchers.StartsWith Java Examples

The following examples show how to use org.llorllale.cactoos.matchers.StartsWith. 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: InputAsBytesTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsInputIntoBytes() throws Exception {
    new Assertion<>(
        "must read bytes from Input",
        new TextOf(
            new InputAsBytes(
                new InputOf(
                    new BytesOf(
                        new TextOf("Hello, друг!")
                    )
                )
            ),
            StandardCharsets.UTF_8
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith("Hello, "),
                new EndsWith("друг!")
            )
        )
    ).affirm();
}
 
Example #2
Source File: RqWithHeadersTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * RqWithHeaders can add headers.
 * @throws IOException If some problem inside
 */
@Test
public void addsHeadersToRequest() throws IOException {
    final String testheader = "TestHeader: someValue";
    final String someheader = "SomeHeader: testValue";
    MatcherAssert.assertThat(
        new TextOf(
            new RqPrint(
                new RqWithHeaders(
                    new RqFake(),
                    testheader,
                    someheader
                )
            ).print()
        ),
        new StartsWith(
            new Joined(
                "\r\n",
                "GET /",
                "Host: www.example.com",
                testheader,
                someheader
            )
        )
    );
}
 
Example #3
Source File: RqWithDefaultHeaderTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * RqWithDefaultHeader can provide default header value.
 * @throws IOException If some problem inside
 */
@Test
public void providesDefaultHeader() throws IOException {
    final String req = "GET /";
    MatcherAssert.assertThat(
        new TextOf(
            new RqPrint(
                new RqWithDefaultHeader(
                    new RqFake(Collections.singletonList(req), "body"),
                    "X-Default-Header1",
                    "X-Default-Value1"
                )
            ).print()
        ),
        new StartsWith(
            new Joined(
                RqWithDefaultHeaderTest.CRLF,
                req,
                "X-Default-Header1: X-Default-Value"
            )
        )
    );
}
 
Example #4
Source File: TkFilesTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkFiles can dispatch by file name.
 * @throws Exception If some problem inside
 */
@Test
public void dispatchesByFileName() throws Exception {
    FileUtils.write(
        this.temp.newFile("a.txt"), "hello, world!", StandardCharsets.UTF_8
    );
    MatcherAssert.assertThat(
        new RsPrint(
            new TkFiles(this.temp.getRoot()).act(
                new RqFake(
                    "GET", "/a.txt?hash=a1b2c3", ""
                )
            )
        ),
        new StartsWith("HTTP/1.1 200 OK")
    );
}
 
Example #5
Source File: TkGzipTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkGzip can return uncompressed content.
 * @throws Exception If some problem inside
 */
@Test
public void doesntCompressIfNotRequired() throws Exception {
    MatcherAssert.assertThat(
        new RsPrint(
            new TkGzip(new TkClasspath()).act(
                new RqFake(
                    Arrays.asList(
                        "GET /org/takes/tk/TkGzip.class HTTP/1.0",
                        "Host: abc.example.com"
                    ),
                    ""
                )
            )
        ),
        new StartsWith("HTTP/1.1 200")
    );
}
 
Example #6
Source File: TkGzipTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkGzip can compress on demand only.
 * @throws Exception If some problem inside
 */
@Test
public void compressesOnDemandOnly() throws Exception {
    MatcherAssert.assertThat(
        new RsPrint(
            new TkGzip(new TkClasspath()).act(
                new RqFake(
                    Arrays.asList(
                        "GET /org/takes/tk/TkGzip.class HTTP/1.1",
                        "Host: www.example.com",
                        "Accept-Encoding: gzip"
                    ),
                    ""
                )
            )
        ),
        new StartsWith("HTTP/1.1 200 OK")
    );
}
 
Example #7
Source File: TkForwardTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkForward can catch RsForward throws by Response.
 * @throws Exception If some problem inside
 */
@Test
public void catchesExceptionThrownByResponse() throws Exception {
    final Take take =
        request -> new ResponseOf(
            () -> new RsEmpty().head(),
            () -> {
                throw new RsForward("/b");
            }
        );
    MatcherAssert.assertThat(
        new RsPrint(
            new TkForward(take).act(new RqFake())
        ),
        new StartsWith("HTTP/1.1 303")
    );
}
 
Example #8
Source File: TkForwardTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkForward can catch RsForward.
 * @throws Exception If some problem inside
 */
@Test
public void catchesExceptionCorrectly() throws Exception {
    final Take take = new Take() {
        @Override
        public Response act(final Request request) throws RsForward {
            throw new RsForward("/");
        }
    };
    MatcherAssert.assertThat(
        new RsPrint(
            new TkForward(take).act(new RqFake())
        ),
        new StartsWith("HTTP/1.1 303 See Other")
    );
}
 
Example #9
Source File: TkPreviousTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * 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 #10
Source File: BytesOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsInputIntoBytesWithSmallBuffer() throws Exception {
    new Assertion<>(
        "must read bytes from Input with a small reading buffer",
        new TextOf(
            new BytesOf(
                new InputOf("Hello, товарищ!"),
                2
            )
        ),
        Matchers.allOf(
            new StartsWith("Hello,"),
            new EndsWith("товарищ!")
        )
    ).affirm();
}
 
Example #11
Source File: InputOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsEncodedArrayOfChars() throws Exception {
    new Assertion<>(
        "must read array of encoded chars.",
        new TextOf(
            new BytesOf(
                new InputOf(
                    new char[]{
                        'O', ' ', 'q', 'u', 'e', ' ', 's', 'e', 'r', 'a',
                        ' ', 'q', 'u', 'e', ' ', 's', 'e', 'r', 'a',
                    },
                    StandardCharsets.UTF_8
                )
            ),
            StandardCharsets.UTF_8
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith("O que sera"),
                new EndsWith(" que sera")
            )
        )
    ).affirm();
}
 
Example #12
Source File: InputOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsArrayOfChars() throws Exception {
    new Assertion<>(
        "must read array of chars.",
        new TextOf(
            new BytesOf(
                new InputOf(
                    'H', 'o', 'l', 'd', ' ',
                    'i', 'n', 'f', 'i', 'n', 'i', 't', 'y'
                )
            )
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith("Hold "),
                new EndsWith("infinity")
            )
        )
    ).affirm();
}
 
Example #13
Source File: InputOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsStringBuffer() throws Exception {
    final String starts = "The future ";
    final String ends = "is now!";
    new Assertion<>(
        "must receive a string buffer",
        new TextOf(
            new BytesOf(
                new InputOf(
                    new StringBuffer(starts)
                        .append(ends)
                )
            )
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith(starts),
                new EndsWith(ends)
            )
        )
    ).affirm();
}
 
Example #14
Source File: InputOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsStringBuilder() throws Exception {
    final String starts = "Name it, ";
    final String ends = "then it exists!";
    new Assertion<>(
        "must receive a string builder",
        new TextOf(
            new BytesOf(
                new InputOf(
                    new StringBuilder(starts)
                        .append(ends)
                )
            )
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith(starts),
                new EndsWith(ends)
            )
        )
    ).affirm();
}
 
Example #15
Source File: InputOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsStringIntoBytes() throws Exception {
    new Assertion<>(
        "must read bytes from Input",
        new TextOf(
            new BytesOf(
                new InputOf("Hello, друг!")
            ),
            StandardCharsets.UTF_8
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith("Hello, "),
                new EndsWith("друг!")
            )
        )
    ).affirm();
}
 
Example #16
Source File: InputAsBytesTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsInputIntoBytesWithSmallBuffer() throws Exception {
    new Assertion<>(
        "must read bytes from Input with a small reading buffer",
        new TextOf(
            new InputAsBytes(
                new InputOf(
                    new BytesOf(
                        new TextOf("Hello, товарищ!")
                    )
                ),
                2
            ),
            StandardCharsets.UTF_8
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith("Hello,"),
                new EndsWith("товарищ!")
            )
        )
    ).affirm();
}
 
Example #17
Source File: BytesOfTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void readsInputIntoBytes() throws Exception {
    new Assertion<>(
        "must read bytes from Input",
        new TextOf(
            new BytesOf(
                new InputOf("Hello, друг!")
            )
        ),
        Matchers.allOf(
            new StartsWith("Hello, "),
            new EndsWith("друг!")
        )
    ).affirm();
}
 
Example #18
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 #19
Source File: TkAuthTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * 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 #20
Source File: TempFileTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void createFileWithPrefix() throws Exception {
    final String prefix = new FormattedText(
        "randomPrefix%s",
        System.currentTimeMillis()
    ).asString();
    try (TempFile file = new TempFile(prefix, "")) {
        new Assertion<>(
            "File must be created with the given prefix",
            new TextOf(file.value().getFileName().toString()),
            new StartsWith(prefix)
        ).affirm();
    }
}
 
Example #21
Source File: TkClasspathTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * TkClasspath can dispatch by resource name.
 * @throws Exception If some problem inside
 */
@Test
public void dispatchesByResourceName() throws Exception {
    MatcherAssert.assertThat(
        new RsPrint(
            new TkClasspath().act(
                new RqFake(
                    "GET", "/org/takes/Take.class?a", ""
                )
            )
        ),
        new StartsWith("HTTP/1.1 200 OK")
    );
}
 
Example #22
Source File: RqWithDefaultHeaderTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * RqWithDefaultHeader can override default value.
 * @throws IOException If some problem inside
 */
@Test
public void allowsOverrideDefaultHeader() throws IOException {
    final String req = "POST /";
    final String header = "X-Default-Header2";
    MatcherAssert.assertThat(
        new TextOf(
            new RqPrint(
                new RqWithDefaultHeader(
                    new RqWithHeader(
                        new RqFake(Collections.singletonList(req), "body2"),
                        header,
                        "Non-Default-Value2"
                    ),
                    header,
                    "X-Default-Value"
                )
            ).print()
        ),
        new StartsWith(
            new Joined(
                RqWithDefaultHeaderTest.CRLF,
                req,
                "X-Default-Header2: Non-Default-Value"
            )
        )
    );
}
 
Example #23
Source File: HtHeadTest.java    From cactoos-http with MIT License 4 votes vote down vote up
@Test
public void edgeOfTheBlockTearing() throws Exception {
    final int size = 16384;
    final Text header = new Joined(
        "\r\n",
        "HTTP/1.1 200 OK",
        "Referer: http://en.wikipedia.org/wiki/Main_Page#\0",
        "Content-type: text/plain",
        ""
    );
    final Text block = new Replaced(
        header,
        "\0",
        new Repeated(
            "x",
            size - header.asString().length() + 1
        ).asString()
    );
    new Assertion<>(
        "make sure the constructed block is exact size",
        block.asString().length(),
        new IsEqual<>(
            size
        )
    ).affirm();
    new Assertion<>(
        String.format("Edge of the block tearing for size: %s", size),
        new TextOf(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        block.asString(),
                        "",
                        "body here"
                    )
                )
            )
        ),
        Matchers.allOf(
            new StartsWith("HTTP"),
            new TextHasString("OK\r\nReferer"),
            new EndsWith("text/plain")
        )
    ).affirm();
}