org.takes.rq.RqWithHeader Java Examples

The following examples show how to use org.takes.rq.RqWithHeader. 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: FkHitRefreshTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * FkHitRefresh can refresh on demand.
 * @throws Exception If some problem inside
 */
@Test
public void refreshesOnDemand() throws Exception {
    final Request req = new RqWithHeader(
        new RqFake(), "X-Takes-HitRefresh: yes"
    );
    final AtomicBoolean done = new AtomicBoolean(false);
    final Fork fork = new FkHitRefresh(
        this.temp.getRoot(),
        new Runnable() {
            @Override
            public void run() {
                done.set(true);
            }
        },
        new TkEmpty()
    );
    TimeUnit.SECONDS.sleep(2L);
    FileUtils.touch(this.temp.newFile("hey.txt"));
    MatcherAssert.assertThat(
        fork.route(req).has(),
        Matchers.is(true)
    );
    MatcherAssert.assertThat(done.get(), Matchers.is(true));
}
 
Example #2
Source File: RqMtFakeTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * Tests the bug described in #577.
 *
 * @throws Exception If there is some error inside
 */
@Test
@Ignore("See puzzle above")
public void contentDispositionShouldBeRecognized() throws Exception {
    new RqMtFake(
        new RqFake(),
        new RqWithHeader(
            new RqFake(new ListOf<>(""), ""),
            new FormattedText(
                RqMtFakeTest.CONTENT_DISP, "name=\"field1\""
            ).asString()
        ),
        new RqWithHeader(
            new RqFake("", "", "field2Value"),
            new FormattedText(
                RqMtFakeTest.CONTENT_DISP, "name=\"field2\""
            ).asString()
        )
    );
}
 
Example #3
Source File: TkFlashTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkFlash can remove a flash cookie.
 * @throws Exception If some problem inside
 */
@Test
public void removesFlashCookie() throws Exception {
    final Take take = new TkFlash(new TkEmpty());
    MatcherAssert.assertThat(
        take.act(
            new RqWithHeader(
                new RqFake(),
                "Cookie: RsFlash=Hello!"
            )
        ).head(),
        Matchers.hasItem(
            Matchers.allOf(
                Matchers.startsWith("Set-Cookie: RsFlash=deleted;"),
                Matchers.containsString("Path=/;"),
                Matchers.containsString(
                    "Expires=Thu, 01 Jan 1970 00:00:00 GMT"
                )
            )
        )
    );
}
 
Example #4
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 #5
Source File: TkReturnTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkReturn can redirect to return location
 * and remove a return cookie.
 * @throws Exception If some problem inside
 */
@Test
public void redirectsAndRemovesCookie() throws Exception {
    final Take take = new TkReturn(new TkEmpty());
    final String destination = "/return/to";
    MatcherAssert.assertThat(
        take.act(
            new RqWithHeader(
                new RqFake(),
                String.format(
                    "Cookie: RsReturn=%s;",
                    URLEncoder.encode(
                        destination,
                        Charset.defaultCharset().name()
                    )
                )
            )
        ).head(),
        Matchers.contains(
            "HTTP/1.1 303 See Other",
            String.format("Location: %s", destination),
            "Set-Cookie: RsReturn=;"
        )
    );
}
 
Example #6
Source File: TkAuthTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * 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 #7
Source File: TkAuthTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkAuth can logout a user.
 * @throws Exception If some problem inside
 */
@Test
public void logsUserOut() throws Exception {
    final Pass pass = new PsLogout();
    final Take take = Mockito.mock(Take.class);
    Mockito.doReturn(new RsText()).when(take)
        .act(Mockito.any(Request.class));
    new TkAuth(take, pass).act(
        new RqWithHeader(
            new RqFake(),
            TkAuth.class.getSimpleName(),
            "urn%3Atest%3A2"
        )
    );
    final ArgumentCaptor<Request> captor =
        ArgumentCaptor.forClass(Request.class);
    Mockito.verify(take).act(captor.capture());
    MatcherAssert.assertThat(
        new RqHeaders.Base(captor.getValue()).header(
            TkAuth.class.getSimpleName()
        ),
        Matchers.emptyIterable()
    );
}
 
Example #8
Source File: XeIdentityTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * XeIdentity can create a correct link.
 * @throws IOException If some problem inside
 */
@Test
public void generatesIdentityInXml() throws IOException {
    MatcherAssert.assertThat(
        IOUtils.toString(
            new RsXembly(
                new XeAppend(
                    "root",
                    new XeIdentity(
                        new RqWithHeader(
                            new RqFake(),
                            TkAuth.class.getSimpleName(),
                            "urn:test:1;name=Jeff"
                        )
                    )
                )
            ).body(),
            StandardCharsets.UTF_8
        ),
        XhtmlMatchers.hasXPaths(
            "/root/identity[urn='urn:test:1']",
            "/root/identity[name='Jeff']"
        )
    );
}
 
Example #9
Source File: TkSecureTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkSecure can pass on registered user.
 * @throws Exception If some problem inside
 */
@Test
public void passesOnRegisteredUser() throws Exception {
    MatcherAssert.assertThat(
        new TkSecure(
            new Take() {
                @Override
                public Response act(final Request request) {
                    return new RsEmpty();
                }
            }
        ).act(
            new RqWithHeader(
                new RqFake(),
                TkAuth.class.getSimpleName(),
                new String(
                    new CcPlain().encode(new Identity.Simple("urn:test:2"))
                )
            )
        ),
        Matchers.instanceOf(RsEmpty.class)
    );
}
 
Example #10
Source File: PsBasicTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * 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 #11
Source File: TkUploadTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
public void rendersIndexPage() throws IOException {
    new Assertion<>(
        "Must upload body",
        new RsPrint(
            new TkUpload((artifact, group) -> null).act(
                new RqMtFake(
                    new RqFake(),
                    new RqWithHeader(
                        new RqFake("POST", "/", "org.jpeek:jpeek"),
                        "Content-Disposition: form-data; name=\"coordinates\""
                    )
                )
            )
        ).printBody(),
        Matchers.startsWith("Uploaded ")
    ).affirm();
}
 
Example #12
Source File: FkAuthenticatedTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * FkAuthenticated can match by user status.
 * @throws Exception If some problem inside
 */
@Test
public void matchesIfAuthenticatedUser() throws Exception {
    MatcherAssert.assertThat(
        new FkAuthenticated(new TkEmpty()).route(
            new RqFake("GET", "/hel?a=1")
        ).has(),
        Matchers.is(false)
    );
    MatcherAssert.assertThat(
        new FkAuthenticated(new TkEmpty()).route(
            new RqWithHeader(
                new RqFake("PUT", "/hello"),
                TkAuth.class.getSimpleName(),
                new String(
                    new CcPlain().encode(new Identity.Simple("urn:test:1"))
                )
            )
        ).has(),
        Matchers.is(true)
    );
}
 
Example #13
Source File: FkTypesTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * FkTypes can match by Accept header.
 * @throws Exception If some problem inside
 */
@Test
public void matchesByAcceptHeader() throws Exception {
    final String accept = "Accept";
    MatcherAssert.assertThat(
        new FkTypes("text/xml", new RsEmpty()).route(
            new RqWithHeader(new RqFake(), accept, "*/* ")
        ).has(),
        Matchers.is(true)
    );
    MatcherAssert.assertThat(
        new FkTypes("application/json", new RsEmpty()).route(
            new RqWithHeader(new RqFake(), accept, "image/*")
        ).has(),
        Matchers.is(false)
    );
    MatcherAssert.assertThat(
        new FkTypes("*/*", new RsEmpty()).route(
            new RqWithHeader(new RqFake(), accept, "text/html")
        ).has(),
        Matchers.is(true)
    );
}
 
Example #14
Source File: TkAppTest.java    From jare with MIT License 6 votes vote down vote up
/**
 * App can render front page.
 * @throws Exception If some problem inside
 */
@Test
public void rendersHomePage() throws Exception {
    final Take take = new TkApp(new FkBase());
    MatcherAssert.assertThat(
        XhtmlMatchers.xhtml(
            new RsPrint(
                take.act(
                    new RqWithHeader(
                        new RqFake("GET", "/"),
                        // @checkstyle MultipleStringLiteralsCheck (1 line)
                        "Accept",
                        "text/xml"
                    )
                )
            ).printBody()
        ),
        XhtmlMatchers.hasXPaths(
            "/page/@date",
            "/page/@sla",
            "/page/millis",
            "/page/links/link[@rel='takes:logout']"
        )
    );
}
 
Example #15
Source File: FkContentTypeTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * FkContentType can match by Content-Type header with different types.
 * @throws Exception If some problem inside
 */
@Test
public void matchesDifferentTypes() throws Exception {
    MatcherAssert.assertThat(
        new FkContentType(
            "application/json charset=utf-8",
            FkContentTypeTest.emptyResponse()
        ).route(
            new RqWithHeader(
                new RqFake(),
                FkContentTypeTest.CONTENT_TYPE,
                "images/*"
            )
        ).has(),
        Matchers.is(false)
    );
}
 
Example #16
Source File: FkContentTypeTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * FkContentType can match by Content-Type header with identical types.
 * @throws Exception If some problem inside
 */
@Test
public void matchesIdenticalTypes() throws Exception {
    MatcherAssert.assertThat(
        new FkContentType(
            FkContentTypeTest.CTYPE,
            FkContentTypeTest.emptyResponse()
        ).route(
            new RqWithHeader(
                new RqFake(),
                FkContentTypeTest.CONTENT_TYPE,
                FkContentTypeTest.CTYPE
            )
        ).has(),
        Matchers.is(true)
    );
}
 
Example #17
Source File: FkContentTypeTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * FkContentType can match by Content-Type header with different encodings.
 * @throws Exception If some problem inside
 */
@Test
public void matchesDifferentEncodingsTypes() throws Exception {
    MatcherAssert.assertThat(
        new FkContentType(
            FkContentTypeTest.CTYPE,
            FkContentTypeTest.emptyResponse()
        ).route(
            new RqWithHeader(
                new RqFake(),
                FkContentTypeTest.CONTENT_TYPE,
                "text/html charset=utf8"
            )
        ).has(),
        Matchers.is(false)
    );
}
 
Example #18
Source File: FkEncodingTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * FkEncoding can match by Accept-Encoding header.
 * @throws IOException If some problem inside
 */
@Test
public void matchesByAcceptEncodingHeader() throws IOException {
    final String header = "Accept-Encoding";
    MatcherAssert.assertThat(
        new FkEncoding("gzip", new RsEmpty()).route(
            new RqWithHeader(new RqFake(), header, "gzip,deflate")
        ).has(),
        Matchers.is(true)
    );
    MatcherAssert.assertThat(
        new FkEncoding("", new RsEmpty()).route(
            new RqWithHeader(new RqFake(), header, "xz,gzip,exi")
        ).has(),
        Matchers.is(true)
    );
    MatcherAssert.assertThat(
        new FkEncoding("deflate", new RsEmpty()).route(
            new RqWithHeader(new RqFake(), header, "gzip,exi")
        ).has(),
        Matchers.is(false)
    );
}
 
Example #19
Source File: RqAuthTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * RqAuth can return identity.
 * @throws IOException If some problem inside
 */
@Test
public final void returnsIdentity() throws IOException {
    final Identity.Simple identity = new Identity.Simple("urn:test:1");
    MatcherAssert.assertThat(
        new RqAuth(
            new RqWithHeader(
                new RqFake(),
                TkAuth.class.getSimpleName(),
                new String(new CcPlain().encode(identity))
            )
        ).identity().urn(),
        Matchers.equalTo(identity.urn())
    );
}
 
Example #20
Source File: FkContentTypeTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * FkContentType can match by Content-Type header with empty type.
 * @throws Exception If some problem inside
 */
@Test
public void matchesEmptyType() throws Exception {
    MatcherAssert.assertThat(
        new FkContentType(
            "*/*",
            FkContentTypeTest.emptyResponse()
        ).route(
            new RqWithHeader(
                new RqFake(), FkContentTypeTest.CONTENT_TYPE, ""
            )
        ).has(), Matchers.is(true)
    );
}
 
Example #21
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 #22
Source File: FkAgentTest.java    From takes with MIT License 5 votes vote down vote up
@Test
public void matchesByVersionGreater() throws Exception {
    final String header = "User-Agent";
    final String agent = "Chrome";
    MatcherAssert.assertThat(
        new FkAgent(
            new TkEmpty(),
            // @checkstyle MagicNumber (1 line)
            new AmVersion(agent, new AmVersion.VmGreater(12))
        ).route(
            new RqWithHeader(
                new RqFake(),
                header,
                "Chrome/63.0.3239.132"
            )
        ).has(),
        Matchers.is(true)
    );
    MatcherAssert.assertThat(
        new FkAgent(
            new TkEmpty(),
            // @checkstyle MagicNumber (1 line)
            new AmVersion(agent, new AmVersion.VmGreater(90))
        ).route(
            new RqWithHeader(
                new RqFake(),
                header,
                "Chrome/41.0.2227.0"
            )
        ).has(),
        Matchers.is(false)
    );
}
 
Example #23
Source File: FkContentTypeTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * FkContentType can match by Content-Type header with any of types.
 * @throws Exception If some problem inside
 */
@Test
public void matchesWithAnyTypes() throws Exception {
    MatcherAssert.assertThat(
        new FkContentType("text/xml", new RsEmpty()).route(
            new RqWithHeader(
                new RqFake(),
                FkContentTypeTest.CONTENT_TYPE,
                "*/* "
            )
        ).has(),
        Matchers.is(true)
    );
}
 
Example #24
Source File: RqTemp.java    From takes with MIT License 5 votes vote down vote up
/**
 * Creates a {@code RqTemp} with the specified temporary file.
 * @param file The temporary that will be automatically deleted when the
 *  body of the request will be closed.
 * @throws IOException If fails
 */
RqTemp(final File file) throws IOException {
    super(
        new RqWithHeader(
            new RqLive(
                new TempInputStream(
                    Files.newInputStream(file.toPath()),
                    file
                )
            ),
            "Content-Length",
            String.valueOf(file.length())
        )
    );
}
 
Example #25
Source File: RqWithAuth.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param identity Identity
 * @param header Header name
 * @param req Request
 * @return Request
 * @throws IOException If fails
 */
private static Request make(final Identity identity, final String header,
    final Request req) throws IOException {
    return new RqWithHeader(
        req,
        header,
        new TextOf(new CcPlain().encode(identity)).asString()
    );
}
 
Example #26
Source File: XeFlashTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * XeFlash can accept RsFlash cookie.
 * @throws IOException If some problem inside
 */
@Test
public void acceptsRsFlashCookie() throws IOException {
    final Pattern pattern = Pattern.compile(
        "^Set-Cookie: RsFlash=(.*?);Path.*"
    );
    final Iterator<String> itr = new RsFlash("hello").head().iterator();
    final List<String> cookies = new ArrayList<>(0);
    while (itr.hasNext()) {
        final Matcher matcher = pattern.matcher(itr.next());
        if (matcher.find()) {
            cookies.add(matcher.group(1));
        }
    }
    MatcherAssert.assertThat(cookies, Matchers.hasSize(1));
    MatcherAssert.assertThat(
        IOUtils.toString(
            new RsXembly(
                new XeAppend(
                    "root",
                    new XeFlash(
                        new RqWithHeader(
                            new RqFake(),
                            "Cookie",
                            "RsFlash=".concat(cookies.get(0))
                        )
                    )
                )
            ).body(),
            StandardCharsets.UTF_8
        ),
        XhtmlMatchers.hasXPaths(
            "/root/flash[message='hello']",
            "/root/flash[level='INFO']"
        )
    );
}
 
Example #27
Source File: FkTypesTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * FkTypes can match by Accept header.
 * @throws Exception If some problem inside
 */
@Test
public void matchesByCompositeType() throws Exception {
    MatcherAssert.assertThat(
        new FkTypes("text/xml,text/json", new RsEmpty()).route(
            new RqWithHeader(new RqFake(), "Accept ", "text/json")
        ).has(),
        Matchers.is(true)
    );
}
 
Example #28
Source File: HmHeaderTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * HmRqHeader can test whether header name and value are available.
 * @throws Exception If some problem inside
 */
@Test
public void testsHeaderNameAndValueAvailable() throws Exception {
    MatcherAssert.assertThat(
        new RqWithHeader(new RqFake(), "header1: value1"),
        new HmHeader<>(
            "header1", "value1"
        )
    );
}
 
Example #29
Source File: HmHeaderTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * HmRqHeader can test whether header name is available
 * and value is not available.
 * @throws Exception If some problem inside
 */
@Test
public void testsValueNotAvailable() throws Exception {
    MatcherAssert.assertThat(
        new RqWithHeader(new RqFake(), "header2: value2"),
        Matchers.not(
            new HmHeader<>(
                "header2", "value21"
            )
        )
    );
}
 
Example #30
Source File: RqMtFakeTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * RqMtFake can throw exception on no name
 * at Content-Disposition header.
 * @throws Exception if there is some problem inside
 */
@Test(expected = IOException.class)
public void throwsExceptionOnNoNameAtContentDispositionHeader()
    throws Exception {
    new RqMtFake(
        new RqWithHeader(
            new RqFake("", "", "340 N Wolfe Rd, Sunnyvale, CA 94085"),
            new FormattedText(
                RqMtFakeTest.CONTENT_DISP, "fake=\"t-3\""
            ).asString()
        )
    );
}