|
| 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.util.Map; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.Assertions; |
| 11 | + |
| 12 | +import io.jooby.exception.MissingValueException; |
| 13 | +import io.jooby.junit.ServerTest; |
| 14 | +import io.jooby.junit.ServerTestRunner; |
| 15 | +import okhttp3.FormBody; |
| 16 | +import okhttp3.MultipartBody; |
| 17 | + |
| 18 | +class Issue4031 { |
| 19 | + |
| 20 | + @ServerTest |
| 21 | + public void formDataAndQueryParameters(ServerTestRunner runner) { |
| 22 | + runner |
| 23 | + .define( |
| 24 | + app -> |
| 25 | + app.post( |
| 26 | + "/", |
| 27 | + ctx -> { |
| 28 | + try { |
| 29 | + Assertions.assertAll( |
| 30 | + () -> { |
| 31 | + Assertions.assertEquals( |
| 32 | + 1, ctx.form().size(), "form contains one entry"); |
| 33 | + Assertions.assertTrue(ctx.form().isPresent()); |
| 34 | + Assertions.assertFalse(ctx.form().isMissing()); |
| 35 | + Assertions.assertEquals("bar", ctx.form().get("foo").value()); |
| 36 | + Assertions.assertEquals("bar", ctx.form("foo").value()); |
| 37 | + Assertions.assertEquals("bar", ctx.form().toMap().get("foo")); |
| 38 | + Assertions.assertEquals("bar", ctx.formMap().get("foo")); |
| 39 | + |
| 40 | + Assertions.assertEquals( |
| 41 | + "?parameter1=value1", ctx.query().queryString()); |
| 42 | + Assertions.assertEquals("parameter1=value1", ctx.query().value()); |
| 43 | + Assertions.assertEquals("value1", ctx.query("parameter1").value()); |
| 44 | + Assertions.assertEquals("value1", ctx.queryMap().get("parameter1")); |
| 45 | + Assertions.assertEquals(1, ctx.queryMap().size()); |
| 46 | + }); |
| 47 | + return "OK"; |
| 48 | + } catch (Exception ex) { |
| 49 | + ctx.setResponseCode(500); |
| 50 | + return ex.getMessage(); |
| 51 | + } |
| 52 | + })) |
| 53 | + .ready( |
| 54 | + http -> { |
| 55 | + http.post( |
| 56 | + "/", |
| 57 | + new FormBody.Builder().add("foo", "bar").build(), |
| 58 | + Map.of("parameter1", "value1"), |
| 59 | + rsp -> { |
| 60 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 61 | + Assertions.assertEquals(200, rsp.code()); |
| 62 | + }); |
| 63 | + |
| 64 | + http.post( |
| 65 | + "/", |
| 66 | + new MultipartBody.Builder() |
| 67 | + .addFormDataPart("foo", "bar") |
| 68 | + .setType(MultipartBody.FORM) |
| 69 | + .build(), |
| 70 | + Map.of("parameter1", "value1"), |
| 71 | + rsp -> { |
| 72 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 73 | + Assertions.assertEquals(200, rsp.code()); |
| 74 | + }); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + @ServerTest |
| 79 | + public void readFormDataBeforeBody(ServerTestRunner runner) { |
| 80 | + runner |
| 81 | + .define( |
| 82 | + app -> |
| 83 | + app.post( |
| 84 | + "/", |
| 85 | + ctx -> { |
| 86 | + try { |
| 87 | + Assertions.assertAll( |
| 88 | + () -> { |
| 89 | + Assertions.assertEquals( |
| 90 | + 1, ctx.form().size(), "form contains one entry"); |
| 91 | + Assertions.assertTrue(ctx.form().isPresent()); |
| 92 | + Assertions.assertFalse(ctx.form().isMissing()); |
| 93 | + Assertions.assertEquals("bar", ctx.form().get("foo").value()); |
| 94 | + Assertions.assertEquals("bar", ctx.form("foo").value()); |
| 95 | + Assertions.assertEquals("bar", ctx.form().toMap().get("foo")); |
| 96 | + Assertions.assertEquals("bar", ctx.formMap().get("foo")); |
| 97 | + |
| 98 | + // get body().value() after calling form methods |
| 99 | + Assertions.assertThrows( |
| 100 | + MissingValueException.class, () -> ctx.body().value()); |
| 101 | + }); |
| 102 | + return "OK"; |
| 103 | + } catch (Exception ex) { |
| 104 | + ctx.setResponseCode(500); |
| 105 | + return ex.getMessage(); |
| 106 | + } |
| 107 | + })) |
| 108 | + .ready( |
| 109 | + http -> { |
| 110 | + http.post( |
| 111 | + "/", |
| 112 | + new FormBody.Builder().add("foo", "bar").build(), |
| 113 | + rsp -> { |
| 114 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 115 | + Assertions.assertEquals(200, rsp.code()); |
| 116 | + }); |
| 117 | + |
| 118 | + http.post( |
| 119 | + "/", |
| 120 | + new MultipartBody.Builder() |
| 121 | + .addFormDataPart("foo", "bar") |
| 122 | + .setType(MultipartBody.FORM) |
| 123 | + .build(), |
| 124 | + rsp -> { |
| 125 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 126 | + Assertions.assertEquals(200, rsp.code()); |
| 127 | + }); |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + @ServerTest |
| 132 | + public void readFormDataAfterBody(ServerTestRunner runner) { |
| 133 | + runner |
| 134 | + .define( |
| 135 | + app -> |
| 136 | + app.post( |
| 137 | + "/", |
| 138 | + ctx -> { |
| 139 | + try { |
| 140 | + Assertions.assertAll( |
| 141 | + () -> { |
| 142 | + // get body().value() before calling form methods |
| 143 | + Assertions.assertThrows( |
| 144 | + MissingValueException.class, () -> ctx.body().value()); |
| 145 | + |
| 146 | + Assertions.assertEquals( |
| 147 | + 1, ctx.form().size(), "form contains one entry"); |
| 148 | + Assertions.assertTrue(ctx.form().isPresent()); |
| 149 | + Assertions.assertFalse(ctx.form().isMissing()); |
| 150 | + Assertions.assertEquals("bar", ctx.form().get("foo").value()); |
| 151 | + Assertions.assertEquals("bar", ctx.form("foo").value()); |
| 152 | + Assertions.assertEquals("bar", ctx.form().toMap().get("foo")); |
| 153 | + Assertions.assertEquals("bar", ctx.formMap().get("foo")); |
| 154 | + }); |
| 155 | + return "OK"; |
| 156 | + } catch (Exception ex) { |
| 157 | + ex.printStackTrace(); |
| 158 | + ctx.setResponseCode(500); |
| 159 | + return ex.getMessage(); |
| 160 | + } |
| 161 | + })) |
| 162 | + .ready( |
| 163 | + http -> { |
| 164 | + http.post( |
| 165 | + "/", |
| 166 | + new FormBody.Builder().add("foo", "bar").build(), |
| 167 | + rsp -> { |
| 168 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 169 | + Assertions.assertEquals(200, rsp.code()); |
| 170 | + }); |
| 171 | + |
| 172 | + http.post( |
| 173 | + "/", |
| 174 | + new MultipartBody.Builder() |
| 175 | + .setType(MultipartBody.FORM) |
| 176 | + .addFormDataPart("foo", "bar") |
| 177 | + .build(), |
| 178 | + rsp -> { |
| 179 | + Assertions.assertEquals("OK", rsp.body().string()); |
| 180 | + Assertions.assertEquals(200, rsp.code()); |
| 181 | + }); |
| 182 | + }); |
| 183 | + } |
| 184 | +} |
0 commit comments