public Flux<DataBuffer> getBody() { return (this.body != null ? this.body : Flux.error(new IllegalStateException("Body has not been written yet"))); }
/** * Configure a custom handler to consume the response body. * <p>By default, response body content is consumed in full and cached for * subsequent access in tests. Use this option to take control over how the * response body is consumed. * @param writeHandler the write handler to use returning {@code Mono<Void>} * when the body has been "written" (i.e. consumed). */ public void setWriteHandler(Function<Flux<DataBuffer>, Mono<Void>> writeHandler) { Assert.notNull(writeHandler, "'writeHandler' is required"); this.body = Flux.error(new IllegalStateException("Not available with custom write handler.")); this.writeHandler = writeHandler; }
/** * Configure a custom handler to consume the response body. * <p>By default, response body content is consumed in full and cached for * subsequent access in tests. Use this option to take control over how the * response body is consumed. * @param writeHandler the write handler to use returning {@code Mono<Void>} * when the body has been "written" (i.e. consumed). */ public void setWriteHandler(Function<Flux<DataBuffer>, Mono<Void>> writeHandler) { Assert.notNull(writeHandler, "'writeHandler' is required"); this.body = Flux.error(new IllegalStateException("Not available with custom write handler.")); this.writeHandler = writeHandler; }
private static <T> Flux<T> unsupportedErrorHandler( ReactiveHttpInputMessage message, UnsupportedMediaTypeException ex) { Flux<T> result; if (message.getHeaders().getContentType() == null) { // Maybe it's okay there is no content type, if there is no content.. result = message.getBody().map(buffer -> { DataBufferUtils.release(buffer); throw ex; }); } else { result = message instanceof ClientHttpResponse ? consumeAndCancel(message).thenMany(Flux.error(ex)) : Flux.error(ex); } return result; }
private Flux<TokenBuffer> tokenize(DataBuffer dataBuffer) { byte[] bytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); DataBufferUtils.release(dataBuffer); try { this.inputFeeder.feedInput(bytes, 0, bytes.length); return parseTokenBufferFlux(); } catch (JsonProcessingException ex) { return Flux.error(new DecodingException( "JSON decoding error: " + ex.getOriginalMessage(), ex)); } catch (IOException ex) { return Flux.error(ex); } }
@Override public Mono<Void> handle(WebSocketSession session) { return session.send(Flux .error(new Throwable()) .onErrorResume(ex -> session.close(CloseStatus.GOING_AWAY)) // SPR-17306 (nested close) .cast(WebSocketMessage.class)); } }
@Override protected void testEncodeError(Publisher<?> input, ResolvableType outputType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { Flux<Resource> i = Flux.error(new InputException()); Flux<DataBuffer> result = ((Encoder<Resource>) this.encoder).encode(i, this.bufferFactory, outputType, mimeType, hints); StepVerifier.create(result) .expectError(InputException.class) .verify(); }
@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 encodeError() { Flux<Pojo> input = Flux.error(RuntimeException::new); testEncode(input, Pojo.class, step -> step .expectError(RuntimeException.class) .verify()); }
@Test public void writeWithError() throws Exception { TestServerHttpResponse response = new TestServerHttpResponse(); response.getHeaders().setContentLength(12); IllegalStateException error = new IllegalStateException("boo"); response.writeWith(Flux.error(error)).onErrorResume(ex -> Mono.empty()).block(); assertFalse(response.statusCodeWritten); assertFalse(response.headersWritten); assertFalse(response.cookiesWritten); assertFalse(response.getHeaders().containsKey(HttpHeaders.CONTENT_LENGTH)); assertTrue(response.body.isEmpty()); }
@Override protected void testDecodeError(Publisher<DataBuffer> input, ResolvableType outputType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { input = Flux.concat( Flux.from(input).take(1), Flux.error(new InputException())); Flux<Resource> result = this.decoder.decode(input, outputType, mimeType, hints); StepVerifier.create(result) .expectError(InputException.class) .verify(); }
@Override protected void testDecodeError(Publisher<DataBuffer> input, ResolvableType outputType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { input = Flux.concat( Flux.from(input).take(1), Flux.error(new InputException())); Flux<String> result = this.decoder.decode(input, outputType, mimeType, hints); StepVerifier.create(result) .expectError(InputException.class) .verify(); }
@Test public void decodeError() throws Exception { Flux<DataBuffer> source = Flux.concat( stringBuffer("<pojo>"), Flux.error(new RuntimeException())); Mono<Object> output = this.decoder.decodeToMono(source, ResolvableType.forClass(Pojo.class), null, Collections.emptyMap()); StepVerifier.create(output) .expectError(RuntimeException.class) .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 writeWritableByteChannelErrorInFlux() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Flux.error(new RuntimeException())); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); StepVerifier.create(writeResult) .consumeNextWith(stringConsumer("foo")) .consumeNextWith(stringConsumer("bar")) .expectError() .verify(Duration.ofSeconds(5)); String result = String.join("", Files.readAllLines(tempFile)); assertEquals("foobar", result); channel.close(); }
@Test public void readError() { Flux<DataBuffer> body = Flux.just(stringBuffer("data:foo\ndata:bar\n\ndata:baz\n\n")) .concatWith(Flux.error(new RuntimeException())); MockServerHttpRequest request = MockServerHttpRequest.post("/") .body(body); Flux<String> data = messageReader.read(ResolvableType.forClass(String.class), request, Collections.emptyMap()).cast(String.class); StepVerifier.create(data) .expectNextMatches(elem -> elem.equals("foo\nbar")) .expectNextMatches(elem -> elem.equals("baz")) .expectError() .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 joinErrors() { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); Flux<DataBuffer> flux = Flux.just(foo, bar).concatWith(Flux.error(new RuntimeException())); Mono<DataBuffer> result = DataBufferUtils.join(flux); StepVerifier.create(result) .expectError(RuntimeException.class) .verify(); }
@Test public void decodeErrorAalto() { 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) .consumeNextWith(e -> assertTrue(e.isStartDocument())) .consumeNextWith(e -> assertStartElement(e, "pojo")) .expectError(RuntimeException.class) .verify(); }