Reading body() before or after form() is not consistent across all servers
維護者通常 1 天內回覆
已經有一個關聯 PR 被合併了。
- #4034 來自 @jknack —— 已合併
評估
研究方向
The report identifies ctx.body().value() and ctx.form()/ctx.formMap() in a ServerTest that runs against Undertow and Jetty. Start by tracing how each server handles form parsing and body access, then run the supplied test against both servers. Done means the call order behaves consistently across both servers, or the server difference is documented.
由索引模型根據 Issue 內容生成。
描述
I was replacing jooby-undertow with jooby-jetty and I am getting different behaviour due to making a call to ctx.body().value() before ctx.form().toMap()
I expected this to be consistent across different servers, or at least for this difference to be documented.
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby.test;
import io.jooby.exception.MissingValueException;
import io.jooby.junit.ServerTest;
import io.jooby.junit.ServerTestRunner;
import java.util.Map;
import okhttp3.FormBody;
import org.junit.jupiter.api.Assertions;
class IssueBodyVsForm {
@ServerTest
public void formDataAndQueryParameters(ServerTestRunner runner) {
runner
.define(
app ->
app.post(
"/",
ctx -> {
try {
Assertions.assertAll(() -> {
Assertions.assertEquals(1, ctx.form().size(), "form contains one entry");
Assertions.assertTrue(ctx.form().isPresent());
Assertions.assertFalse(ctx.form().isMissing());
Assertions.assertEquals("bar", ctx.form().get("foo").value());
Assertions.assertEquals("bar", ctx.form("foo").value());
Assertions.assertEquals("bar", ctx.form().toMap().get("foo"));
Assertions.assertEquals("bar", ctx.formMap().get("foo"));
Assertions.assertEquals("?parameter1=value1", ctx.query().queryString());
Assertions.assertEquals("parameter1=value1", ctx.query().value());
Assertions.assertEquals("value1", ctx.query("parameter1").value());
Assertions.assertEquals("value1", ctx.queryMap().get("parameter1"));
Assertions.assertEquals(1, ctx.queryMap().size());
});
return "OK";
} catch (Exception ex){
ex.printStackTrace();
ctx.setResponseCode(500);
return ex.getMessage();
}
}))
.ready(
http ->
http
.post(
"/",
new FormBody.Builder().add("foo", "bar").build(),
Map.of("parameter1", "value1"),
rsp -> {
Assertions.assertEquals("OK", rsp.body().string());
Assertions.assertEquals(200, rsp.code());
}));
}
@ServerTest
public void readFormDataBeforeBody(ServerTestRunner runner) {
runner
.define(
app ->
app.post(
"/",
ctx -> {
try {
Assertions.assertAll(() -> {
Assertions.assertEquals(1, ctx.form().size(), "form contains one entry");
Assertions.assertTrue(ctx.form().isPresent());
Assertions.assertFalse(ctx.form().isMissing());
Assertions.assertEquals("bar", ctx.form().get("foo").value());
Assertions.assertEquals("bar", ctx.form("foo").value());
Assertions.assertEquals("bar", ctx.form().toMap().get("foo"));
Assertions.assertEquals("bar", ctx.formMap().get("foo"));
// get body().value() after calling form methods
Assertions.assertThrows(MissingValueException.class, () -> ctx.body().value());
});
return "OK";
} catch (Exception ex){
ex.printStackTrace();
ctx.setResponseCode(500);
return ex.getMessage();
}
}))
.ready(
http ->
http
.post(
"/",
new FormBody.Builder().add("foo", "bar").build(),
rsp -> {
Assertions.assertEquals("OK", rsp.body().string());
Assertions.assertEquals(200, rsp.code());
}));
}
@ServerTest
public void readFormDataAfterBody(ServerTestRunner runner) {
runner
.define(
app ->
app.post(
"/",
ctx -> {
try {
Assertions.assertAll(() -> {
// get body().value() before calling form methods
Assertions.assertThrows(MissingValueException.class, () -> ctx.body().value());
Assertions.assertEquals(1, ctx.form().size(), "form contains one entry");
Assertions.assertTrue(ctx.form().isPresent());
Assertions.assertFalse(ctx.form().isMissing());
Assertions.assertEquals("bar", ctx.form().get("foo").value());
Assertions.assertEquals("bar", ctx.form("foo").value());
Assertions.assertEquals("bar", ctx.form().toMap().get("foo"));
Assertions.assertEquals("bar", ctx.formMap().get("foo"));
});
return "OK";
} catch (Exception ex){
ex.printStackTrace();
ctx.setResponseCode(500);
return ex.getMessage();
}
}))
.ready(
http ->
http
.post(
"/",
new FormBody.Builder().add("foo", "bar").build(),
rsp -> {
Assertions.assertEquals("OK", rsp.body().string());
Assertions.assertEquals(200, rsp.code());
}));
}
}
- 主要語言
- Java
- 星號
- 1.8k
- 分支
- 207
- 平均合併
- 1 天 8 小時
- 30 天內合併 PR
- 20
環境準備
- 提供 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 沒有貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
jooby-project/jooby 的其他 Issue
-
難度 5/5 一週以上 新手友好度 25/100
jooby-project/jooby#4021 · 5 則留言 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 38/100
jooby-project/jooby#3766 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 25/100
jooby-project/jooby#3521 · 2 則留言 ·
維護者通常 1 天內回覆
-
question
難度 4/5 3-5 天 新手友好度 35/100
jooby-project/jooby#1513 · 10 則留言 ·
維護者通常 1 天內回覆
查看 jooby-project/jooby 的全部 Issue
相似的 Issue
-
Update samples to Citrus 5.1.0可能已有人在做 @asuruceanu 今天認領。 未關閉
難度 2/5 1-3 小時 新手友好度 82/100
-
難度 2/5 1-3 小時 新手友好度 72/100
snowflakedb/snowflake-kafka-connector#1610 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
swagger-api/swagger-petstore#245 ·
-
難度 2/5 1-3 小時 新手友好度 70/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
liquid-java/liquidjava#443 ·
維護者通常 2 天內回覆