Package com.github.kevinsawicki.http

Examples of com.github.kevinsawicki.http.HttpRequest$CloseOperation


    }

    @Test
    public void should_access_secured_resource_with_http_basic() throws Exception {
        HttpTestClient client = server.client();
        HttpRequest httpRequest = client.GET("/api/security/user")
                .basic("admin", Hashing.md5().hashString("juma", Charsets.UTF_8).toString());
        assertThat(httpRequest.code()).isEqualTo(200);
        assertThat(httpRequest.headers("Set-Cookie")[1]).contains("\"principal\":\"admin\"");
        assertThat(httpRequest.body().trim()).isEqualTo("admin");
    }
View Full Code Here


    }

    @Test
    public void should_not_access_secured_resource_with_http_basic_when_deactivated() throws Exception {
        threadLocal().set(activationKey(HttpAuthenticationFilter.class, "HttpAuthenticationFilter"), "false");
        HttpRequest httpRequest = server.client().GET("/api/security/user")
                .basic("admin", Hashing.md5().hashString("juma", Charsets.UTF_8).toString());
        assertThat(httpRequest.code()).isEqualTo(401);
    }
View Full Code Here

    }

    @Test
    public void should_share_threadlocal_components_with_server() throws Exception {
        // first we try the default implementation of the ClientAffinityResource
        HttpRequest httpRequest = server.client().GET("/api/clientAffinity");
        assertThat(httpRequest.code()).isEqualTo(200);
        assertThat(httpRequest.body().trim()).isEqualTo("NONE");

        // now we provide a component in thread local, which should be used on the server for this test
        overrideComponents().set(ClientAffinityResource.COMPONENT_NAME, "myvalue");
        httpRequest = server.client().GET("/api/clientAffinity");
        assertThat(httpRequest.code()).isEqualTo(200);
        assertThat(httpRequest.body().trim()).isEqualTo("myvalue");
    }
View Full Code Here

    public void should_access_secured_resource_with_http_basic_no_session() throws Exception {
        threadLocal()
                .set(activationKey(RestxSessionCookieFilter.class, "RestxSessionCookieFilter"), "false")
                .set(activationKey(RestxSessionBareFilter.class, "RestxSessionBareFilter"), "true");
        HttpTestClient client = server.client();
        HttpRequest httpRequest = client.GET("/api/security/user")
                .basic("admin", Hashing.md5().hashString("juma", Charsets.UTF_8).toString());
        assertThat(httpRequest.code()).isEqualTo(200);
        assertThat(httpRequest.header("Set-Cookie")).isNull();
        assertThat(httpRequest.body().trim()).isEqualTo("admin");

        httpRequest = client.GET("/api/security/user");
        assertThat(httpRequest.code()).isEqualTo(401);
    }
View Full Code Here

        Factory.LocalMachines.threadLocal().clear();
    }

    @Test
    public void should_return_hello() throws Exception {
        HttpRequest httpRequest = server.client().authenticatedAs("admin").GET(
                "/api/core/hello?who=restx");
        assertThat(httpRequest.code()).isEqualTo(200);
        assertThat(httpRequest.body().trim()).isEqualTo("hello restx");
    }
View Full Code Here

        assertThat(httpRequest.body().trim()).isEqualTo("hello restx");
    }

    @Test
    public void should_return_hello_msg() throws Exception {
        HttpRequest httpRequest = server.client().authenticatedAs("admin").GET(
                "/api/core/hellomsg?who=restx");
        assertThat(httpRequest.code()).isEqualTo(200);
        assertThat(httpRequest.body().trim()).isEqualTo("{\n  \"msg\" : \"hello restx\"\n}");
    }
View Full Code Here

        assertThat(httpRequest.body().trim()).isEqualTo("{\n  \"msg\" : \"hello restx\"\n}");
    }

    @Test
    public void should_post_hello() throws Exception {
        HttpRequest httpRequest = server.client().authenticatedAs("admin").POST(
                "/api/core/hellomsg").send("{\"msg\": \"restx\"}");
        assertThat(httpRequest.code()).isEqualTo(200);
        assertThat(httpRequest.body().trim()).isEqualTo("{\n  \"msg\" : \"hello restx\"\n}");
    }
View Full Code Here

        HttpTestClient httpTestClient = HttpTestClient.withBaseUrl(baseUrl);
        for (Map.Entry<String, String> cookie : when.getCookies().entrySet()) {
            httpTestClient = httpTestClient.withCookie(cookie.getKey(), cookie.getValue());
        }

        HttpRequest httpRequest = httpTestClient.http(when.getMethod(), when.getPath());

        ImmutableMap<String, String> cookies = when.getCookies();
        if (!cookies.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : cookies.entrySet()) {
                sb.append(entry.getKey()).append("=\"").append(entry.getValue().replace("\"", "\\\"")).append("\"; ");
            }
            sb.setLength(sb.length() - 2);
            httpRequest.header("Cookie", sb.toString());
        }

        if (!Strings.isNullOrEmpty(when.getBody())) {
            httpRequest.contentType("application/json");
            httpRequest.send(when.getBody());
            System.out.println(when.getBody());
        }
        System.out.println();

        int code = httpRequest.code();
        System.out.println("<< RESPONSE");
        System.out.println(code);
        System.out.println();
        String body = httpRequest.body(Charsets.UTF_8.name());
        System.out.println(body);
        System.out.println();

        assertThat(code).isEqualTo(when.getThen().getExpectedCode());
        if (isJSON(when.getThen().getExpected())) {
View Full Code Here

    @Test
    public void should_lifecycle_hello() throws Exception {
        LifecycleListenerFilter filter = new LifecycleListenerFilter();
        Factory.LocalMachines.overrideComponents().set("LifecycleListenerFilter", filter);
        HttpRequest httpRequest = server.client().authenticatedAs("admin").GET(
                "/api/core/hello?who=restx");

        assertThat(httpRequest.code()).isEqualTo(200);
        assertThat(httpRequest.body().trim()).isEqualTo("hello restx");

        assertLifecycleMethods(filter, "[Optional.absent()]", "[Optional.of(hello restx)]");
    }
View Full Code Here

    @Test
    public void should_lifecycle_hello_msg() throws Exception {
        LifecycleListenerFilter filter = new LifecycleListenerFilter();
        Factory.LocalMachines.overrideComponents().set("LifecycleListenerFilter", filter);
        HttpRequest httpRequest = server.client().authenticatedAs("admin").GET(
                "/api/core/hellomsg?who=restx");

        assertThat(httpRequest.code()).isEqualTo(200);
        assertThat(httpRequest.body().trim()).isEqualTo("{\n  \"msg\" : \"hello restx\"\n}");

        assertLifecycleMethods(filter, "[Optional.absent()]", "[Optional.of(Message{msg='hello restx'})]");
    }
View Full Code Here

TOP

Related Classes of com.github.kevinsawicki.http.HttpRequest$CloseOperation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.