RequestParser { void parseBody(RequestImpl request, HttpServerExchange exchange) throws Throwable { var body = exchange.getAttachment(RequestBodyReader.REQUEST_BODY); if (body != null) { request.body = body.body(); logger.debug("[request] body={}", BodyLogParam.of(request.body, request.contentType)); } else { parseForm(request, exchange); } }  void parse(RequestImpl request, HttpServerExchange exchange, ActionLog actionLog); final ClientIPParser clientIPParser; public boolean logSiteHeaders; }
@Test void parseBody() throws Throwable { var request = new RequestImpl(null, null); request.contentType = ContentType.TEXT_XML; var exchange = new HttpServerExchange(null); byte[] body = Strings.bytes("<xml/>"); exchange.putAttachment(RequestBodyReader.REQUEST_BODY, new RequestBodyReader.RequestBody(body, null)); parser.parseBody(request, exchange); assertThat(request.body()).hasValue(body); }
@Test void parseBodyWithForm() throws Throwable { var request = new RequestImpl(null, null); var exchange = new HttpServerExchange(null); var form = new FormData(3); form.add("k1", "v1"); form.add("k2", "v2"); form.add("file", new byte[0], "file", new HeaderMap()); exchange.putAttachment(FormDataParser.FORM_DATA, form); parser.parseBody(request, exchange); assertThat(request.formParams).containsKeys("k1", "k2"); assertThat(request.files).containsKeys("file"); }
@Test void failedToReadBody() { var request = new RequestImpl(null, null); var exchange = new HttpServerExchange(null); exchange.putAttachment(RequestBodyReader.REQUEST_BODY, new RequestBodyReader.RequestBody(null, new IOException())); assertThatThrownBy(() -> parser.parseBody(request, exchange)) .isInstanceOf(BadRequestException.class) .satisfies(e -> assertThat(((ErrorCode) e).errorCode()).isEqualTo("FAILED_TO_READ_HTTP_REQUEST")); }