@Test // SPR-14952 public void writeAndFlushWithFluxOfDefaultDataBuffer() throws Exception { TestServerHttpResponse response = new TestServerHttpResponse(); Flux<Flux<DefaultDataBuffer>> flux = Flux.just(Flux.just(wrap("foo"))); response.writeAndFlushWith(flux).block(); assertTrue(response.statusCodeWritten); assertTrue(response.headersWritten); assertTrue(response.cookiesWritten); assertEquals(1, response.body.size()); assertEquals("foo", new String(response.body.get(0).asByteBuffer().array(), StandardCharsets.UTF_8)); }
@Test public void bodyToFluxParameterizedTypeReference() { Flux<String> result = Flux.just("foo"); ParameterizedTypeReference<String> reference = new ParameterizedTypeReference<String>() {}; when(mockRequest.bodyToFlux(reference)).thenReturn(result); assertSame(result, wrapper.bodyToFlux(reference)); }
@Test public void bodyToFluxParameterizedTypeReference() { Flux<String> result = Flux.just("foo"); ParameterizedTypeReference<String> reference = new ParameterizedTypeReference<String>() {}; when(mockResponse.bodyToFlux(reference)).thenReturn(result); assertSame(result, wrapper.bodyToFlux(reference)); }
private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) { byte[] bytes = value.getBytes(StandardCharsets.UTF_8); DataBuffer buffer = new DefaultDataBufferFactory().wrap(bytes); return exchange.getResponse().writeWith(Flux.just(buffer)); }
@Test // SPR-16402 public void singleSubscriberWithStrings() { UnicastProcessor<String> processor = UnicastProcessor.create(); Flux.just("foo", "bar", "baz").subscribe(processor); MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.asyncPart("name", processor, String.class); Mono<MultiValueMap<String, HttpEntity<?>>> result = Mono.just(bodyBuilder.build()); Map<String, Object> hints = Collections.emptyMap(); this.writer.write(result, null, MediaType.MULTIPART_FORM_DATA, this.response, hints).block(); }
@Test public void setContentLengthForMonoBody() { DefaultDataBufferFactory factory = new DefaultDataBufferFactory(); DataBuffer buffer = factory.wrap("body".getBytes(StandardCharsets.UTF_8)); HttpMessageWriter<String> writer = getWriter(Flux.just(buffer), MimeTypeUtils.TEXT_PLAIN); writer.write(Mono.just("body"), forClass(String.class), TEXT_PLAIN, this.response, NO_HINTS).block(); assertEquals(4, this.response.getHeaders().getContentLength()); }
@Test public void writeOneItem() throws Exception { Mono<Void> completion = Flux.just("one").as(this::sendOperator); Signal<Void> signal = completion.materialize().block(); assertNotNull(signal); assertTrue("Unexpected signal: " + signal, signal.isOnComplete()); assertEquals(1, this.writer.items.size()); assertEquals("one", this.writer.items.get(0)); assertTrue(this.writer.completed); }
private ServerHttpRequest generateErrorMultipartRequest() { return MockServerHttpRequest.post("/") .header(CONTENT_TYPE, MULTIPART_FORM_DATA.toString()) .body(Flux.just(new DefaultDataBufferFactory().wrap("invalid content".getBytes()))); }
@SuppressWarnings("unchecked") private void assertStringDecoder(Decoder<?> decoder, boolean textOnly) { assertEquals(StringDecoder.class, decoder.getClass()); assertTrue(decoder.canDecode(forClass(String.class), MimeTypeUtils.TEXT_PLAIN)); assertEquals(!textOnly, decoder.canDecode(forClass(String.class), MediaType.TEXT_EVENT_STREAM)); Flux<String> flux = (Flux<String>) decoder.decode( Flux.just(new DefaultDataBufferFactory().wrap("line1\nline2".getBytes(StandardCharsets.UTF_8))), ResolvableType.forClass(String.class), MimeTypeUtils.TEXT_PLAIN, Collections.emptyMap()); assertEquals(Arrays.asList("line1", "line2"), flux.collectList().block(Duration.ZERO)); }
@Override @Test public void encode() { Flux<ByteBuffer> input = Flux.just(this.fooBytes, this.barBytes) .map(ByteBuffer::wrap); testEncodeAll(input, ByteBuffer.class, step -> step .consumeNextWith(expectBytes(this.fooBytes)) .consumeNextWith(expectBytes(this.barBytes)) .verifyComplete()); } }
@Test public void decodeEmptyDataBuffer() { Flux<DataBuffer> input = Flux.just(stringBuffer("")); Flux<String> output = this.decoder.decode(input, TYPE, null, Collections.emptyMap()); StepVerifier.create(output) .expectNext("") .expectComplete().verify(); }
public Mono<ServerResponse> flux(ServerRequest request) { Person person1 = new Person("John"); Person person2 = new Person("Jane"); return ServerResponse.ok().body( fromPublisher(Flux.just(person1, person2), Person.class)); } }
@Test public void fromMultipartData() { MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.set("name 3", "value 3"); BodyInserters.FormInserter<Object> inserter = BodyInserters.fromMultipartData("name 1", "value 1") .withPublisher("name 2", Flux.just("foo", "bar", "baz"), String.class) .with(map); MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("http://example.com")); Mono<Void> result = inserter.insert(request, this.context); StepVerifier.create(result).expectComplete().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 writeWritableByteChannel() throws Exception { DataBuffer foo = stringBuffer("foo"); DataBuffer bar = stringBuffer("bar"); DataBuffer baz = stringBuffer("baz"); DataBuffer qux = stringBuffer("qux"); Flux<DataBuffer> flux = Flux.just(foo, bar, baz, qux); WritableByteChannel channel = Files.newByteChannel(tempFile, StandardOpenOption.WRITE); Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel); verifyWrittenData(writeResult); channel.close(); }
@Test // SPR-16521 public void jsonEOFExceptionIsWrappedAsDecodingError() { Flux<DataBuffer> source = Flux.just(stringBuffer("{\"status\": \"noClosingQuote}")); Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(source, this.jsonFactory, false); StepVerifier.create(tokens) .expectError(DecodingException.class) .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 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)); }
@Override public void encode() throws Exception { Flux<Object> input = Flux.just(new Pojo("foo", "bar"), new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar")); testEncodeAll(input, ResolvableType.forClass(Pojo.class), step -> step .consumeNextWith(expectString("{\"foo\":\"foo\",\"bar\":\"bar\"}\n")) .consumeNextWith(expectString("{\"foo\":\"foofoo\",\"bar\":\"barbar\"}\n")) .consumeNextWith(expectString("{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}\n")) .verifyComplete(), APPLICATION_STREAM_JSON, null); }