Add MockServerHttpRequest/Response
This commit is contained in:
parent
78454f5a2e
commit
448aac813a
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright 2002-2015 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.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MockServerHttpRequest implements ServerHttpRequest {
|
||||
|
||||
private HttpMethod httpMethod;
|
||||
|
||||
private URI uri;
|
||||
|
||||
private HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
private Publisher<ByteBuffer> body;
|
||||
|
||||
|
||||
public MockServerHttpRequest(HttpMethod httpMethod, URI uri) {
|
||||
this.httpMethod = httpMethod;
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public MockServerHttpRequest(Publisher<ByteBuffer> body, HttpMethod httpMethod, URI uri) {
|
||||
this.body = body;
|
||||
this.httpMethod = httpMethod;
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public HttpMethod getMethod() {
|
||||
return this.httpMethod;
|
||||
}
|
||||
|
||||
public void setHttpMethod(HttpMethod httpMethod) {
|
||||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
public void setUri(URI uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
public void setHeaders(HttpHeaders headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<ByteBuffer> getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
public void setBody(Publisher<ByteBuffer> body) {
|
||||
this.body = body;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright 2002-2015 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.http.server.reactive;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.Publishers;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
/**
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MockServerHttpResponse implements ServerHttpResponse {
|
||||
|
||||
private HttpStatus status;
|
||||
|
||||
private HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
private Publisher<ByteBuffer> body;
|
||||
|
||||
|
||||
@Override
|
||||
public void setStatusCode(HttpStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public HttpStatus getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Void> writeHeaders() {
|
||||
return Publishers.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<Void> setBody(Publisher<ByteBuffer> body) {
|
||||
this.body = body;
|
||||
return Publishers.empty();
|
||||
}
|
||||
|
||||
public Publisher<ByteBuffer> getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
}
|
|
@ -17,8 +17,6 @@
|
|||
package org.springframework.web.reactive.method.annotation;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
@ -28,8 +26,8 @@ import org.reactivestreams.Publisher;
|
|||
import reactor.rx.Streams;
|
||||
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
@ -59,7 +57,7 @@ public class RequestMappingHandlerMappingTests {
|
|||
|
||||
@Test
|
||||
public void path() throws Exception {
|
||||
ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, "boo");
|
||||
ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, new URI("boo"));
|
||||
Publisher<?> handlerPublisher = this.mapping.getHandler(request);
|
||||
HandlerMethod handlerMethod = toHandlerMethod(handlerPublisher);
|
||||
assertEquals(TestController.class.getMethod("boo"), handlerMethod.getMethod());
|
||||
|
@ -67,12 +65,12 @@ public class RequestMappingHandlerMappingTests {
|
|||
|
||||
@Test
|
||||
public void method() throws Exception {
|
||||
ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.POST, "foo");
|
||||
ServerHttpRequest request = new MockServerHttpRequest(HttpMethod.POST, new URI("foo"));
|
||||
Publisher<?> handlerPublisher = this.mapping.getHandler(request);
|
||||
HandlerMethod handlerMethod = toHandlerMethod(handlerPublisher);
|
||||
assertEquals(TestController.class.getMethod("postFoo"), handlerMethod.getMethod());
|
||||
|
||||
request = new MockServerHttpRequest(HttpMethod.GET, "foo");
|
||||
request = new MockServerHttpRequest(HttpMethod.GET, new URI("foo"));
|
||||
handlerPublisher = this.mapping.getHandler(request);
|
||||
handlerMethod = toHandlerMethod(handlerPublisher);
|
||||
assertEquals(TestController.class.getMethod("getFoo"), handlerMethod.getMethod());
|
||||
|
@ -112,44 +110,4 @@ public class RequestMappingHandlerMappingTests {
|
|||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* TODO: this is more widely needed.
|
||||
*/
|
||||
private static class MockServerHttpRequest implements ServerHttpRequest {
|
||||
|
||||
private HttpMethod method;
|
||||
|
||||
private URI uri;
|
||||
|
||||
public MockServerHttpRequest(HttpMethod method, String path) {
|
||||
this.method = method;
|
||||
try {
|
||||
this.uri = new URI(path);
|
||||
} catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException("Could not get URI: " + ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Publisher<ByteBuffer> getBody() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpMethod getMethod() {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue