RecordedHttpBody incomingBody = incomingRequest.getHttpBody(); if (incomingBody == null) { incomingBody = new RecordedByteHttpBody(new byte[0]); expectedBody = new RecordedByteHttpBody(new byte[0]); expectedBody = new RecordedByteHttpBody(recordedBody.getBytes(charSet2));
@Override public boolean test(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest) { String charSet1 = incomingRequest.getCharset(); String charSet2 = expectedRequest.getCharset(); RecordedHttpBody incomingBody = incomingRequest.getHttpBody(); if (incomingBody == null) { incomingBody = new RecordedByteHttpBody(new byte[0]); } RecordedHttpBody expectedBody = expectedRequest.getHttpBody(); if (expectedBody == null) { expectedBody = new RecordedByteHttpBody(new byte[0]); } try { return Arrays.equals(incomingBody.getContent(charSet1), expectedBody.getContent(charSet2)); } catch (IOException e) { //TODO: PLACEHOLDER, error handling will be in separate RB. throw new RuntimeException("Failed to convert to byte arrays", e); } }
/** * Given content type, charset, construct concrete {@link com.linkedin.flashback.serializable.RecordedByteHttpBody} * @param contentType http body content type * @param contentEncoding http body content encoding * @param inputStream input stream from http request/response * @param charset charset * @return concrete {@link com.linkedin.flashback.serializable.RecordedHttpBody} * * */ public static RecordedHttpBody create(String contentType, String contentEncoding, final InputStream inputStream, final String charset) throws IOException { if (HttpUtilities.isCompressedContentEncoding(contentEncoding)) { return new RecordedEncodedHttpBody(IOUtils.toByteArray(inputStream), contentEncoding, charset, contentType); } else if (HttpUtilities.isTextContentType(contentType)) { return new RecordedStringHttpBody(IOUtils.toString(inputStream, charset)); } else { return new RecordedByteHttpBody(IOUtils.toByteArray(inputStream)); } } }
/** * Constructor used to create a RecordedEncodedHttpBody instance from the encoded (wire) bytes * * @param encodedContent the already encoded content, representing the bytes transferred over the wire * @param encodingName name of the encoding used to encode the content * @param charset charset * @param contentType http Content-Type for the content (once decoded) * @throws java.io.IOException */ public RecordedEncodedHttpBody(byte[] encodedContent, String encodingName, String charset, String contentType) throws IOException { _encodedContent = encodedContent; _encodingName = encodingName; byte[] decodedContent = getDecompressor().decompress(encodedContent); if (HttpUtilities.isTextContentType(contentType)) { _decodedBody = new RecordedStringHttpBody(new String(decodedContent, charset)); } else { _decodedBody = new RecordedByteHttpBody(decodedContent); } }
private RecordedHttpBody createHttpBody() throws IOException { if (isValidOptionalField(SceneSerializationConstant.SCENE_TAG_ENCODED_HTTPBODY)) { skipStartObject(); // ENCODEDHTTPBODY _jsonParser.nextToken(); // HTTPBODYENCODING Field validateRequiredField(SceneSerializationConstant.SCENE_TAG_HTTPBODY_ENCODING); _jsonParser.nextToken(); // HTTPBODYENCODING Value String encodingName = _jsonParser.getValueAsString(); _jsonParser.nextToken(); RecordedHttpBody decodedBody = createHttpBody(); // Read in the "decoded" body content so that we can wrap it skipEndObject(); return new RecordedEncodedHttpBody(decodedBody, encodingName); } if (isValidOptionalField(SceneSerializationConstant.SCENE_TAG_STRING_HTTPBODY)) { _jsonParser.nextToken(); // Field return new RecordedStringHttpBody(_jsonParser.getValueAsString()); } if (isValidOptionalField(SceneSerializationConstant.SCENE_TAG_BINARY_HTTPBODY)) { _jsonParser.nextToken(); // Field return new RecordedByteHttpBody(_jsonParser.getBinaryValue()); } return null; }
static Scene getMockSceneWithoutHeaders() throws URISyntaxException { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("GET", new URI("https://www.google.com"), null, new RecordedStringHttpBody("Hello world request is awesome")); RecordedHttpResponse recordedHttpResponse1 = new RecordedHttpResponse(200, null, new RecordedStringHttpBody("Hello world response is awesome")); RecordedHttpExchange recordedHttpExchange1 = new RecordedHttpExchange(recordedHttpRequest1, recordedHttpResponse1, new Date("2 Oct 2015 21:04:49 GMT")); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("GET", new URI("https://www.yahoo.com"), null, new RecordedStringHttpBody("Hello world request is awesome")); byte[] bytes = {1, 2, 3, 4, 5}; RecordedHttpResponse recordedHttpResponse2 = new RecordedHttpResponse(200, null, new RecordedByteHttpBody(bytes)); RecordedHttpExchange recordedHttpExchange2 = new RecordedHttpExchange(recordedHttpRequest2, recordedHttpResponse2, new Date("2 Oct 2015 21:04:49 GMT")); List<RecordedHttpExchange> recordedHttpExchangeList = new ArrayList<>(); recordedHttpExchangeList.add(recordedHttpExchange1); recordedHttpExchangeList.add(recordedHttpExchange2); return new Scene("testing", null, ".", recordedHttpExchangeList); }
byte[] bytes = {1, 2, 3, 4, 5}; RecordedHttpResponse recordedHttpResponse2 = new RecordedHttpResponse(200, responseHeaders2, new RecordedByteHttpBody(bytes)); RecordedHttpExchange recordedHttpExchange2 = new RecordedHttpExchange(recordedHttpRequest2, recordedHttpResponse2, new Date("2 Oct 2015 21:04:49 GMT"));