@Override
 public void handle(MvcResult result) throws Exception {
   @SuppressWarnings("unchecked")
   Map<String, Object> configuration =
       (Map<String, Object>) result.getRequest().getAttribute(ATTRIBUTE_NAME_CONFIGURATION);
   this.delegate.handle(result.getRequest(), result.getResponse(), configuration);
 }
  @Test
  public void testResetPassword() throws Exception {
    String username = "******";
    User user = userManager.getUserByUsername(username);
    String token = userManager.generateRecoveryToken(user);
    String password = "******";

    Wiser wiser = startWiser(getSmtpPort());

    ResultActions update =
        mockMvc
            .perform(
                post("/updatePassword")
                    .param("username", username)
                    .param("token", token)
                    .param("password", password))
            .andExpect(status().is3xxRedirection())
            .andExpect(redirectedUrl("/"));

    wiser.stop();
    assertTrue(wiser.getMessages().size() == 1);

    MvcResult result = update.andReturn();
    MockHttpSession session = (MockHttpSession) result.getRequest().getSession();
    assertNotNull(session.getAttribute(BaseFormController.MESSAGES_KEY));
    assertNull(session.getAttribute(BaseFormController.ERRORS_MESSAGES_KEY));
  }
 @Test
 public void correctlyRecordsMetricsForFailedDeferredResultResponse() throws Exception {
   AnnotationConfigApplicationContext context =
       new AnnotationConfigApplicationContext(Config.class, MetricFilterAutoConfiguration.class);
   MetricsFilter filter = context.getBean(MetricsFilter.class);
   CountDownLatch latch = new CountDownLatch(1);
   MockMvc mvc =
       MockMvcBuilders.standaloneSetup(new MetricFilterTestController(latch))
           .addFilter(filter)
           .build();
   String attributeName = MetricsFilter.class.getName() + ".StopWatch";
   MvcResult result =
       mvc.perform(post("/createFailure"))
           .andExpect(status().isOk())
           .andExpect(request().asyncStarted())
           .andExpect(request().attribute(attributeName, is(notNullValue())))
           .andReturn();
   latch.countDown();
   try {
     mvc.perform(asyncDispatch(result));
     fail();
   } catch (Exception ex) {
     assertThat(result.getRequest().getAttribute(attributeName)).isNull();
     verify(context.getBean(CounterService.class)).increment("status.500.createFailure");
   } finally {
     context.close();
   }
 }
 @Test
 public void testZip2() throws Exception {
   MvcResult mvcResult =
       mockMvc
           .perform(post("/").param("addr", "花蓮縣鳳林鎮信義路249號"))
           .andExpect(request().asyncStarted())
           .andExpect(request().asyncStarted())
           .andExpect(request().asyncResult(is(not(isEmptyOrNullString()))))
           .andReturn();
   List<Post5> zips = (List<Post5>) mvcResult.getRequest().getAttribute("zips");
   assertThat("Error zip.", zips, is(hasSize(2)));
 }
 @Test(dataProvider = "addrs")
 public void testZip1(String zipcode, String addr) throws Exception {
   logger.debug("測試{}:{}", zipcode, addr);
   MvcResult mvcResult =
       mockMvc
           .perform(post("/").param("addr", addr))
           .andExpect(request().asyncStarted())
           .andExpect(request().asyncStarted())
           .andExpect(request().asyncResult(is(not(isEmptyOrNullString()))))
           .andReturn();
   List<Post5> zips = (List<Post5>) mvcResult.getRequest().getAttribute("zips");
   assertThat("Error zip.", zips, is(hasSize(1)));
   assertThat("Error zip.", zips.get(0).getZipcode(), is(equalTo(zipcode)));
 }
  @Test
  public void testShowResetPasswordFormBadToken() throws Exception {
    String username = "******";
    String badtoken = RandomStringUtils.random(32);

    ResultActions update =
        mockMvc
            .perform(get("/updatePassword").param("username", username).param("token", badtoken))
            .andExpect(status().is3xxRedirection())
            .andExpect(redirectedUrl("/"));

    MvcResult result = update.andReturn();
    MockHttpSession session = (MockHttpSession) result.getRequest().getSession();
    assertNotNull(session.getAttribute(BaseFormController.ERRORS_MESSAGES_KEY));
  }
  @Test
  public void testShowResetPasswordForm() throws Exception {
    String username = "******";
    User user = userManager.getUserByUsername(username);
    String token = userManager.generateRecoveryToken(user);

    ResultActions update =
        mockMvc
            .perform(get("/updatePassword").param("username", username).param("token", token))
            .andExpect(status().isOk())
            .andExpect(view().name("updatePasswordForm"));

    MvcResult result = update.andReturn();
    MockHttpSession session = (MockHttpSession) result.getRequest().getSession();
    assertNull(session.getAttribute(BaseFormController.ERRORS_MESSAGES_KEY));
  }
  @Test
  public void testExecute() throws Exception {
    // start SMTP Server
    Wiser wiser = startWiser(getSmtpPort());

    ResultActions actions =
        mockMvc
            .perform(get("/passwordHint.html").param("username", "user"))
            .andExpect(status().is3xxRedirection());

    MvcResult result = actions.andReturn();
    MockHttpSession session = (MockHttpSession) result.getRequest().getSession();
    // verify that success messages are in the session
    assertNotNull(session.getAttribute(BaseFormController.MESSAGES_KEY));

    // verify an account information e-mail was sent
    wiser.stop();
    assertTrue(wiser.getMessages().size() == 1);
  }
 @Override
 protected Reader getPayloadReader(MvcResult result) throws IOException {
   return result.getRequest().getReader();
 }
  @Test
  public void testCreatingAnAccount() throws Exception {
    String scimUserJSONString =
        "{"
            + "\"userName\": \"[email protected]\","
            + "\"id\": \"newly-created-user-id\","
            + "\"emails\": [{\"value\":\"[email protected]\"}]"
            + "}";
    mockUaaServer
        .expect(requestTo("http://*****:*****@example.com"))
        .andExpect(jsonPath("$.password").value("secret"))
        .andExpect(jsonPath("$.origin").value("uaa"))
        .andExpect(jsonPath("$.verified").value(false))
        .andExpect(jsonPath("$.emails[0].value").value("*****@*****.**"))
        .andRespond(withSuccess(scimUserJSONString, APPLICATION_JSON));

    mockUaaServer
        .expect(requestTo("http://*****:*****@example.com")
                .param("password", "secret")
                .param("password_confirmation", "secret")
                .param("client_id", "app"))
        .andExpect(status().isFound())
        .andExpect(redirectedUrl("accounts/email_sent"));

    MvcResult mvcResult =
        mockMvc
            .perform(get("/verify_user").param("code", "the_secret_code"))
            .andExpect(status().isFound())
            .andExpect(redirectedUrl("http://example.com/redirect"))
            .andReturn();

    SecurityContext securityContext =
        (SecurityContext)
            mvcResult
                .getRequest()
                .getSession()
                .getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
    Authentication authentication = securityContext.getAuthentication();
    Assert.assertThat(authentication.getPrincipal(), instanceOf(UaaPrincipal.class));
    UaaPrincipal principal = (UaaPrincipal) authentication.getPrincipal();
    Assert.assertThat(principal.getId(), equalTo("newly-created-user-id"));
    Assert.assertThat(principal.getEmail(), equalTo("*****@*****.**"));
    Assert.assertThat(principal.getOrigin(), equalTo(Origin.UAA));
  }