Example usage for org.springframework.mock.web MockHttpServletRequest setPathInfo

List of usage examples for org.springframework.mock.web MockHttpServletRequest setPathInfo

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest setPathInfo.

Prototype

public void setPathInfo(@Nullable String pathInfo) 

Source Link

Usage

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void getServiceAccessTokenTest() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();

    req.addParameter("client_id", serviceApplication.getExternalId());
    req.addParameter("client_secret", serviceApplication.getSecret());
    req.addParameter("grant_type", "client_credentials");
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {/*ww w  .j  a  v  a 2s.  co m*/
        oauthServlet.service(req, res);

        Assert.assertEquals("must return status OK", 200, res.getStatus());

        String tokenJson = res.getContentAsString();

        final JsonObject token = new JsonParser().parse(tokenJson).getAsJsonObject();

        Assert.assertTrue("response must be a valid json and have access_token field",
                token.has(ACCESS_TOKEN) && token.get(ACCESS_TOKEN).getAsString().length() > 0);

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void testServiceOnlyEndpoint() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();

    req.addParameter("client_id", serviceApplication.getExternalId());
    req.addParameter("client_secret", serviceApplication.getSecret());
    req.addParameter("grant_type", "client_credentials");
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {/*from w  w w.ja  va 2 s .com*/
        oauthServlet.service(req, res);

        Assert.assertEquals("must return status OK", 200, res.getStatus());

        String tokenJson = res.getContentAsString();

        final JsonObject token = new JsonParser().parse(tokenJson).getAsJsonObject();
        final String accessToken = token.get(ACCESS_TOKEN).getAsString();

        Assert.assertTrue("response must be a valid json and have access_token field",
                token.has(ACCESS_TOKEN) && accessToken.length() > 0);

        String result = target("bennu-oauth").path("test").path("service-only-without-scope")
                .queryParam(ACCESS_TOKEN, accessToken).request().get(String.class);
        Assert.assertEquals("this is an endpoint with serviceOnly", result);

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void testServiceOnlyEndpointWithScopeMustFail() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();

    req.addParameter("client_id", serviceApplication.getExternalId());
    req.addParameter("client_secret", serviceApplication.getSecret());
    req.addParameter("grant_type", "client_credentials");
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {//  w ww.j  av a  2s  .  com
        oauthServlet.service(req, res);

        Assert.assertEquals("must return status OK", 200, res.getStatus());

        String tokenJson = res.getContentAsString();

        final JsonObject token = new JsonParser().parse(tokenJson).getAsJsonObject();
        final String accessToken = token.get(ACCESS_TOKEN).getAsString();

        Assert.assertTrue("response must be a valid json and have access_token field",
                token.has(ACCESS_TOKEN) && accessToken.length() > 0);

        Response result = target("bennu-oauth").path("test").path("service-only-with-scope")
                .queryParam(ACCESS_TOKEN, accessToken).request().get(Response.class);

        Assert.assertNotEquals("request must fail", 200, result.getStatus());

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void testServiceOnlyWithScopeEndpoint() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();

    req.addParameter("client_id", serviceApplicationWithScope.getExternalId());
    req.addParameter("client_secret", serviceApplicationWithScope.getSecret());
    req.addParameter("grant_type", "client_credentials");
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {/*  w  w  w . ja v a2 s.  c o  m*/
        oauthServlet.service(req, res);

        Assert.assertEquals("must return status OK", 200, res.getStatus());

        String tokenJson = res.getContentAsString();

        final JsonObject token = new JsonParser().parse(tokenJson).getAsJsonObject();
        final String accessToken = token.get(ACCESS_TOKEN).getAsString();

        Assert.assertTrue("response must be a valid json and have access_token field",
                token.has(ACCESS_TOKEN) && accessToken.length() > 0);

        String result = target("bennu-oauth").path("test").path("service-only-with-scope")
                .queryParam(ACCESS_TOKEN, accessToken).request().get(String.class);
        Assert.assertEquals("this is an endpoint with SERVICE scope, serviceOnly", result);

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.n52.sos.service.it.MockHttpClient.java

private MockHttpServletRequest build() {
    try {/*w  w  w . ja v a  2s . c om*/
        final MockHttpServletRequest req = new MockHttpServletRequest(context);
        req.setMethod(method);
        for (String header : headers.keySet()) {
            for (String value : headers.get(header)) {
                req.addHeader(header, value);
            }
        }
        final StringBuilder queryString = new StringBuilder();
        if (query != null && !query.isEmpty()) {
            boolean first = true;
            for (String key : query.keySet()) {
                final Set<String> values = query.get(key);
                req.addParameter(key, values.toArray(new String[values.size()]));
                if (first) {
                    queryString.append("?");
                    first = false;
                } else {
                    queryString.append("&");
                }
                queryString.append(key).append("=");
                Iterator<String> i = values.iterator();
                queryString.append(i.next());
                while (i.hasNext()) {
                    queryString.append(",").append(i.next());
                }
            }
            req.setQueryString(queryString.toString());
        }
        req.setRequestURI(path + queryString.toString());
        if (path == null) {
            path = "/";
        }
        req.setPathInfo(path);
        if (content != null) {
            req.setContent(content.getBytes(MockHttpExecutor.ENCODING));
        }
        return req;
    } catch (UnsupportedEncodingException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void getServiceAccessTokenHeaderEmptyTest() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    Authenticate.unmock();/*  w  ww .  j  av  a 2 s .co m*/
    String clientSecret = "";

    req.addHeader(HttpHeaders.AUTHORIZATION,
            "Basic " + Base64.getEncoder().encodeToString(clientSecret.getBytes(StandardCharsets.UTF_8)));
    req.addParameter(GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS);
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {
        oauthServlet.service(req, res);
        Assert.assertEquals("must return BAD_REQUEST", 400, res.getStatus());

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void getServiceAccessTokenWithWrongClientSecretTest() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();

    req.addParameter("client_id", serviceApplication.getExternalId());
    req.addParameter("client_secret",
            BaseEncoding.base64().encode((serviceApplication.getExternalId() + ":lasdlkasldksladkalskdsal")
                    .getBytes(StandardCharsets.UTF_8)));
    req.addParameter("grant_type", "client_credentials");
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {//from w  ww  .j a va  2 s.c  om
        oauthServlet.service(req, res);

        Assert.assertEquals("must return status BAD_REQUEST", 400, res.getStatus());

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void getServiceAccessTokenHeaderTest() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    Authenticate.unmock();/* w  ww.jav  a2  s.com*/
    String clientSecret = serviceApplication.getExternalId() + ":" + serviceApplication.getSecret();
    req.addHeader(HttpHeaders.AUTHORIZATION,
            "Basic " + Base64.getEncoder().encodeToString(clientSecret.getBytes(StandardCharsets.UTF_8)));
    req.addParameter(GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS);
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {
        oauthServlet.service(req, res);

        Assert.assertEquals("must return status OK", 200, res.getStatus());

        String tokenJson = res.getContentAsString();

        final JsonObject token = new JsonParser().parse(tokenJson).getAsJsonObject();

        Assert.assertTrue("response must be a valid json and have access_token field",
                token.has(ACCESS_TOKEN) && token.get(ACCESS_TOKEN).getAsString().length() > 0);

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void testServiceApplicationWithUnexistingScope() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    Authenticate.unmock();/*w  w  w.  jav a  2 s .c om*/

    User user = createUser("testServiceApplicationWithUnexistingScope", "John", "Doe", "John Doe",
            "john.doe@fenixedu.org");

    ServiceApplication serviceApplication = new ServiceApplication();
    serviceApplication.setAuthor(user);

    req.addParameter("client_id", serviceApplication.getExternalId());
    req.addParameter("client_secret", serviceApplication.getSecret());
    req.addParameter("grant_type", "client_credentials");
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {
        oauthServlet.service(req, res);

        Assert.assertEquals("must return status OK", 200, res.getStatus());

        String tokenJson = res.getContentAsString();

        final String serviceAccessToken = new JsonParser().parse(tokenJson).getAsJsonObject()
                .get("access_token").getAsString();

        Response response = target("bennu-oauth").path("test").path("service-only-with-unexisting-scope")
                .queryParam("access_token", serviceAccessToken).request().get();

        Assert.assertNotEquals("request must fail since scope does not exist", 200, response.getStatus());

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    }
}

From source file:org.fenixedu.bennu.oauth.OAuthServletTest.java

@Test
public void testServiceApplicationOAuthAccessProvider() {
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    Authenticate.unmock();/*from   w w  w  .j  av a2s . c o m*/

    User user = createUser("testServiceApplicationOAuthAccessProvider", "John", "Doe", "John Doe",
            "john.doe@fenixedu.org");

    ServiceApplication serviceApplication = new ServiceApplication();
    serviceApplication.setAuthor(user1);
    serviceApplication.addScopes(serviceApplicationOAuthAccessProvider);
    serviceApplication.addScopes(loggedScope);

    req.addParameter("client_id", serviceApplication.getExternalId());
    req.addParameter("client_secret", serviceApplication.getSecret());
    req.addParameter("grant_type", "client_credentials");
    req.setMethod("POST");
    req.setPathInfo("/access_token");

    try {
        oauthServlet.service(req, res);

        Assert.assertEquals("must return status OK", 200, res.getStatus());

        String tokenJson = res.getContentAsString();

        final String serviceAccessToken = new JsonParser().parse(tokenJson).getAsJsonObject()
                .get("access_token").getAsString();

        String result = target("oauth").path("provider").path(serviceApplication.getExternalId())
                .path(user.getUsername()).queryParam("access_token", serviceAccessToken).request()
                .post(null, String.class);

        Authenticate.unmock();

        final String userAccessToken = new JsonParser().parse(result).getAsJsonObject().get("access_token")
                .getAsString();

        result = target("bennu-oauth").path("test").path("test-scope-with-logged-user")
                .queryParam("access_token", userAccessToken).request().get(String.class);

        Assert.assertEquals("this is an endpoint with TEST scope: testServiceApplicationOAuthAccessProvider",
                result);

        Authenticate.mock(user);

        JsonArray authorizations = target("bennu-oauth").path("authorizations").request().get(JsonElement.class)
                .getAsJsonArray();

        Assert.assertEquals("no authorizations because it is a service application", 0, authorizations.size());

    } catch (ServletException | IOException e) {
        Assert.fail(e.getMessage());
    } finally {
        serviceApplication.removeScope(serviceApplicationOAuthAccessProvider);
        serviceApplication.removeScope(loggedScope);
    }

}