Search in sources :

Example 56 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.

the class RequestHeaderMethodArgumentResolverTests method resolveDefaultValue.

@Test
public void resolveDefaultValue() throws Exception {
    MockServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
    Mono<Object> mono = this.resolver.resolveArgument(this.paramNamedDefaultValueStringHeader, this.bindingContext, exchange);
    Object result = mono.block();
    assertTrue(result instanceof String);
    assertEquals("bar", result);
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) Test(org.junit.Test)

Example 57 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.

the class ViewResolutionResultHandlerTests method modelWithAsyncAttributes.

@Test
public void modelWithAsyncAttributes() throws Exception {
    this.bindingContext.getModel().addAttribute("attr1", Mono.just(new TestBean("Bean1"))).addAttribute("attr2", Flux.just(new TestBean("Bean1"), new TestBean("Bean2"))).addAttribute("attr3", Single.just(new TestBean("Bean2"))).addAttribute("attr4", Observable.just(new TestBean("Bean1"), new TestBean("Bean2"))).addAttribute("attr5", Mono.empty());
    MethodParameter returnType = on(TestController.class).resolveReturnType(void.class);
    HandlerResult result = new HandlerResult(new Object(), null, returnType, this.bindingContext);
    ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account"));
    MockServerWebExchange exchange = get("/account").toExchange();
    handler.handleResult(exchange, result).block(Duration.ofMillis(5000));
    assertResponseBody(exchange, "account: {" + "attr1=TestBean[name=Bean1], " + "attr2=[TestBean[name=Bean1], TestBean[name=Bean2]], " + "attr3=TestBean[name=Bean2], " + "attr4=[TestBean[name=Bean1], TestBean[name=Bean2]], " + "org.springframework.validation.BindingResult.attr1=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors, " + "org.springframework.validation.BindingResult.attr3=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors" + "}");
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 58 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.

the class ViewResolutionResultHandlerTests method contentNegotiationWith406.

@Test
public void contentNegotiationWith406() throws Exception {
    TestBean value = new TestBean("Joe");
    MethodParameter returnType = on(TestController.class).resolveReturnType(TestBean.class);
    HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext);
    MockServerWebExchange exchange = get("/account").accept(APPLICATION_JSON).toExchange();
    ViewResolutionResultHandler resultHandler = resultHandler(new TestViewResolver("account"));
    Mono<Void> mono = resultHandler.handleResult(exchange, handlerResult);
    StepVerifier.create(mono).expectNextCount(0).expectError(NotAcceptableStatusException.class).verify();
}
Also used : NotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 59 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method statusCode.

@Test
public void statusCode() throws Exception {
    ResponseEntity<Void> value = ResponseEntity.noContent().build();
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = get("/path").toExchange();
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertEquals(HttpStatus.NO_CONTENT, exchange.getResponse().getStatusCode());
    assertEquals(0, exchange.getResponse().getHeaders().size());
    assertResponseBodyIsEmpty(exchange);
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 60 with MockServerWebExchange

use of org.springframework.mock.http.server.reactive.test.MockServerWebExchange in project spring-framework by spring-projects.

the class HttpMessageWriterViewTests method render.

@Test
public void render() throws Exception {
    Map<String, String> pojoData = new LinkedHashMap<>();
    pojoData.put("foo", "f");
    pojoData.put("bar", "b");
    this.model.addAttribute("pojoData", pojoData);
    this.view.setModelKeys(Collections.singleton("pojoData"));
    MockServerWebExchange exchange = MockServerHttpRequest.get("/path").toExchange();
    this.view.render(this.model, MediaType.APPLICATION_JSON, exchange).block(Duration.ofSeconds(5));
    StepVerifier.create(exchange.getResponse().getBody()).consumeNextWith(buf -> assertEquals("{\"foo\":\"f\",\"bar\":\"b\"}", DataBufferTestUtils.dumpString(buf, StandardCharsets.UTF_8))).expectComplete().verify();
}
Also used : MockServerWebExchange(org.springframework.mock.http.server.reactive.test.MockServerWebExchange) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)135 Test (org.junit.Test)127 ClassPathResource (org.springframework.core.io.ClassPathResource)19 Resource (org.springframework.core.io.Resource)19 HandlerResult (org.springframework.web.reactive.HandlerResult)17 MethodParameter (org.springframework.core.MethodParameter)15 MediaType (org.springframework.http.MediaType)8 Instant (java.time.Instant)7 HttpHeaders (org.springframework.http.HttpHeaders)5 MockServerHttpResponse (org.springframework.mock.http.server.reactive.test.MockServerHttpResponse)5 FileSystemResource (org.springframework.core.io.FileSystemResource)3 UrlResource (org.springframework.core.io.UrlResource)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)3 CompositeContentTypeResolver (org.springframework.web.reactive.accept.CompositeContentTypeResolver)3 RequestedContentTypeResolverBuilder (org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Assert.assertSame (org.junit.Assert.assertSame)2 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)2