List of usage examples for org.springframework.mock.web MockHttpServletResponse getStatus
@Override public int getStatus()
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 {//from w ww .j a v 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(); 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 getServiceAccessTokenWithWrongGrantTypeTest() { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); req.addParameter("client_id", serviceApplication.getExternalId()); req.addParameter("client_secret", serviceApplication.getSecret()); req.addParameter("grant_type", "authorization_code"); req.setMethod("POST"); req.setPathInfo("/access_token"); try {// www .ja v a 2 s .c o m 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 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. j a v a 2 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-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 {/* www . j a v a 2s. 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); 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 . j av 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.fenixedu.bennu.oauth.OAuthServletTest.java
@Test public void getServiceAccessTokenHeaderEmptyTest() { MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse res = new MockHttpServletResponse(); Authenticate.unmock();//www.j av a 2 s. c o 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.openmrs.module.atomfeed.web.AtomFeedDownloadServletTest.java
/** * @see AtomFeedDownloadServlet#doHead(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) * @verifies send not modified error if atom feed has not changed *///from w w w. j a va2s.com @Test public void doHead_shouldSendNotModifiedErrorIfAtomFeedHasNotChanged() throws Exception { // create servlet and corresponding request and response object to be sent AtomFeedDownloadServlet atomFeedDownloadServlet = new AtomFeedDownloadServlet(); MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/atomfeed"); request.setContextPath("/somecontextpath"); MockHttpServletResponse response = new MockHttpServletResponse(); // intentionally change atom feed in order to not depend from other tests AtomFeedUtil.objectCreated(new Encounter()); // read atom feed header specific information from the header file String headerFileContent = AtomFeedUtil.readFeedHeaderFile(); int contentLength = 0; String etagToken = ""; Date lastModified = null; if (StringUtils.isNotBlank(headerFileContent)) { contentLength = headerFileContent.length() + Integer .valueOf(StringUtils.substringBetween(headerFileContent, "<entriesSize>", "</entriesSize>")); etagToken = StringUtils.substringBetween(headerFileContent, "<versionId>", "</versionId>"); try { lastModified = new SimpleDateFormat(AtomFeedUtil.RFC_3339_DATE_FORMAT) .parse(StringUtils.substringBetween(headerFileContent, "<updated>", "</updated>")); } catch (ParseException e) { // ignore it here } } // set request headers request.addHeader("If-None-Match", '"' + etagToken + '"'); request.addHeader("If-Modified-Since", lastModified); atomFeedDownloadServlet.service(request, response); // check response headers Assert.assertEquals(contentLength, response.getContentLength()); Assert.assertEquals("application/atom+xml", response.getContentType()); Assert.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus()); Assert.assertNotNull(response.getHeader("Last-Modified")); }
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 {/*w w w . ja va 2s .c o m*/ 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();/*from w w w .j a va2s.c o m*/ 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:com.github.mike10004.stormpathacctmgr.NewPasswordFormServletTest.java
@Test public void testDoGet() throws Exception { System.out.println("NewPasswordFormServletTest.testDoGet"); String emailAddress = "somebody@example.com"; String token = "981ng9014ng4nh9014h901nh4jhqg8rejg089rjg09zahg49hqg08"; final Application application = createNiceMock(Application.class); Account account = createNiceMock(Account.class); final RequestDispatcher requestDispatcher = createNiceMock(RequestDispatcher.class); MockHttpServletResponse mockResponse = new MockHttpServletResponse(); MockHttpServletRequest mockRequest = new MockHttpServletRequest() { @Override// w ww . j av a 2s . c o m public RequestDispatcher getRequestDispatcher(String path) { assertEquals(NewPasswordFormServlet.RESET_ENTER_NEW_PASSWORD_JSP_PATH, path); return requestDispatcher; } }; mockRequest.setParameter(PasswordReset.PARAM_RESET_TOKEN, token); requestDispatcher.forward(mockRequest, mockResponse); EasyMock.expectLastCall().times(1); expect(account.getEmail()).andReturn(emailAddress).anyTimes(); expect(application.verifyPasswordResetToken(token)).andReturn(account).times(1); replay(application, account, requestDispatcher); final Stormpaths stormpaths = new Stormpaths() { @Override public Application buildApplication() { return application; } }; NewPasswordFormServlet instance = new NewPasswordFormServlet() { @Override protected Stormpaths createStormpaths() { return stormpaths; } }; MockServletContext servletContext = new MockServletContext(); instance.init(new MockServletConfig(servletContext)); instance.doGet(mockRequest, mockResponse); String actualTargetEmailAttribute = (String) mockRequest .getAttribute(NewPasswordFormServlet.ATTR_TARGET_EMAIL); System.out.println("email attribute: " + actualTargetEmailAttribute); assertEquals(emailAddress, actualTargetEmailAttribute); System.out.println("status: " + mockResponse.getStatus()); assertEquals(HttpServletResponse.SC_OK, mockResponse.getStatus()); }