@Test public void intercept_performed_regex() throws Exception { Replacement replacement = new Replacement(RegexHolder.of(" h(\\S+)"), StringLiteral.of(" x$1")); MediaType contentType = MediaType.PLAIN_TEXT_UTF_8; Charset charset = contentType.charset().get(); byte[] body = "We said hello to horticulturalists with handkerchiefs".getBytes(charset); String expected = "We said xello to xorticulturalists with xandkerchiefs"; HttpRespondable actual = doIntercept(replacement, body, contentType); WritingActionResult<MediaType> writeResult = ReplacingInterceptor.writeByteArray(actual::writeBody, expected.length()); String actualText = new String(writeResult.byteArray, writeResult.actionReturnValue.charset().get()); assertEquals("actual bytes", expected, actualText); assertEquals("content type", contentType, writeResult.actionReturnValue); }
protected static <T> WritingActionResult<T> writeByteArray(WritingAction<T> action, int expectedOutputLength) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(expectedOutputLength); T returnValue = action.write(baos); baos.flush(); byte[] data = baos.toByteArray(); return new WritingActionResult<>(returnValue, data); }
ReplacingInterceptor implements ResponseInterceptor { protected static <T> WritingActionResult<T> writeByteArray(WritingAction<T> action, int expectedOutputLength) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(expectedOutputLength); T returnValue = action.write(baos); baos.flush(); byte[] data = baos.toByteArray(); return new WritingActionResult<>(returnValue, data); } }
ReplacingInterceptor implements ResponseInterceptor { protected static <T> WritingActionResult<T> writeByteArray(WritingAction<T> action, int expectedOutputLength) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(expectedOutputLength); T returnValue = action.write(baos); baos.flush(); byte[] data = baos.toByteArray(); return new WritingActionResult<>(returnValue, data); } ReplacingInterceptor(VhsReplayManagerConfig config, Replacement replacement); }
ReplacingInterceptor implements ResponseInterceptor { protected static <T> WritingActionResult<T> writeByteArray(WritingAction<T> action, int expectedOutputLength) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(expectedOutputLength); T returnValue = action.write(baos); baos.flush(); byte[] data = baos.toByteArray(); return new WritingActionResult<>(returnValue, data); } ReplacingInterceptor(VhsReplayManagerConfig config, Replacement replacement); @Override HttpRespondable intercept(ParsedRequest parsedRequest, HttpRespondable httpRespondable); }
ReplacingInterceptor implements ResponseInterceptor { protected static <T> WritingActionResult<T> writeByteArray(WritingAction<T> action, int expectedOutputLength) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(expectedOutputLength); T returnValue = action.write(baos); baos.flush(); byte[] data = baos.toByteArray(); return new WritingActionResult<>(returnValue, data); } ReplacingInterceptor(VhsReplayManagerConfig config, Replacement replacement); @Override HttpRespondable intercept(ParsedRequest parsedRequest, HttpRespondable httpRespondable);  }