|
| 1 | +package jooby; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.Writer; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import jooby.mvc.POST; |
| 10 | +import jooby.mvc.Path; |
| 11 | + |
| 12 | +import org.apache.http.client.fluent.Request; |
| 13 | +import org.apache.http.entity.ContentType; |
| 14 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +import com.google.common.collect.ImmutableList; |
| 18 | +import com.google.common.io.CharSink; |
| 19 | +import com.google.common.io.CharStreams; |
| 20 | +import com.google.inject.TypeLiteral; |
| 21 | + |
| 22 | +public class BodyParamFeature extends ServerFeature { |
| 23 | + |
| 24 | + private static class Bean { |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + @Path("/r") |
| 29 | + public static class Resource { |
| 30 | + |
| 31 | + @Path("/body") |
| 32 | + @POST |
| 33 | + public Bean body(final Bean param) throws IOException { |
| 34 | + return param; |
| 35 | + } |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + { |
| 40 | + |
| 41 | + use(new BodyConverter() { |
| 42 | + |
| 43 | + @Override |
| 44 | + public void write(final Object body, final BodyWriter writer) throws Exception { |
| 45 | + writer.text(out -> new CharSink() { |
| 46 | + @Override |
| 47 | + public Writer openStream() throws IOException { |
| 48 | + return out; |
| 49 | + } |
| 50 | + }.write("{}")); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public List<MediaType> types() { |
| 55 | + return ImmutableList.of(MediaType.json); |
| 56 | + } |
| 57 | + |
| 58 | + @SuppressWarnings("unchecked") |
| 59 | + @Override |
| 60 | + public <T> T read(final TypeLiteral<T> type, final BodyReader reader) throws Exception { |
| 61 | + String body = reader.text(in ->CharStreams.toString(in)); |
| 62 | + assertEquals("{}", body); |
| 63 | + return (T) new Bean(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean canWrite(final Class<?> type) { |
| 68 | + return type == Bean.class; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean canRead(final TypeLiteral<?> type) { |
| 73 | + return type.getRawType() == Bean.class; |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + post("/body", (req, resp) -> { |
| 78 | + Bean bean = req.param("param").to(Bean.class); |
| 79 | + resp.send(bean); |
| 80 | + }); |
| 81 | + |
| 82 | + use(Resource.class); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void multipart() throws Exception { |
| 87 | + assertEquals("{}", |
| 88 | + Request |
| 89 | + .Post(uri("body").build()) |
| 90 | + .body( |
| 91 | + MultipartEntityBuilder |
| 92 | + .create() |
| 93 | + .addTextBody("param", "{}", ContentType.APPLICATION_JSON) |
| 94 | + .build()).execute().returnContent().asString()); |
| 95 | + |
| 96 | + assertEquals("{}", |
| 97 | + Request.Post(uri("r", "body").build()) |
| 98 | + .body(MultipartEntityBuilder.create() |
| 99 | + .addTextBody("param", "{}", ContentType.APPLICATION_JSON) |
| 100 | + .build()).execute().returnContent().asString()); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments