org.takes.http.FtRemote Java Examples

The following examples show how to use org.takes.http.FtRemote. 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: 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 rendersHomePageViaHttp() throws Exception {
    final Take app = new TkApp(new FkBase());
    new FtRemote(app).exec(
        home -> {
            new JdkRequest(home)
                .fetch()
                .as(RestResponse.class)
                .assertStatus(HttpURLConnection.HTTP_OK)
                .as(XmlResponse.class)
                .assertXPath("/xhtml:html");
            new JdkRequest(home)
                .through(VerboseWire.class)
                .header("Accept", "application/xml")
                .fetch()
                .as(RestResponse.class)
                .assertStatus(HttpURLConnection.HTTP_OK)
                .as(XmlResponse.class)
                .assertXPath("/page/version");
        }
    );
}
 
Example #2
Source File: TakesAppIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenTake_whenRunRemoteServer_thenRespond() throws Exception {
    new FtRemote(new TakesContact()).exec(
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                HttpClient client = HttpClientBuilder.create().build();    
                HttpResponse response = client.execute(new HttpGet(home));
                int statusCode = response.getStatusLine().getStatusCode();
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity);
                
                assertEquals(200, statusCode);
                assertEquals("Contact us at https://www.baeldung.com", result);
            }
        });
}
 
Example #3
Source File: TkSlf4jRemoteTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * TkSlf4j can return an empty response body for {@link TkEmpty}.
 * @throws Exception if some I/O problem occurred.
 */
@Ignore
@Test
public void returnsAnEmptyResponseBody() throws Exception {
    new FtRemote(
        new TkSlf4j(new TkEmpty())
    ).exec(
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                new JdkRequest(home)
                    .method("POST")
                    .body().set("returnsAnEmptyResponseBody").back()
                    .fetch()
                    .as(RestResponse.class)
                    .assertBody(new IsEqual<>(""))
                    .assertStatus(HttpURLConnection.HTTP_NO_CONTENT);
            }
        }
    );
}
 
Example #4
Source File: TkProxyTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * 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 #5
Source File: AppTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * App can work.
 * @throws Exception If some problem inside
 */
@Test
public void justWorks() throws Exception {
    final File dir = this.temp.newFolder();
    FileUtils.write(new File(dir, "hello.txt"), "hello, world!");
    new FtRemote(new App(dir)).exec(
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                new JdkRequest(home)
                    .uri().path("/f").back()
                    .through(VerboseWire.class)
                    .fetch()
                    .as(RestResponse.class)
                    .assertStatus(HttpURLConnection.HTTP_OK)
                    .as(XmlResponse.class)
                    .assertXPath("//xhtml:li[.='hello.txt: 13']");
            }
        }
    );
}
 
Example #6
Source File: TkAppTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
public void rendersOneReport(@TempDir final Path temp) throws Exception {
    final Take app = new TkApp(temp);
    new FtRemote(app).exec(
        home -> {
            new JdkRequest(home)
                .uri().path("org.jpeek")
                .path("jpeek")
                .path("index.html").back()
                .fetch()
                .as(RestResponse.class)
                .assertStatus(HttpURLConnection.HTTP_NOT_FOUND);
            new JdkRequest(String.format("%s/org.jpeek/jpeek/", home))
                .fetch()
                .as(RestResponse.class)
                .assertStatus(HttpURLConnection.HTTP_SEE_OTHER);
            new JdkRequest(String.format("%s/org.jpeek/jpeek", home))
                .fetch()
                .as(RestResponse.class)
                .assertStatus(HttpURLConnection.HTTP_SEE_OTHER);
        }
    );
}
 
Example #7
Source File: HtTimedWireTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@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: HtAutoRedirectTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@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 #9
Source File: HtKeepAliveResponseTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@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 #10
Source File: HtUpgradeWireTest.java    From cactoos-http with MIT License 6 votes vote down vote up
/**
 * 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 #11
Source File: HtUpgradeWireTest.java    From cactoos-http with MIT License 6 votes vote down vote up
/**
 * 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 #12
Source File: HtResponseTest.java    From cactoos-http with MIT License 5 votes vote down vote up
@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 #13
Source File: HtKeepAliveResponseTest.java    From cactoos-http with MIT License 5 votes vote down vote up
@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 #14
Source File: TkReadAlwaysTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * Send a request with body which is ignored.
 * @throws Exception If there are problems
 */
@Test
public void requestBodyIsIgnored() throws Exception {
    final String expected = "response ok";
    final Take take = new Take() {
        @Override
        public Response act(final Request req) throws IOException {
            return new RsText(expected);
        }
    };
    new FtRemote(new TkReadAlways(take)).exec(
        new FtRemote.Script() {
            @Override
            public void exec(final URI home) throws IOException {
                new JdkRequest(home)
                    .method("POST").header(
                        "Content-Type", "application/x-www-form-urlencoded"
                    ).body()
                    .formParam("name", "Jeff Warraby")
                    .formParam("age", "4")
                    .back()
                    .fetch()
                    .as(RestResponse.class)
                    .assertStatus(HttpURLConnection.HTTP_OK)
                    .assertBody(Matchers.equalTo(expected));
            }
        }
    );
}
 
Example #15
Source File: TkProxyTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * 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 #16
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 #17
Source File: TkMethodsTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * TkMethods can return 405 status when acting on unknown method.
 * @throws Exception If any I/O error occurs
 */
@Test
public void returnsMethodIsNotAllowedForUnsupportedMethods() throws
    Exception {
    new FtRemote(new TkMethods(new TkEmpty(), RqMethod.PUT)).exec(
        url -> new JdkRequest(url)
            .method(RqMethod.POST)
            .fetch().as(RestResponse.class)
            .assertStatus(HttpURLConnection.HTTP_BAD_METHOD)
    );
}
 
Example #18
Source File: HtAutoClosedResponseTest.java    From cactoos-http with MIT License 5 votes vote down vote up
@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 #19
Source File: TkRelayTest.java    From jare with MIT License 5 votes vote down vote up
/**
 * 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: 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 #21
Source File: HtResponseTest.java    From cactoos-http with MIT License 5 votes vote down vote up
@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 #22
Source File: InputOfTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void readsRealUrl() throws IOException {
    new FtRemote(new TkHtml("<html>How are you?</html>")).exec(
        home -> new Assertion<>(
            "must fetch bytes from the URL",
            new TextOf(
                new InputOf(home)
            ),
            new MatchesRegex("<html.*html>")
        ).affirm()
    );
}
 
Example #23
Source File: HtWireTest.java    From cactoos-http with MIT License 5 votes vote down vote up
@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 #24
Source File: HtWireTest.java    From cactoos-http with MIT License 5 votes vote down vote up
@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 #25
Source File: HtSecureWireTest.java    From cactoos-http with MIT License 5 votes vote down vote up
/**
 * Creates an instance of secure Front.
 * @param take Take
 * @return FtRemote Front
 * @throws Exception If fails
 */
private static FtRemote secure(final Take take, final int port)
    throws Exception {
    final ServerSocket skt = SSLServerSocketFactory.getDefault()
        .createServerSocket(port);
    return new FtRemote(new BkBasic(take), skt);
}
 
Example #26
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 #27
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 #28
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 #29
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)
                );
            }
        }
    );
}
 
Example #30
Source File: TkProxyTest.java    From takes with MIT License 4 votes vote down vote up
/**
 * 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()
                        )
                    )
                );
            }
        }
    );
}