Search in sources :

Example 1 with DefaultServerWebExchange

use of org.springframework.web.server.adapter.DefaultServerWebExchange in project spring-framework by spring-projects.

the class FreeMarkerViewTests method subscribeWithoutDemand.

// gh-22754
@Test
public void subscribeWithoutDemand() {
    ZeroDemandResponse response = new ZeroDemandResponse();
    ServerWebExchange exchange = new DefaultServerWebExchange(MockServerHttpRequest.get("/path").build(), response, new DefaultWebSessionManager(), ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    FreeMarkerView view = new FreeMarkerView();
    view.setApplicationContext(this.context);
    view.setConfiguration(this.freeMarkerConfig);
    view.setUrl("test.ftl");
    ModelMap model = new ExtendedModelMap();
    model.addAttribute("hello", "hi FreeMarker");
    view.render(model, null, exchange).subscribe();
    response.cancelWrite();
    response.checkForLeaks();
}
Also used : AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ZeroDemandResponse(org.springframework.web.reactive.result.view.ZeroDemandResponse) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) ModelMap(org.springframework.ui.ModelMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) DefaultWebSessionManager(org.springframework.web.server.session.DefaultWebSessionManager) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultServerWebExchange

use of org.springframework.web.server.adapter.DefaultServerWebExchange in project spring-framework by spring-projects.

the class DefaultWebSessionManagerTests method setUp.

@BeforeEach
void setUp() throws Exception {
    given(this.createSession.save()).willReturn(Mono.empty());
    given(this.createSession.getId()).willReturn("create-session-id");
    given(this.updateSession.getId()).willReturn("update-session-id");
    given(this.sessionStore.createWebSession()).willReturn(Mono.just(this.createSession));
    given(this.sessionStore.retrieveSession(this.updateSession.getId())).willReturn(Mono.just(this.updateSession));
    this.sessionManager = new DefaultWebSessionManager();
    this.sessionManager.setSessionIdResolver(this.sessionIdResolver);
    this.sessionManager.setSessionStore(this.sessionStore);
    MockServerHttpRequest request = MockServerHttpRequest.get("/path").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    this.exchange = new DefaultServerWebExchange(request, response, this.sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
}
Also used : AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with DefaultServerWebExchange

use of org.springframework.web.server.adapter.DefaultServerWebExchange in project spring-framework by spring-projects.

the class NashornScriptTemplateTests method subscribeWithoutDemand.

// gh-22754
@Test
public void subscribeWithoutDemand() throws Exception {
    ZeroDemandResponse response = new ZeroDemandResponse();
    ServerWebExchange exchange = new DefaultServerWebExchange(MockServerHttpRequest.get("/path").build(), response, new DefaultWebSessionManager(), ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    Map<String, Object> model = new HashMap<>();
    model.put("title", "Layout example");
    model.put("body", "This is the body");
    String viewUrl = "org/springframework/web/reactive/result/view/script/nashorn/template.html";
    ScriptTemplateView view = createViewWithUrl(viewUrl, ScriptTemplatingConfiguration.class);
    view.render(model, null, exchange).subscribe();
    response.cancelWrite();
    response.checkForLeaks();
}
Also used : AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ZeroDemandResponse(org.springframework.web.reactive.result.view.ZeroDemandResponse) HashMap(java.util.HashMap) DefaultWebSessionManager(org.springframework.web.server.session.DefaultWebSessionManager) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultServerWebExchange

use of org.springframework.web.server.adapter.DefaultServerWebExchange in project spring-security by spring-projects.

the class WebSessionOAuth2ServerAuthorizationRequestRepositoryDoNotAllowMultipleAuthorizationRequestsTests method removeAuthorizationRequestWhenMultipleThenSessionAttributeRemoved.

// gh-5145
@Test
public void removeAuthorizationRequestWhenMultipleThenSessionAttributeRemoved() {
    String oldState = "state0";
    // @formatter:off
    MockServerHttpRequest oldRequest = MockServerHttpRequest.get("/").queryParam(OAuth2ParameterNames.STATE, oldState).build();
    OAuth2AuthorizationRequest oldAuthorizationRequest = OAuth2AuthorizationRequest.authorizationCode().authorizationUri("https://example.com/oauth2/authorize").clientId("client-id").redirectUri("http://localhost/client-1").state(oldState).build();
    // @formatter:on
    Map<String, Object> sessionAttrs = spy(new HashMap<>());
    WebSession session = mock(WebSession.class);
    given(session.getAttributes()).willReturn(sessionAttrs);
    WebSessionManager sessionManager = (e) -> Mono.just(session);
    this.exchange = new DefaultServerWebExchange(this.exchange.getRequest(), new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    ServerWebExchange oldExchange = new DefaultServerWebExchange(oldRequest, new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    // @formatter:off
    Mono<OAuth2AuthorizationRequest> saveAndSaveAndRemove = this.repository.saveAuthorizationRequest(oldAuthorizationRequest, oldExchange).then(this.repository.saveAuthorizationRequest(this.authorizationRequest, this.exchange)).then(this.repository.removeAuthorizationRequest(this.exchange));
    StepVerifier.create(saveAndSaveAndRemove).expectNext(this.authorizationRequest).verifyComplete();
    StepVerifier.create(this.repository.loadAuthorizationRequest(this.exchange)).verifyComplete();
    // @formatter:on
    verify(sessionAttrs, times(2)).put(anyString(), any());
    verify(sessionAttrs).remove(anyString());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) OAuth2ParameterNames(org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) Mockito.spy(org.mockito.Mockito.spy) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) WebSessionManager(org.springframework.web.server.session.WebSessionManager) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) WebSession(org.springframework.web.server.WebSession) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) WebSession(org.springframework.web.server.WebSession) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) WebSessionManager(org.springframework.web.server.session.WebSessionManager) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultServerWebExchange

use of org.springframework.web.server.adapter.DefaultServerWebExchange in project spring-security by spring-projects.

the class WebSessionOAuth2ServerAuthorizationRequestRepositoryAllowMultipleAuthorizationRequestsTests method removeAuthorizationRequestWhenMultipleThenRemovedAndSessionAttributeUpdated.

// gh-7327
@Test
public void removeAuthorizationRequestWhenMultipleThenRemovedAndSessionAttributeUpdated() {
    String oldState = "state0";
    // @formatter:off
    MockServerHttpRequest oldRequest = MockServerHttpRequest.get("/").queryParam(OAuth2ParameterNames.STATE, oldState).build();
    OAuth2AuthorizationRequest oldAuthorizationRequest = OAuth2AuthorizationRequest.authorizationCode().authorizationUri("https://example.com/oauth2/authorize").clientId("client-id").redirectUri("http://localhost/client-1").state(oldState).build();
    // @formatter:on
    Map<String, Object> sessionAttrs = spy(new HashMap<>());
    WebSession session = mock(WebSession.class);
    given(session.getAttributes()).willReturn(sessionAttrs);
    WebSessionManager sessionManager = (e) -> Mono.just(session);
    this.exchange = new DefaultServerWebExchange(this.exchange.getRequest(), new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    ServerWebExchange oldExchange = new DefaultServerWebExchange(oldRequest, new MockServerHttpResponse(), sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
    // @formatter:off
    Mono<OAuth2AuthorizationRequest> saveAndSaveAndRemove = this.repository.saveAuthorizationRequest(oldAuthorizationRequest, oldExchange).then(this.repository.saveAuthorizationRequest(this.authorizationRequest, this.exchange)).then(this.repository.removeAuthorizationRequest(this.exchange));
    StepVerifier.create(saveAndSaveAndRemove).expectNext(this.authorizationRequest).verifyComplete();
    StepVerifier.create(this.repository.loadAuthorizationRequest(this.exchange)).verifyComplete();
    // @formatter:on
    verify(sessionAttrs, times(3)).put(any(), any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) OAuth2ParameterNames(org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames) StepVerifier(reactor.test.StepVerifier) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) Mockito.spy(org.mockito.Mockito.spy) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) BDDMockito.given(org.mockito.BDDMockito.given) Map(java.util.Map) WebSessionManager(org.springframework.web.server.session.WebSessionManager) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) WebSession(org.springframework.web.server.WebSession) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Mockito.mock(org.mockito.Mockito.mock) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse) AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) WebSession(org.springframework.web.server.WebSession) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) WebSessionManager(org.springframework.web.server.session.WebSessionManager) OAuth2AuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultServerWebExchange (org.springframework.web.server.adapter.DefaultServerWebExchange)7 AcceptHeaderLocaleContextResolver (org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver)7 Test (org.junit.jupiter.api.Test)6 ServerWebExchange (org.springframework.web.server.ServerWebExchange)6 HashMap (java.util.HashMap)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Map (java.util.Map)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 BDDMockito.given (org.mockito.BDDMockito.given)4 Mockito.mock (org.mockito.Mockito.mock)4 Mockito.spy (org.mockito.Mockito.spy)4 Mockito.times (org.mockito.Mockito.times)4 Mockito.verify (org.mockito.Mockito.verify)4 ServerCodecConfigurer (org.springframework.http.codec.ServerCodecConfigurer)4 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)4 MockServerHttpRequest (org.springframework.mock.http.server.reactive.MockServerHttpRequest)4 MockServerHttpResponse (org.springframework.mock.http.server.reactive.MockServerHttpResponse)4 OAuth2AuthorizationRequest (org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest)4 OAuth2ParameterNames (org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames)4 WebSession (org.springframework.web.server.WebSession)4