|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.i4031; |
| 7 | + |
| 8 | +import java.net.URLEncoder; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Assertions; |
| 13 | + |
| 14 | +import io.jooby.exception.MissingValueException; |
| 15 | +import io.jooby.junit.ServerTest; |
| 16 | +import io.jooby.junit.ServerTestRunner; |
| 17 | +import okhttp3.FormBody; |
| 18 | +import okhttp3.MediaType; |
| 19 | +import okhttp3.MultipartBody; |
| 20 | +import okhttp3.RequestBody; |
| 21 | + |
| 22 | +class Issue4031 { |
| 23 | + |
| 24 | + @ServerTest |
| 25 | + public void formDataAndQueryParameters(ServerTestRunner runner) { |
| 26 | + runner |
| 27 | + .define( |
| 28 | + app -> |
| 29 | + app.post( |
| 30 | + "/", |
| 31 | + ctx -> { |
| 32 | + try { |
| 33 | + Assertions.assertAll( |
| 34 | + () -> |
| 35 | + Assertions.assertEquals( |
| 36 | + 1, ctx.form().size(), "form contains one entry"), |
| 37 | + () -> Assertions.assertTrue(ctx.form().isPresent()), |
| 38 | + () -> Assertions.assertFalse(ctx.form().isMissing()), |
| 39 | + () -> Assertions.assertEquals("bar", ctx.form().get("foo").value()), |
| 40 | + () -> Assertions.assertEquals("bar", ctx.form("foo").value()), |
| 41 | + () -> Assertions.assertEquals("bar", ctx.form().toMap().get("foo")), |
| 42 | + () -> Assertions.assertEquals("bar", ctx.formMap().get("foo")), |
| 43 | + () -> |
| 44 | + Assertions.assertEquals( |
| 45 | + "?parameter1=value1", ctx.query().queryString()), |
| 46 | + () -> Assertions.assertEquals("parameter1=value1", ctx.query().value()), |
| 47 | + () -> |
| 48 | + Assertions.assertEquals("value1", ctx.query("parameter1").value()), |
| 49 | + () -> |
| 50 | + Assertions.assertEquals("value1", ctx.queryMap().get("parameter1")), |
| 51 | + () -> Assertions.assertEquals(1, ctx.queryMap().size())); |
| 52 | + return "OK"; |
| 53 | + } catch (Exception ex) { |
| 54 | + ctx.setResponseCode(500); |
| 55 | + return ex.getMessage(); |
| 56 | + } |
| 57 | + })) |
| 58 | + .ready( |
| 59 | + http -> { |
| 60 | + http.post( |
| 61 | + "/", |
| 62 | + new FormBody.Builder().add("foo", "bar").build(), |
| 63 | + Map.of("parameter1", "value1"), |
| 64 | + rsp -> { |
| 65 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 66 | + Assertions.assertEquals(200, rsp.code()); |
| 67 | + }); |
| 68 | + |
| 69 | + // form with bad media type |
| 70 | + http.post( |
| 71 | + "/", |
| 72 | + customForm( |
| 73 | + Map.of("foo", "bar"), "application/x-www-form-urlencoded; charset=utf-8"), |
| 74 | + Map.of("parameter1", "value1"), |
| 75 | + rsp -> { |
| 76 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 77 | + Assertions.assertEquals(200, rsp.code()); |
| 78 | + }); |
| 79 | + |
| 80 | + http.post( |
| 81 | + "/", |
| 82 | + customForm( |
| 83 | + Map.of("foo", "bar"), "application/x-www-form-urlencoded;charset=iso-8859-1"), |
| 84 | + Map.of("parameter1", "value1"), |
| 85 | + rsp -> { |
| 86 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 87 | + Assertions.assertEquals(200, rsp.code()); |
| 88 | + }); |
| 89 | + |
| 90 | + // multipart |
| 91 | + http.post( |
| 92 | + "/", |
| 93 | + new MultipartBody.Builder() |
| 94 | + .addFormDataPart("foo", "bar") |
| 95 | + .setType(MultipartBody.FORM) |
| 96 | + .build(), |
| 97 | + Map.of("parameter1", "value1"), |
| 98 | + rsp -> { |
| 99 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 100 | + Assertions.assertEquals(200, rsp.code()); |
| 101 | + }); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + private static RequestBody customForm(Map<String, Object> form, String mediaType) { |
| 106 | + var payload = |
| 107 | + form.entrySet().stream() |
| 108 | + .map( |
| 109 | + entry -> |
| 110 | + entry.getKey() |
| 111 | + + "=" |
| 112 | + + URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8)) |
| 113 | + .reduce((a, b) -> a + "&" + b) |
| 114 | + .orElse(""); |
| 115 | + |
| 116 | + return RequestBody.create(payload, MediaType.parse(mediaType)); |
| 117 | + } |
| 118 | + |
| 119 | + @ServerTest |
| 120 | + public void readFormDataAfterBody(ServerTestRunner runner) { |
| 121 | + runner |
| 122 | + .define( |
| 123 | + app -> |
| 124 | + app.post( |
| 125 | + "/", |
| 126 | + ctx -> { |
| 127 | + try { |
| 128 | + Assertions.assertAll( |
| 129 | + () -> |
| 130 | + Assertions.assertEquals( |
| 131 | + 1, ctx.form().size(), "form contains one entry"), |
| 132 | + () -> Assertions.assertTrue(ctx.form().isPresent()), |
| 133 | + () -> Assertions.assertFalse(ctx.form().isMissing()), |
| 134 | + () -> Assertions.assertEquals("bar", ctx.form().get("foo").value()), |
| 135 | + () -> Assertions.assertEquals("bar", ctx.form("foo").value()), |
| 136 | + () -> Assertions.assertEquals("bar", ctx.form().toMap().get("foo")), |
| 137 | + () -> Assertions.assertEquals("bar", ctx.formMap().get("foo")), |
| 138 | + // get body().value() after calling form methods |
| 139 | + () -> |
| 140 | + Assertions.assertThrows( |
| 141 | + MissingValueException.class, () -> ctx.body().value())); |
| 142 | + return "OK"; |
| 143 | + } catch (Exception ex) { |
| 144 | + ctx.setResponseCode(500); |
| 145 | + return ex.getMessage(); |
| 146 | + } |
| 147 | + })) |
| 148 | + .ready( |
| 149 | + http -> { |
| 150 | + http.post( |
| 151 | + "/", |
| 152 | + new FormBody.Builder().add("foo", "bar").build(), |
| 153 | + rsp -> { |
| 154 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 155 | + Assertions.assertEquals(200, rsp.code()); |
| 156 | + }); |
| 157 | + |
| 158 | + http.post( |
| 159 | + "/", |
| 160 | + customForm( |
| 161 | + Map.of("foo", "bar"), "application/x-www-form-urlencoded; charset=utf-8"), |
| 162 | + rsp -> { |
| 163 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 164 | + Assertions.assertEquals(200, rsp.code()); |
| 165 | + }); |
| 166 | + |
| 167 | + http.post( |
| 168 | + "/", |
| 169 | + new MultipartBody.Builder() |
| 170 | + .addFormDataPart("foo", "bar") |
| 171 | + .setType(MultipartBody.FORM) |
| 172 | + .build(), |
| 173 | + rsp -> { |
| 174 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 175 | + Assertions.assertEquals(200, rsp.code()); |
| 176 | + }); |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + @ServerTest |
| 181 | + public void readFormDataBeforeBody(ServerTestRunner runner) { |
| 182 | + runner |
| 183 | + .define( |
| 184 | + app -> |
| 185 | + app.post( |
| 186 | + "/", |
| 187 | + ctx -> { |
| 188 | + try { |
| 189 | + Assertions.assertAll( |
| 190 | + // get body().value() before calling form methods |
| 191 | + () -> |
| 192 | + Assertions.assertThrows( |
| 193 | + MissingValueException.class, () -> ctx.body().value()), |
| 194 | + () -> |
| 195 | + Assertions.assertEquals( |
| 196 | + 1, ctx.form().size(), "form contains one entry"), |
| 197 | + () -> Assertions.assertTrue(ctx.form().isPresent()), |
| 198 | + () -> Assertions.assertFalse(ctx.form().isMissing()), |
| 199 | + () -> Assertions.assertEquals("bar", ctx.form().get("foo").value()), |
| 200 | + () -> Assertions.assertEquals("bar", ctx.form("foo").value()), |
| 201 | + () -> Assertions.assertEquals("bar", ctx.form().toMap().get("foo")), |
| 202 | + () -> Assertions.assertEquals("bar", ctx.formMap().get("foo"))); |
| 203 | + return "OK"; |
| 204 | + } catch (Exception ex) { |
| 205 | + ctx.setResponseCode(500); |
| 206 | + return ex.getMessage(); |
| 207 | + } |
| 208 | + })) |
| 209 | + .ready( |
| 210 | + http -> { |
| 211 | + // http.post( |
| 212 | + // "/", |
| 213 | + // new FormBody.Builder().add("foo", "bar").build(), |
| 214 | + // rsp -> { |
| 215 | + // Assertions.assertEquals("OK", rsp.body().string()); |
| 216 | + // Assertions.assertEquals(200, rsp.code()); |
| 217 | + // }); |
| 218 | + |
| 219 | + http.post( |
| 220 | + "/", |
| 221 | + customForm( |
| 222 | + Map.of("foo", "bar"), "application/x-www-form-urlencoded; charset=utf-8"), |
| 223 | + rsp -> { |
| 224 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 225 | + Assertions.assertEquals(200, rsp.code()); |
| 226 | + }); |
| 227 | + |
| 228 | + // http.post( |
| 229 | + // "/", |
| 230 | + // new MultipartBody.Builder() |
| 231 | + // .setType(MultipartBody.FORM) |
| 232 | + // .addFormDataPart("foo", "bar") |
| 233 | + // .build(), |
| 234 | + // rsp -> { |
| 235 | + // Assertions.assertEquals("OK", rsp.body().string()); |
| 236 | + // Assertions.assertEquals(200, rsp.code()); |
| 237 | + // }); |
| 238 | + }); |
| 239 | + } |
| 240 | +} |
0 commit comments