Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Reading body() before or after form() is not consistent across all servers

Chiusa
#4,031 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Una pull request collegata è già stata integrata.

  • #4034 di @jknack — integrata

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
java
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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());
                        }));
  }
}
Lingua principale
Java
Stelle
1.8k
Fork
207
Merge medio
1g 8h
PR unite (30g)
20

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di jooby-project/jooby

Tutte le issue di jooby-project/jooby

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.