public void copyFrom(Request other) { this.postParams.copyFrom(other.postParams); this.headers.copyFrom(other.headers); this.httpVersion = other.httpVersion; this.method = other.method; this.socket.copyFrom(other.socket); this.url.copyFrom(other.url); this.cookies.copyFrom(other.cookies); if (other.bodyBuffer != null) { final CharBuffer otherBuffer = other.getBodyBuffer(); final CharBuffer thisBuffer = this.withBodyBuffer(); for (int i = 0; i < otherBuffer.length(); i++) { thisBuffer.append(otherBuffer.charAt(i)); } thisBuffer.flip(); } }
@Test void testBodyBuffer() throws IOException { final Transaction transaction = createTransactionWithRequiredValues(); final CharBuffer bodyBuffer = transaction.getContext().getRequest().withBodyBuffer(); IOUtils.decodeUtf8Bytes("{f".getBytes(StandardCharsets.UTF_8), bodyBuffer); IOUtils.decodeUtf8Bytes(new byte[]{0, 0, 'o', 'o', 0}, 2, 2, bodyBuffer); IOUtils.decodeUtf8Byte((byte) '}', bodyBuffer); bodyBuffer.flip(); final String content = serializer.toJsonString(transaction); System.out.println(content); final JsonNode transactionJson = objectMapper.readTree(content); assertThat(transactionJson.get("context").get("request").get("body").textValue()).isEqualTo("{foo}"); transaction.resetState(); assertThat((Object) transaction.getContext().getRequest().getBodyBuffer()).isNull(); }
private void serializeRequest(final Request request) { if (request.hasContent()) { writeFieldName("request"); jw.writeByte(OBJECT_START); writeField("method", request.getMethod()); writeField("headers", request.getHeaders()); writeField("cookies", request.getCookies()); // only one of those can be non-empty if (!request.getFormUrlEncodedParameters().isEmpty()) { writeField("body", request.getFormUrlEncodedParameters()); } else if (request.getBodyBuffer() != null && request.getBodyBuffer().length() > 0) { writeFieldName("body"); jw.writeString(request.getBodyBuffer()); jw.writeByte(COMMA); } if (request.getUrl().hasContent()) { serializeUrl(request.getUrl()); } if (request.getSocket().hasContent()) { serializeSocket(request.getSocket()); } writeLastField("http_version", request.getHttpVersion()); jw.writeByte(OBJECT_END); jw.writeByte(COMMA); } }