@Test public void unresolvedViewName() { String returnValue = "account"; MethodParameter returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(String.class); HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext); MockServerWebExchange exchange = MockServerWebExchange.from(get("/path")); Mono<Void> mono = resultHandler().handleResult(exchange, result); StepVerifier.create(mono) .expectNextCount(0) .expectErrorMessage("Could not resolve view with name 'path'.") .verify(); }
@Test public void contentNegotiationWith406() { TestBean value = new TestBean("Joe"); MethodParameter returnType = on(Handler.class).resolveReturnType(TestBean.class); HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext); MockServerWebExchange exchange = MockServerWebExchange.from(get("/account").accept(APPLICATION_JSON)); ViewResolutionResultHandler resultHandler = resultHandler(new TestViewResolver("account")); Mono<Void> mono = resultHandler.handleResult(exchange, handlerResult); StepVerifier.create(mono) .expectNextCount(0) .expectError(NotAcceptableStatusException.class) .verify(); }
@Test public void resolve() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().noName()).arg(Foo.class); Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); StepVerifier.create(mono) .expectNextCount(0) .expectError(ServerWebInputException.class) .verify(); Foo foo = new Foo(); this.exchange.getAttributes().put("foo", foo); mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertSame(foo, mono.block()); }
@Test public void andRoute() { RouterFunction<ServerResponse> routerFunction1 = request -> Mono.empty(); RequestPredicate requestPredicate = request -> true; RouterFunction<ServerResponse> result = routerFunction1.andRoute(requestPredicate, this::handlerMethod); assertNotNull(result); MockServerRequest request = MockServerRequest.builder().build(); Mono<? extends HandlerFunction<?>> resultHandlerFunction = result.route(request); StepVerifier.create(resultHandlerFunction) .expectNextCount(1) .expectComplete() .verify(); }
@Test public void emptyBodyWithFlowable() throws Exception { ResolvableType type = httpEntityType(Flowable.class, String.class); HttpEntity<Flowable<String>> entity = resolveValueWithEmptyBody(type); StepVerifier.create(entity.getBody()) .expectNextCount(0) .expectComplete() .verify(); }
@Test public void emptyBodyWithObservable() throws Exception { ResolvableType type = httpEntityType(Observable.class, String.class); HttpEntity<Observable<String>> entity = resolveValueWithEmptyBody(type); StepVerifier.create(RxReactiveStreams.toPublisher(entity.getBody())) .expectNextCount(0) .expectComplete() .verify(); }
@Test public void emptyBodyWithSingle() throws Exception { ResolvableType type = httpEntityType(Single.class, String.class); HttpEntity<Single<String>> entity = resolveValueWithEmptyBody(type); StepVerifier.create(RxReactiveStreams.toPublisher(entity.getBody())) .expectNextCount(0) .expectError(ServerWebInputException.class) .verify(); }
@Test public void missingRequestParam() { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class); Mono<Object> mono = this.resolver.resolveArgument(param, this.bindContext, exchange); StepVerifier.create(mono) .expectNextCount(0) .expectError(ServerWebInputException.class) .verify(); }
@Test public void notFound() throws Exception { Mono<Object> mono = resolver.resolveArgument( this.paramNamedValueStringArray, this.bindingContext, MockServerWebExchange.from(MockServerHttpRequest.get("/"))); StepVerifier.create(mono) .expectNextCount(0) .expectError(ServerWebInputException.class) .verify(); }
@Test public void handleMissingValue() throws Exception { BindingContext bindingContext = new BindingContext(); Mono<Object> mono = this.resolver.resolveArgument(this.paramNamedString, bindingContext, this.exchange); StepVerifier.create(mono) .expectNextCount(0) .expectError(ServerErrorException.class) .verify(); }
@Test public void ofServerSentEventFlux() { ServerSentEvent<String> event = ServerSentEvent.builder("foo").build(); Flux<ServerSentEvent<String>> body = Flux.just(event); BodyInserter<Flux<ServerSentEvent<String>>, ServerHttpResponse> inserter = BodyInserters.fromServerSentEvents(body); MockServerHttpResponse response = new MockServerHttpResponse(); Mono<Void> result = inserter.insert(response, this.context); StepVerifier.create(result).expectNextCount(0).expectComplete().verify(); }
@Test public void emptyBodyWithRxJava2Maybe() throws Exception { ResolvableType type = httpEntityType(Maybe.class, String.class); HttpEntity<Maybe<String>> entity = resolveValueWithEmptyBody(type); StepVerifier.create(entity.getBody().toFlowable()) .expectNextCount(0) .expectComplete() .verify(); }
@Test public void notFound() { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); Mono<Object> mono = resolver.resolveArgument(this.cookieParameter, this.bindingContext, exchange); StepVerifier.create(mono) .expectNextCount(0) .expectError(ServerWebInputException.class) .verify(); }
@Test public void partialContentInvalidRangeHeader() { MockServerHttpRequest request = MockServerHttpRequest.get("").header("range", "bytes=foo bar").build(); MockServerWebExchange exchange = MockServerWebExchange.from(request); setPathWithinHandlerMapping(exchange, "foo.txt"); StepVerifier.create(this.handler.handle(exchange)) .expectNextCount(0) .expectComplete() .verify(); assertEquals(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE, exchange.getResponse().getStatusCode()); assertEquals("bytes", exchange.getResponse().getHeaders().getFirst("Accept-Ranges")); }
@Test public void emptyBodyWithFlux() throws Exception { ResolvableType type = httpEntityType(Flux.class, String.class); HttpEntity<Flux<String>> entity = resolveValueWithEmptyBody(type); StepVerifier.create(entity.getBody()).expectNextCount(0).expectComplete().verify(); }
@Test @SuppressWarnings("unchecked") public void validateMonoTestBean() throws Exception { String body = "{\"bar\":\"b1\"}"; ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class); MethodParameter param = this.testMethod.arg(type); Mono<TestBean> mono = resolveValue(param, body); StepVerifier.create(mono).expectNextCount(0).expectError(ServerWebInputException.class).verify(); }
@Test public void emptyBodyWithRxJava2Observable() throws Exception { ResolvableType type = httpEntityType(io.reactivex.Observable.class, String.class); HttpEntity<io.reactivex.Observable<String>> entity = resolveValueWithEmptyBody(type); StepVerifier.create(entity.getBody().toFlowable(BackpressureStrategy.BUFFER)) .expectNextCount(0) .expectComplete() .verify(); }
@Test public void nullIfNotRequired() throws Exception { BindingContext bindingContext = new BindingContext(); Mono<Object> mono = this.resolver.resolveArgument(this.paramNotRequired, bindingContext, this.exchange); StepVerifier.create(mono) .expectNextCount(0) .expectComplete() .verify(); }
@Test public void emptyBodyWithRxJava2Single() throws Exception { ResolvableType type = httpEntityType(io.reactivex.Single.class, String.class); HttpEntity<io.reactivex.Single<String>> entity = resolveValueWithEmptyBody(type); StepVerifier.create(entity.getBody().toFlowable()) .expectNextCount(0) .expectError(ServerWebInputException.class) .verify(); }
@Test public void emptyBodyWithMono() throws Exception { ResolvableType type = httpEntityType(Mono.class, String.class); HttpEntity<Mono<String>> entity = resolveValueWithEmptyBody(type); StepVerifier.create(entity.getBody()).expectNextCount(0).expectComplete().verify(); }