From 448aac813a24a434e9deb51995ba12e6f00d892f Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 9 Dec 2015 14:43:22 -0500 Subject: [PATCH] Add MockServerHttpRequest/Response --- .../reactive/MockServerHttpRequest.java | 87 +++++++++++++++++++ .../reactive/MockServerHttpResponse.java | 67 ++++++++++++++ .../RequestMappingHandlerMappingTests.java | 50 +---------- 3 files changed, 158 insertions(+), 46 deletions(-) create mode 100644 spring-web-reactive/src/test/java/org/springframework/http/server/reactive/MockServerHttpRequest.java create mode 100644 spring-web-reactive/src/test/java/org/springframework/http/server/reactive/MockServerHttpResponse.java diff --git a/spring-web-reactive/src/test/java/org/springframework/http/server/reactive/MockServerHttpRequest.java b/spring-web-reactive/src/test/java/org/springframework/http/server/reactive/MockServerHttpRequest.java new file mode 100644 index 0000000000..c5ffbc83fa --- /dev/null +++ b/spring-web-reactive/src/test/java/org/springframework/http/server/reactive/MockServerHttpRequest.java @@ -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 body; + + + public MockServerHttpRequest(HttpMethod httpMethod, URI uri) { + this.httpMethod = httpMethod; + this.uri = uri; + } + + public MockServerHttpRequest(Publisher 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 getBody() { + return this.body; + } + + public void setBody(Publisher body) { + this.body = body; + } +} diff --git a/spring-web-reactive/src/test/java/org/springframework/http/server/reactive/MockServerHttpResponse.java b/spring-web-reactive/src/test/java/org/springframework/http/server/reactive/MockServerHttpResponse.java new file mode 100644 index 0000000000..694951ff38 --- /dev/null +++ b/spring-web-reactive/src/test/java/org/springframework/http/server/reactive/MockServerHttpResponse.java @@ -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 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 writeHeaders() { + return Publishers.empty(); + } + + @Override + public Publisher setBody(Publisher body) { + this.body = body; + return Publishers.empty(); + } + + public Publisher getBody() { + return this.body; + } + +} diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/method/annotation/RequestMappingHandlerMappingTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/method/annotation/RequestMappingHandlerMappingTests.java index f3eac33527..4bbe2cd443 100644 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/method/annotation/RequestMappingHandlerMappingTests.java +++ b/spring-web-reactive/src/test/java/org/springframework/web/reactive/method/annotation/RequestMappingHandlerMappingTests.java @@ -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 getBody() { - return null; - } - - @Override - public HttpMethod getMethod() { - return this.method; - } - - @Override - public URI getURI() { - return this.uri; - } - - @Override - public HttpHeaders getHeaders() { - return null; - } - } - }