private <T> void testWrite( Publisher<T> source, MediaType mediaType, MockServerHttpResponse response, Class<T> clazz) { Mono<Void> result = this.messageWriter.write(source, forClass(clazz), mediaType, response, HINTS); StepVerifier.create(result) .verifyComplete(); }
@Test public void filterWithInvalidMethodValue() { StepVerifier.create(postForm("_method=INVALID")) .consumeErrorWith(error -> { assertThat(error, Matchers.instanceOf(IllegalArgumentException.class)); assertEquals("HttpMethod 'INVALID' not supported", error.getMessage()); }) .verify(); }
@Test public void bodyError() { ServerHttpRequest request = generateErrorMultipartRequest(); ResolvableType elementType = forClassWithGenerics(MultiValueMap.class, String.class, Part.class); StepVerifier.create(this.reader.readMono(elementType, request, emptyMap())).verifyError(); }
@Test public void decodeErrorNonAalto() { decoder.useAalto = false; Flux<DataBuffer> source = Flux.concat( stringBuffer("<pojo>"), Flux.error(new RuntimeException())); Flux<XMLEvent> events = this.decoder.decode(source, null, null, Collections.emptyMap()); StepVerifier.create(events) .expectError(RuntimeException.class) .verify(); }
@Test public void readResourcePosition() throws Exception { Flux<DataBuffer> flux = DataBufferUtils.read(this.resource, 9, this.bufferFactory, 3); StepVerifier.create(flux) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Test public void modelAttributeConventions() { Mono<RenderingResponse> result = RenderingResponse.create("foo") .modelAttribute("bar").build(); StepVerifier.create(result) .expectNextMatches(response -> "bar".equals(response.model().get("string"))) .expectComplete() .verify(); }
@Test // gh-22042 public void decodeWithNullLiteral() { Flux<Object> result = this.decoder.decode(Flux.concat(stringBuffer("null")), ResolvableType.forType(Pojo.class), MediaType.APPLICATION_JSON, Collections.emptyMap()); StepVerifier.create(result).expectComplete().verify(); }
@Test public void allow() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).allow(HttpMethod.GET).build(); Set<HttpMethod> expected = EnumSet.of(HttpMethod.GET); StepVerifier.create(result) .expectNextMatches(response -> expected.equals(response.headers().getAllow())) .expectComplete() .verify(); }
@Test public void unknownReturnType() { MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/unknown-return-type")); Mono<Void> publisher = this.dispatcherHandler.handle(exchange); StepVerifier.create(publisher) .consumeErrorWith(error -> { assertThat(error, instanceOf(IllegalStateException.class)); assertThat(error.getMessage(), startsWith("No HandlerResultHandler")); }) .verify(); }
@Test public void errorInStream() { DataBuffer buffer = stringBuffer("{\"id\":1,\"name\":"); Flux<DataBuffer> source = Flux.just(buffer) .concatWith(Flux.error(new RuntimeException())); Flux<TokenBuffer> result = Jackson2Tokenizer.tokenize(source, this.jsonFactory, true); StepVerifier.create(result) .expectError(RuntimeException.class) .verify(); }
@Test public void decodeSingleXmlRootElement() throws Exception { Mono<DataBuffer> source = stringBuffer(POJO_ROOT); Mono<Object> output = this.decoder.decodeToMono(source, ResolvableType.forClass(Pojo.class), null, Collections.emptyMap()); StepVerifier.create(output) .expectNext(new Pojo("foofoo", "barbar")) .expectComplete() .verify(); }
@Test public void readFormError() { DataBuffer fooBuffer = stringBuffer("name=value"); Flux<DataBuffer> body = Flux.just(fooBuffer).concatWith(Flux.error(new RuntimeException())); MockServerHttpRequest request = request(body); Flux<MultiValueMap<String, String>> result = this.reader.read(null, request, null); StepVerifier.create(result) .expectError() .verify(); }
@Test public void etag() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).eTag("foo").build(); StepVerifier.create(result) .expectNextMatches(response -> "\"foo\"".equals(response.headers().getETag())) .expectComplete() .verify(); }
private void verifyReadData(Flux<DataBuffer> buffers) { StepVerifier.create(buffers) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .consumeNextWith(stringConsumer("baz")) .consumeNextWith(stringConsumer("qux")) .expectComplete() .verify(Duration.ofSeconds(3)); }
@Test // SPR-17482 public void bodyToMonoVoidWithoutContentType() { this.server.enqueue(new MockResponse() .setResponseCode(HttpStatus.ACCEPTED.value()) .setChunkedBody("{\"foo\" : \"123\", \"baz\" : \"456\", \"baz\" : \"456\"}", 5)); Mono<Map<String, String>> mono = this.webClient.get() .uri("/sample").accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToMono(new ParameterizedTypeReference<Map<String, String>>() {}); StepVerifier.create(mono).expectError(UnsupportedMediaTypeException.class).verify(Duration.ofSeconds(3)); assertEquals(1, this.server.getRequestCount()); }
@Test public void takeUntilByteCount() { Flux<DataBuffer> result = DataBufferUtils.takeUntilByteCount( Flux.just(stringBuffer("foo"), stringBuffer("bar")), 5L); StepVerifier.create(result) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("ba")) .expectComplete() .verify(Duration.ofSeconds(5)); }
@Test public void unresolvedException() throws Exception { Mono<Void> mono = createWebHandler(new UnresolvedExceptionHandler()).handle(this.exchange); StepVerifier.create(mono).expectErrorMessage("boo").verify(); assertNull(this.exchange.getResponse().getStatusCode()); }
@Test public void multipartData() { Mono<ClientResponse> result = webClient .post() .uri("http://localhost:" + this.port + "/multipartData") .syncBody(generateBody()) .exchange(); StepVerifier .create(result) .consumeNextWith(response -> assertEquals(HttpStatus.OK, response.statusCode())) .verifyComplete(); }
@Test public void parts() { Mono<ClientResponse> result = webClient .post() .uri("http://localhost:" + this.port + "/parts") .syncBody(generateBody()) .exchange(); StepVerifier .create(result) .consumeNextWith(response -> assertEquals(HttpStatus.OK, response.statusCode())) .verifyComplete(); }
@Test public void contentType() { String body = "foo"; Mono<EntityResponse<String>> result = EntityResponse.fromObject(body).contentType(MediaType.APPLICATION_JSON).build(); StepVerifier.create(result) .expectNextMatches(response -> MediaType.APPLICATION_JSON.equals(response.headers().getContentType())) .expectComplete() .verify(); }