DefaultSavedRequestHandler implements SavedRequestHandler { @Override public HttpAction restore(final WebContext context, final String defaultUrl) { final Optional<Object> optRequestedUrl = context.getSessionStore().get(context, Pac4jConstants.REQUESTED_URL); HttpAction requestedAction = null; if (optRequestedUrl.isPresent()) { context.getSessionStore().set(context, Pac4jConstants.REQUESTED_URL, ""); final Object requestedUrl = optRequestedUrl.get(); if (requestedUrl instanceof FoundAction) { requestedAction = (FoundAction) requestedUrl; } else if (requestedUrl instanceof OkAction) { requestedAction = (OkAction) requestedUrl; } } if (requestedAction == null) { requestedAction = new FoundAction(defaultUrl); } LOGGER.debug("requestedAction: {}", requestedAction.getMessage()); if (requestedAction instanceof FoundAction) { return RedirectionActionHelper.buildRedirectUrlAction(context, ((FoundAction) requestedAction).getLocation()); } else { return RedirectionActionHelper.buildFormPostContentAction(context, ((OkAction) requestedAction).getContent()); } }  @Override void save(final WebContext context); @Override HttpAction restore(final WebContext context, final String defaultUrl);  }
@Test public void testRestoreNoRequestedUrl() { final MockWebContext context = MockWebContext.create(); final HttpAction action = handler.restore(context, LOGIN_URL); assertTrue(action instanceof FoundAction); assertEquals(LOGIN_URL, ((FoundAction) action).getLocation()); assertFalse(context.getSessionStore().get(context, Pac4jConstants.REQUESTED_URL).isPresent()); }
@Test public void testRestoreEmptyString() { final MockWebContext context = MockWebContext.create(); context.getSessionStore().set(context, Pac4jConstants.REQUESTED_URL, ""); final HttpAction action = handler.restore(context, LOGIN_URL); assertTrue(action instanceof FoundAction); assertEquals(LOGIN_URL, ((FoundAction) action).getLocation()); assertEquals("", context.getSessionStore().get(context, Pac4jConstants.REQUESTED_URL).get()); }
@Test public void testRestoreFoundAction() { final MockWebContext context = MockWebContext.create(); context.getSessionStore().set(context, Pac4jConstants.REQUESTED_URL, new FoundAction(PAC4J_URL)); final HttpAction action = handler.restore(context, LOGIN_URL); assertTrue(action instanceof FoundAction); assertEquals(PAC4J_URL, ((FoundAction) action).getLocation()); assertEquals("", context.getSessionStore().get(context, Pac4jConstants.REQUESTED_URL).get()); }
@Test public void testRestoreFoundActionAfterPost() { final MockWebContext context = MockWebContext.create(); context.setRequestMethod("POST"); context.getSessionStore().set(context, Pac4jConstants.REQUESTED_URL, new FoundAction(PAC4J_URL)); final HttpAction action = handler.restore(context, LOGIN_URL); assertTrue(action instanceof SeeOtherAction); assertEquals(PAC4J_URL, ((SeeOtherAction) action).getLocation()); assertEquals("", context.getSessionStore().get(context, Pac4jConstants.REQUESTED_URL).get()); }
@Test public void testRestoreOkAction() { final MockWebContext context = MockWebContext.create().setFullRequestURL(PAC4J_URL).addRequestParameter(KEY, VALUE); final String formPost = RedirectionActionHelper.buildFormPostContent(context); context.getSessionStore().set(context, Pac4jConstants.REQUESTED_URL, new OkAction(formPost)); final HttpAction action = handler.restore(context, LOGIN_URL); assertTrue(action instanceof OkAction); assertEquals(FORM_DATA, ((OkAction) action).getContent()); assertEquals("", context.getSessionStore().get(context, Pac4jConstants.REQUESTED_URL).get()); }
@Test public void testRestoreOkActionAfterPost() { final MockWebContext context = MockWebContext.create().setFullRequestURL(PAC4J_URL).addRequestParameter(KEY, VALUE); final String formPost = RedirectionActionHelper.buildFormPostContent(context); context.setRequestMethod("POST"); context.getSessionStore().set(context, Pac4jConstants.REQUESTED_URL, new OkAction(formPost)); final HttpAction action = handler.restore(context, LOGIN_URL); assertTrue(action instanceof TemporaryRedirectAction); assertEquals(FORM_DATA, ((TemporaryRedirectAction) action).getContent()); assertEquals("", context.getSessionStore().get(context, Pac4jConstants.REQUESTED_URL).get()); }