@Override public Flux<DataBuffer> getBody() { return this.part.content(); } }
private String partToUtf8String(Part part) { DataBuffer buffer = DataBufferUtils.join(part.content()).block(); return DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8); }
@Test public void part() { MethodParameter param = this.testMethod.annot(requestPart()).arg(Part.class); MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.part("name", new Person("Jones")); Part actual = resolveArgument(param, bodyBuilder); DataBuffer buffer = DataBufferUtils.join(actual.content()).block(); assertEquals("{\"name\":\"Jones\"}", DataBufferTestUtils.dumpString(buffer, StandardCharsets.UTF_8)); }
private void assertFooPart(Part part) { assertEquals("fooPart", part.name()); assertTrue(part instanceof FilePart); assertEquals("foo.txt", ((FilePart) part).filename()); StepVerifier.create(DataBufferUtils.join(part.content())) .consumeNextWith(buffer -> { assertEquals(12, buffer.readableByteCount()); byte[] byteContent = new byte[12]; buffer.read(byteContent); assertEquals("Lorem Ipsum.", new String(byteContent)); }) .verifyComplete(); }
@Test public void resolveParts() { ServerHttpRequest request = generateMultipartRequest(); ResolvableType elementType = forClassWithGenerics(MultiValueMap.class, String.class, Part.class); MultiValueMap<String, Part> parts = this.reader.readMono(elementType, request, emptyMap()).block(); assertEquals(2, parts.size()); assertTrue(parts.containsKey("fooPart")); Part part = parts.getFirst("fooPart"); assertTrue(part instanceof FilePart); assertEquals("fooPart", part.name()); assertEquals("foo.txt", ((FilePart) part).filename()); DataBuffer buffer = DataBufferUtils.join(part.content()).block(); assertEquals(12, buffer.readableByteCount()); byte[] byteContent = new byte[12]; buffer.read(byteContent); assertEquals("Lorem Ipsum.", new String(byteContent)); assertTrue(parts.containsKey("barPart")); part = parts.getFirst("barPart"); assertTrue(part instanceof FormFieldPart); assertEquals("barPart", part.name()); assertEquals("bar", ((FormFieldPart) part).value()); }
assertEquals(MediaType.APPLICATION_JSON_UTF8, part.headers().getContentType()); String value = StringDecoder.textPlainOnly(false).decodeToMono(part.content(), ResolvableType.forClass(String.class), MediaType.TEXT_PLAIN, Collections.emptyMap()).block(Duration.ZERO); assertEquals("publisher", part.name()); value = StringDecoder.textPlainOnly(false).decodeToMono(part.content(), ResolvableType.forClass(String.class), MediaType.TEXT_PLAIN, Collections.emptyMap()).block(Duration.ZERO);
private ByteArrayOutputStream readPartBodyContent(Part part) { ByteArrayOutputStream contentStream = new ByteArrayOutputStream(); DataBufferUtils.write(part.content(), contentStream).blockFirst(); return contentStream; }