@Override public void writeTape(final Tape tape) { String fileName = normalize(tape.getName()); if (tape.isDirty()) { //noinspection OverlyBroadCatchBlock try { Writer writer = tapeRoot.writerFor(fileName); LOG.info(String.format("writing tape %s to file %s...", tape.getName(), fileName)); writeTo(tape, writer); } catch (IOException e) { throw new RuntimeException(e); } } }
private okhttp3.Response replayResponse(okhttp3.Request request, Tape tape, Request recordedRequest) { LOG.info(String.format("Playing back request %s %s from tape '%s'", recordedRequest.method(), recordedRequest.url().toString(), tape.getName())); Response recordedResponse = tape.play(recordedRequest); okhttp3.Response okhttpResponse = OkHttpResponseAdapter.adapt(request, recordedResponse); okhttpResponse = setOkReplayHeader(okhttpResponse, "PLAY"); okhttpResponse = setViaHeader(okhttpResponse); return okhttpResponse; }
private okhttp3.Response recordResponse(okhttp3.Request request, Tape tape, Request recordedRequest, okhttp3.Response okhttpResponse) { okhttpResponse = setOkReplayHeader(okhttpResponse, "REC"); okhttpResponse = setViaHeader(okhttpResponse); LOG.info(String.format("Recording request %s %s to tape '%s'", request.method(), request.url().toString(), tape.getName())); ResponseBody bodyClone = OkHttpResponseAdapter.cloneResponseBody(okhttpResponse.body()); Response recordedResponse = OkHttpResponseAdapter.adapt(okhttpResponse, bodyClone); tape.record(recordedRequest, recordedResponse); okhttpResponse = okhttpResponse.newBuilder() .body(OkHttpResponseAdapter.cloneResponseBody(okhttpResponse.body())) .build(); okhttpResponse.body().close(); return okhttpResponse; }
if (tape.isReadable() && tape.seek(recordedRequest)) { LOG.info(String.format("Playing back request %s %s from tape '%s'", recordedRequest.method(), recordedRequest.url().toString(), tape.getName())); Response recordedResponse = tape.play(recordedRequest); okhttp3.Response okhttpResponse = OkHttpResponseAdapter.adapt(request, recordedResponse); okhttpResponse = setOkReplayHeader(okhttpResponse, "PLAY"); } else { LOG.warning(String.format("no matching request found on tape '%s' for request %s %s", tape.getName(), request.method(), request.url().toString())); okhttp3.Response okhttpResponse = chain.proceed(request); okhttpResponse = setOkReplayHeader(okhttpResponse, "REC"); okhttpResponse = setViaHeader(okhttpResponse); if (tape.isWritable()) { LOG.info(String.format("Recording request %s %s to tape '%s'", request.method(), request.url().toString(), tape.getName())); ResponseBody bodyClone = OkHttpResponseAdapter.cloneResponseBody(okhttpResponse.body()); Response recordedResponse = OkHttpResponseAdapter.adapt(okhttpResponse, bodyClone); tape.record(recordedRequest, recordedResponse); okhttpResponse = okhttpResponse.newBuilder() .body(OkHttpResponseAdapter.cloneResponseBody(okhttpResponse.body()))
@Override public okhttp3.Response intercept(Chain chain) throws IOException { okhttp3.Request request = chain.request(); if (isRunning && !isHostIgnored(request)) { if (!tape.isPresent()) { return buildResponse(request, 403, "No tape"); } else { //noinspection ConstantConditions Tape tape = this.tape.get(); Request recordedRequest = OkHttpRequestAdapter.adapt(request); if (tape.isReadable() && tape.seek(recordedRequest)) { return replayResponse(request, tape, recordedRequest); } else { LOG.warning(String.format("no matching request found on tape '%s' for request %s %s", tape.getName(), request.method(), request.url().toString())); if (tape.getMode() == TapeMode.READ_ONLY_QUIET) { return buildResponse(request, 404, "No matching response"); } // If the tape isn't writeable, abandon this request. This prevents us from // talking to the server for non-mutable tapes. if (!tape.isWritable()) { throwTapeNotWritable(request.method() + " " + request.url().toString()); } // Continue the request and attempt to write the response to the tape. return recordResponse(request, tape, recordedRequest, chain.proceed(request)); } } } else { return chain.proceed(request); } }
/** * Starts the Recorder, inserting a tape with the specified parameters. * * @param tapeName the name of the tape. * @param mode the tape mode. If not supplied the default mode from the configuration is * used. * @param matchRule the rules used to match recordings on the tape. If not supplied a default is * used. * @throws IllegalStateException if the Recorder is already started. */ @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public void start(String tapeName, Optional<TapeMode> mode, Optional<MatchRule> matchRule) { if (tape != null) { throw new IllegalStateException("start called when Recorder is already started"); } tape = getTapeLoader().loadTape(tapeName); tape.setMode(mode.or(configuration.getDefaultMode())); tape.setMatchRule(matchRule.or(configuration.getDefaultMatchRule())); configuration.interceptor().start(configuration, tape); for (RecorderListener listener : listeners) { listener.onRecorderStart(tape); } }
/** * Starts the Recorder, inserting a tape with the specified parameters. * * @param tapeName the name of the tape. * @param mode the tape mode. If not supplied the default mode from the configuration is * used. * @param matchRule the rules used to match recordings on the tape. If not supplied a default is * used. * @throws IllegalStateException if the Recorder is already started. */ @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public void start(String tapeName, Optional<TapeMode> mode, Optional<MatchRule> matchRule) { if (tape != null) { throw new IllegalStateException("start called when Recorder is already started"); } tape = getTapeLoader().loadTape(tapeName); tape.setMode(mode.or(configuration.getDefaultMode())); tape.setMatchRule(matchRule.or(configuration.getDefaultMatchRule())); configuration.interceptor().start(configuration, tape); for (RecorderListener listener : listeners) { listener.onRecorderStart(tape); } }
@Override public void writeTape(final Tape tape) { String fileName = normalize(tape.getName()); if (tape.isDirty()) { //noinspection OverlyBroadCatchBlock try { Writer writer = tapeRoot.writerFor(fileName); LOG.info(String.format("writing tape %s to file %s...", tape.getName(), fileName)); writeTo(tape, writer); } catch (IOException e) { throw new RuntimeException(e); } } }