Add MockServerWebExchange and toExchange shortcuts
Issue: SPR-15350
This commit is contained in:
parent
45aa1edf87
commit
41c413a748
|
@ -103,6 +103,15 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
return this.cookies;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Shortcut to wrap the request with a {@code MockServerWebExchange}.
|
||||
*/
|
||||
public MockServerWebExchange toExchange() {
|
||||
return new MockServerWebExchange(this);
|
||||
}
|
||||
|
||||
|
||||
// Static builder methods
|
||||
|
||||
/**
|
||||
|
@ -199,8 +208,8 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
|
||||
|
||||
/**
|
||||
* Defines a builder that adds headers to the request.
|
||||
* @param <B> the builder subclass
|
||||
* Request builder exposing properties not related to the body.
|
||||
* @param <B> the builder sub-class
|
||||
*/
|
||||
public interface BaseBuilder<B extends BaseBuilder<B>> {
|
||||
|
||||
|
@ -290,6 +299,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
* @see BodyBuilder#body(String)
|
||||
*/
|
||||
MockServerHttpRequest build();
|
||||
|
||||
/**
|
||||
* Shortcut for:<br>
|
||||
* {@code build().toExchange()}
|
||||
*/
|
||||
MockServerWebExchange toExchange();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -466,6 +481,11 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
public MockServerHttpRequest build() {
|
||||
return body(Flux.empty());
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockServerWebExchange toExchange() {
|
||||
return build().toExchange();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.mock.http.server.reactive;
|
||||
|
||||
import org.springframework.web.server.ServerWebExchangeDecorator;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
import org.springframework.web.server.session.DefaultWebSessionManager;
|
||||
|
||||
/**
|
||||
* {@code ServerWebExchange} for use in tests.
|
||||
*
|
||||
* <p>Effectively a wrapper around {@link DefaultServerWebExchange} plugged in
|
||||
* with {@link MockServerHttpRequest} and {@link MockServerHttpResponse}.
|
||||
*
|
||||
* <p>Typically used via {@link MockServerHttpRequest#toExchange()}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
public class MockServerWebExchange extends ServerWebExchangeDecorator {
|
||||
|
||||
|
||||
public MockServerWebExchange(MockServerHttpRequest request) {
|
||||
super(new DefaultServerWebExchange(
|
||||
request, new MockServerHttpResponse(), new DefaultWebSessionManager()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MockServerHttpResponse getResponse() {
|
||||
return (MockServerHttpResponse) super.getResponse();
|
||||
}
|
||||
|
||||
}
|
|
@ -44,7 +44,6 @@ import org.springframework.util.MultiValueMap;
|
|||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebSession;
|
||||
import org.springframework.web.server.session.DefaultWebSessionManager;
|
||||
import org.springframework.web.server.session.WebSessionManager;
|
||||
|
||||
/**
|
||||
|
@ -82,14 +81,6 @@ public class DefaultServerWebExchange implements ServerWebExchange {
|
|||
private volatile boolean notModified;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor with a request and response only.
|
||||
* By default creates a session manager of type {@link DefaultWebSessionManager}.
|
||||
*/
|
||||
public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response) {
|
||||
this(request, response, new DefaultWebSessionManager());
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternate constructor with a WebSessionManager parameter.
|
||||
*/
|
||||
|
|
|
@ -103,6 +103,15 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
return this.cookies;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Shortcut to wrap the request with a {@code MockServerWebExchange}.
|
||||
*/
|
||||
public MockServerWebExchange toExchange() {
|
||||
return new MockServerWebExchange(this);
|
||||
}
|
||||
|
||||
|
||||
// Static builder methods
|
||||
|
||||
/**
|
||||
|
@ -199,8 +208,8 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
|
||||
|
||||
/**
|
||||
* Defines a builder that adds headers to the request.
|
||||
* @param <B> the builder subclass
|
||||
* Request builder exposing properties not related to the body.
|
||||
* @param <B> the builder sub-class
|
||||
*/
|
||||
public interface BaseBuilder<B extends BaseBuilder<B>> {
|
||||
|
||||
|
@ -219,6 +228,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
*/
|
||||
B cookie(String path, HttpCookie... cookie);
|
||||
|
||||
/**
|
||||
* Add the given cookies.
|
||||
* @param cookies the cookies.
|
||||
*/
|
||||
B cookies(MultiValueMap<String, HttpCookie> cookies);
|
||||
|
||||
/**
|
||||
* Add the given, single header value under the given name.
|
||||
* @param headerName the header name
|
||||
|
@ -227,6 +242,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
*/
|
||||
B header(String headerName, String... headerValues);
|
||||
|
||||
/**
|
||||
* Add the given header values.
|
||||
* @param headers the header values
|
||||
*/
|
||||
B headers(MultiValueMap<String, String> headers);
|
||||
|
||||
/**
|
||||
* Set the list of acceptable {@linkplain MediaType media types}, as
|
||||
* specified by the {@code Accept} header.
|
||||
|
@ -278,6 +299,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
* @see BodyBuilder#body(String)
|
||||
*/
|
||||
MockServerHttpRequest build();
|
||||
|
||||
/**
|
||||
* Shortcut for:<br>
|
||||
* {@code build().toExchange()}
|
||||
*/
|
||||
MockServerWebExchange toExchange();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -360,6 +387,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyBuilder cookies(MultiValueMap<String, HttpCookie> cookies) {
|
||||
this.cookies.putAll(cookies);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyBuilder header(String headerName, String... headerValues) {
|
||||
for (String headerValue : headerValues) {
|
||||
|
@ -368,6 +401,12 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyBuilder headers(MultiValueMap<String, String> headers) {
|
||||
this.headers.putAll(headers);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodyBuilder accept(MediaType... acceptableMediaTypes) {
|
||||
this.headers.setAccept(Arrays.asList(acceptableMediaTypes));
|
||||
|
@ -442,6 +481,11 @@ public class MockServerHttpRequest extends AbstractServerHttpRequest {
|
|||
public MockServerHttpRequest build() {
|
||||
return body(Flux.empty());
|
||||
}
|
||||
|
||||
@Override
|
||||
public MockServerWebExchange toExchange() {
|
||||
return build().toExchange();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.mock.http.server.reactive.test;
|
||||
|
||||
import org.springframework.web.server.ServerWebExchangeDecorator;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
import org.springframework.web.server.session.DefaultWebSessionManager;
|
||||
|
||||
/**
|
||||
* {@code ServerWebExchange} for use in tests.
|
||||
*
|
||||
* <p>Effectively a wrapper around {@link DefaultServerWebExchange} plugged in
|
||||
* with {@link MockServerHttpRequest} and {@link MockServerHttpResponse}.
|
||||
*
|
||||
* <p>Typically used via {@link MockServerHttpRequest#toExchange()}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*/
|
||||
public class MockServerWebExchange extends ServerWebExchangeDecorator {
|
||||
|
||||
|
||||
public MockServerWebExchange(MockServerHttpRequest request) {
|
||||
super(new DefaultServerWebExchange(
|
||||
request, new MockServerHttpResponse(), new DefaultWebSessionManager()));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MockServerHttpResponse getResponse() {
|
||||
return (MockServerHttpResponse) super.getResponse();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue