Java Code Examples for org.springframework.mock.web.mockhttpservletrequest#setServerName()

The following examples show how to use org.springframework.mock.web.mockhttpservletrequest#setServerName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OAuth2AuthenticationServiceTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticationCookies() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("www.test.com");
    request.addHeader("Authorization", CLIENT_AUTHORIZATION);
    Map<String, String> params = new HashMap<>();
    params.put("username", "user");
    params.put("password", "user");
    params.put("rememberMe", "true");
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.authenticate(request, response, params);
    //check that cookies are set correctly
    Cookie accessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(ACCESS_TOKEN_VALUE, accessTokenCookie.getValue());
    Cookie refreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(REFRESH_TOKEN_VALUE, OAuth2CookieHelper.getRefreshTokenValue(refreshTokenCookie));
    Assert.assertTrue(OAuth2CookieHelper.isRememberMe(refreshTokenCookie));
}
 
Example 2
Source File: OAuth2AuthenticationServiceTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void testAuthenticationNoRememberMe() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("www.test.com");
    Map<String, String> params = new HashMap<>();
    params.put("username", "user");
    params.put("password", "user");
    params.put("rememberMe", "false");
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.authenticate(request, response, params);
    //check that cookies are set correctly
    Cookie accessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(ACCESS_TOKEN_VALUE, accessTokenCookie.getValue());
    Cookie refreshTokenCookie = response.getCookie(OAuth2CookieHelper.SESSION_TOKEN_COOKIE);
    Assert.assertEquals(REFRESH_TOKEN_VALUE, OAuth2CookieHelper.getRefreshTokenValue(refreshTokenCookie));
    Assert.assertFalse(OAuth2CookieHelper.isRememberMe(refreshTokenCookie));
}
 
Example 3
Source File: OAuth2AuthenticationServiceTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void testAuthenticationCookies() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("www.test.com");
    request.addHeader("Authorization", CLIENT_AUTHORIZATION);
    Map<String, String> params = new HashMap<>();
    params.put("username", "user");
    params.put("password", "user");
    params.put("rememberMe", "true");
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.authenticate(request, response, params);
    //check that cookies are set correctly
    Cookie accessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(ACCESS_TOKEN_VALUE, accessTokenCookie.getValue());
    Cookie refreshTokenCookie = response.getCookie(OAuth2CookieHelper.REFRESH_TOKEN_COOKIE);
    Assert.assertEquals(REFRESH_TOKEN_VALUE, OAuth2CookieHelper.getRefreshTokenValue(refreshTokenCookie));
    Assert.assertTrue(OAuth2CookieHelper.isRememberMe(refreshTokenCookie));
}
 
Example 4
Source File: OAuth2AuthenticationServiceTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticationNoRememberMe() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("www.test.com");
    Map<String, String> params = new HashMap<>();
    params.put("username", "user");
    params.put("password", "user");
    params.put("rememberMe", "false");
    MockHttpServletResponse response = new MockHttpServletResponse();
    authenticationService.authenticate(request, response, params);
    //check that cookies are set correctly
    Cookie accessTokenCookie = response.getCookie(OAuth2CookieHelper.ACCESS_TOKEN_COOKIE);
    Assert.assertEquals(ACCESS_TOKEN_VALUE, accessTokenCookie.getValue());
    Cookie refreshTokenCookie = response.getCookie(OAuth2CookieHelper.SESSION_TOKEN_COOKIE);
    Assert.assertEquals(REFRESH_TOKEN_VALUE, OAuth2CookieHelper.getRefreshTokenValue(refreshTokenCookie));
    Assert.assertFalse(OAuth2CookieHelper.isRememberMe(refreshTokenCookie));
}
 
Example 5
Source File: OAuth2CookieHelperTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void testIpAddress() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("127.0.0.1");
    String name = ReflectionTestUtils.invokeMethod(cookieHelper, GET_COOKIE_DOMAIN_METHOD, request);
    Assert.assertNull(name);
}
 
Example 6
Source File: PresentationsControllerTest.java    From spring-comparing-template-engines with Apache License 2.0 5 votes vote down vote up
@Test
public void should_return_other_view() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setServerName("localhost");
	request.setRequestURI("/test");
	
	final String view = controller.showList(request, "test", modelMap);
	assertEquals("index-test", view);
}
 
Example 7
Source File: HtmlUnitRequestBuilder.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
	String charset = getCharset();
	String httpMethod = this.webRequest.getHttpMethod().name();
	UriComponents uriComponents = uriComponents();

	MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(servletContext, httpMethod,
			uriComponents.getPath());
	parent(request, this.parentBuilder);
	request.setServerName(uriComponents.getHost()); // needs to be first for additional headers
	authType(request);
	request.setCharacterEncoding(charset);
	content(request, charset);
	contextPath(request, uriComponents);
	contentType(request);
	cookies(request);
	headers(request);
	locales(request);
	servletPath(uriComponents, request);
	params(request, uriComponents);
	ports(uriComponents, request);
	request.setProtocol("HTTP/1.1");
	request.setQueryString(uriComponents.getQuery());
	request.setScheme(uriComponents.getScheme());
	pathInfo(uriComponents,request);

	return postProcess(request);
}
 
Example 8
Source File: WebAuthnRegistrationRequestValidatorTest.java    From webauthn4j-spring-security with Apache License 2.0 5 votes vote down vote up
@Test
public void validate_with_transports_null_test() {
    WebAuthnRegistrationRequestValidator target = new WebAuthnRegistrationRequestValidator(
            webAuthnManager, serverPropertyProvider
    );

    ServerProperty serverProperty = mock(ServerProperty.class);
    when(serverPropertyProvider.provide(any())).thenReturn(serverProperty);

    CollectedClientData collectedClientData = mock(CollectedClientData.class);
    AttestationObject attestationObject = mock(AttestationObject.class);
    AuthenticationExtensionsClientOutputs<RegistrationExtensionClientOutput<?>> clientExtensionOutputs = new AuthenticationExtensionsClientOutputs<>();
    when(webAuthnManager.validate(any(RegistrationRequest.class), any(RegistrationParameters.class))).thenReturn(
            new RegistrationData(attestationObject, null, collectedClientData, null, clientExtensionOutputs, null));

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    mockHttpServletRequest.setScheme("https");
    mockHttpServletRequest.setServerName("example.com");
    mockHttpServletRequest.setServerPort(443);
    String clientDataBase64 = "clientDataBase64";
    String attestationObjectBase64 = "attestationObjectBase64";
    String clientExtensionsJSON = "clientExtensionsJSON";

    target.validate(mockHttpServletRequest, clientDataBase64, attestationObjectBase64, null, clientExtensionsJSON);

    ArgumentCaptor<RegistrationRequest> registrationRequestArgumentCaptor = ArgumentCaptor.forClass(RegistrationRequest.class);
    ArgumentCaptor<RegistrationParameters> registrationParametersArgumentCaptor = ArgumentCaptor.forClass(RegistrationParameters.class);
    verify(webAuthnManager).validate(registrationRequestArgumentCaptor.capture(), registrationParametersArgumentCaptor.capture());
    RegistrationRequest registrationRequest = registrationRequestArgumentCaptor.getValue();
    RegistrationParameters registrationParameters = registrationParametersArgumentCaptor.getValue();

    assertThat(registrationRequest.getClientDataJSON()).isEqualTo(Base64UrlUtil.decode(clientDataBase64));
    assertThat(registrationRequest.getAttestationObject()).isEqualTo(Base64UrlUtil.decode(attestationObjectBase64));
    assertThat(registrationRequest.getClientExtensionsJSON()).isEqualTo(clientExtensionsJSON);
    assertThat(registrationParameters.getServerProperty()).isEqualTo(serverProperty);
    assertThat(registrationParameters.getExpectedExtensionIds()).isEqualTo(target.getExpectedRegistrationExtensionIds());
}
 
Example 9
Source File: OAuth2CookieHelperTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void testComSubDomain() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("abc.test.com");
    String name = ReflectionTestUtils.invokeMethod(cookieHelper, GET_COOKIE_DOMAIN_METHOD, request);
    Assert.assertEquals(".test.com", name);
}
 
Example 10
Source File: HtmlUnitRequestBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
	Charset charset = getCharset();
	String httpMethod = this.webRequest.getHttpMethod().name();
	UriComponents uriComponents = uriComponents();
	String path = uriComponents.getPath();

	MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(
			servletContext, httpMethod, (path != null ? path : ""));
	parent(request, this.parentBuilder);
	String host = uriComponents.getHost();
	request.setServerName(host != null ? host : "");  // needs to be first for additional headers
	authType(request);
	request.setCharacterEncoding(charset.name());
	content(request, charset);
	contextPath(request, uriComponents);
	contentType(request);
	cookies(request);
	headers(request);
	locales(request);
	servletPath(uriComponents, request);
	params(request, uriComponents);
	ports(uriComponents, request);
	request.setProtocol("HTTP/1.1");
	request.setQueryString(uriComponents.getQuery());
	String scheme = uriComponents.getScheme();
	request.setScheme(scheme != null ? scheme : "");
	request.setPathInfo(null);

	return postProcess(request);
}
 
Example 11
Source File: OptionsProviderImplTest.java    From webauthn4j-spring-security with Apache License 2.0 5 votes vote down vote up
@Test
public void getEffectiveRpId() {
    WebAuthnUserDetailsService userDetailsService = mock(WebAuthnUserDetailsService.class);
    ChallengeRepository challengeRepository = mock(ChallengeRepository.class);
    OptionsProviderImpl optionsProvider = new OptionsProviderImpl(userDetailsService, challengeRepository);
    optionsProvider.setRpId(null);
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    httpServletRequest.setScheme("https");
    httpServletRequest.setServerName("example.com");
    httpServletRequest.setServerPort(8080);
    assertThat(optionsProvider.getEffectiveRpId(httpServletRequest)).isEqualTo("example.com");

}
 
Example 12
Source File: HtmlUnitRequestBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
	Charset charset = getCharset();
	String httpMethod = this.webRequest.getHttpMethod().name();
	UriComponents uriComponents = uriComponents();
	String path = uriComponents.getPath();

	MockHttpServletRequest request = new HtmlUnitMockHttpServletRequest(
			servletContext, httpMethod, (path != null ? path : ""));
	parent(request, this.parentBuilder);
	String host = uriComponents.getHost();
	request.setServerName(host != null ? host : "");  // needs to be first for additional headers
	authType(request);
	request.setCharacterEncoding(charset.name());
	content(request, charset);
	contextPath(request, uriComponents);
	contentType(request);
	cookies(request);
	headers(request);
	locales(request);
	servletPath(uriComponents, request);
	params(request, uriComponents);
	ports(uriComponents, request);
	request.setProtocol("HTTP/1.1");
	request.setQueryString(uriComponents.getQuery());
	String scheme = uriComponents.getScheme();
	request.setScheme(scheme != null ? scheme : "");
	request.setPathInfo(null);

	return postProcess(request);
}
 
Example 13
Source File: OAuth2CookieHelperTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
public void testLocalhostDomain() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("localhost");
    String name = ReflectionTestUtils.invokeMethod(cookieHelper, GET_COOKIE_DOMAIN_METHOD, request);
    Assert.assertNull(name);
}
 
Example 14
Source File: ApiProxyHandlerTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
private void testWithServletPath(String servletPath) throws Exception {
  MockHttpServletRequest request = new MockHttpServletRequest();
  request.setServerName("localhost");
  request.setServerPort(8080);
  request.setServletPath(servletPath);
  MockHttpServletResponse response = new MockHttpServletResponse();
  ApiProxyHandler handler = new ApiProxyHandler();
  EndpointsContext context =
      new EndpointsContext("GET", "static/proxy.html", request, response, true);

  handler.handle(context);

  assertThat(response.getContentAsString()).contains("googleapis.server.init()");
}
 
Example 15
Source File: OAuth2CookieHelperTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
public void testCoUkDomain() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("test.co.uk");
    String name = ReflectionTestUtils.invokeMethod(cookieHelper, GET_COOKIE_DOMAIN_METHOD, request);
    Assert.assertNull(name);            //already top-level domain
}
 
Example 16
Source File: RequestProcessorTest.java    From auth0-java-mvc-common with MIT License 5 votes vote down vote up
private MockHttpServletRequest getRequest(Map<String, Object> parameters) {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setScheme("https");
    request.setServerName("me.auth0.com");
    request.setServerPort(80);
    request.setRequestURI("/callback");
    request.setParameters(parameters);
    return request;
}
 
Example 17
Source File: EndpointsServletTest.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws ServletException {
  req = new MockHttpServletRequest();
  req.setServletPath("/_ah/api");
  req.addHeader("Host", API_SERVER_NAME);
  req.setServerName(API_SERVER_NAME);
  req.setServerPort(API_PORT);
  resp = new MockHttpServletResponse();
  servlet = new EndpointsServlet();
  MockServletConfig config = new MockServletConfig();
  config.addInitParameter("services", TestApi.class.getName());
  servlet.init(config);
}
 
Example 18
Source File: OAuth2CookieHelperTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
public void testWwwDomainCom() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("www.test.com");
    String name = ReflectionTestUtils.invokeMethod(cookieHelper, GET_COOKIE_DOMAIN_METHOD, request);
    Assert.assertNull(name);
}
 
Example 19
Source File: OAuth2CookieHelperTest.java    From cubeai with Apache License 2.0 5 votes vote down vote up
@Test
public void testComDomain() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServerName("test.com");
    String name = ReflectionTestUtils.invokeMethod(cookieHelper, GET_COOKIE_DOMAIN_METHOD, request);
    Assert.assertNull(name);        //already top-level domain
}
 
Example 20
Source File: RegistrationValidationTest.java    From webauthn4j-spring-security with Apache License 2.0 4 votes vote down vote up
@Test
public void validate_test() {
    ServerProperty serverProperty = new ServerProperty(origin, rpId, challenge, null);
    when(serverPropertyProvider.provide(any())).thenReturn(serverProperty);


    AuthenticatorSelectionCriteria authenticatorSelectionCriteria =
            new AuthenticatorSelectionCriteria(AuthenticatorAttachment.CROSS_PLATFORM, true, UserVerificationRequirement.REQUIRED);

    PublicKeyCredentialParameters publicKeyCredentialParameters = new PublicKeyCredentialParameters(PublicKeyCredentialType.PUBLIC_KEY, COSEAlgorithmIdentifier.ES256);

    PublicKeyCredentialUserEntity publicKeyCredentialUserEntity = new PublicKeyCredentialUserEntity();

    PublicKeyCredentialCreationOptions credentialCreationOptions = new PublicKeyCredentialCreationOptions(
            new PublicKeyCredentialRpEntity(rpId, "example.com"),
            publicKeyCredentialUserEntity,
            challenge,
            Collections.singletonList(publicKeyCredentialParameters),
            null,
            null,
            authenticatorSelectionCriteria,
            AttestationConveyancePreference.NONE,
            null
    );

    AuthenticatorAttestationResponse registrationRequest = clientPlatform.create(credentialCreationOptions).getAuthenticatorResponse();

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
    mockHttpServletRequest.setScheme("https");
    mockHttpServletRequest.setServerName("example.com");
    mockHttpServletRequest.setServerPort(443);

    String clientDataBase64 = Base64UrlUtil.encodeToString(registrationRequest.getClientDataJSON());
    String attestationObjectBase64 = Base64UrlUtil.encodeToString(registrationRequest.getAttestationObject());
    Set<String> transports = Collections.emptySet();
    String clientExtensionsJSON = null;

    WebAuthnRegistrationRequestValidationResponse response
            = target.validate(mockHttpServletRequest, clientDataBase64, attestationObjectBase64, transports, clientExtensionsJSON);

    assertThat(response.getAttestationObject()).isNotNull();
    assertThat(response.getCollectedClientData()).isNotNull();
    assertThat(response.getRegistrationExtensionsClientOutputs()).isNull();
}