com.github.tomakehurst.wiremock.extension.Parameters Java Examples

The following examples show how to use com.github.tomakehurst.wiremock.extension.Parameters. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IntegrationTest.java    From prebid-server-java with Apache License 2.0 6 votes vote down vote up
@Override
public com.github.tomakehurst.wiremock.http.Response transform(
        Request request, com.github.tomakehurst.wiremock.http.Response response, FileSource files,
        Parameters parameters) {

    final String newResponse;
    try {
        newResponse = cacheResponseFromRequestJson(request.getBodyAsString(),
                parameters.getString("matcherName"));
    } catch (IOException e) {
        return com.github.tomakehurst.wiremock.http.Response.response()
                .body(e.getMessage())
                .status(500)
                .build();
    }
    return com.github.tomakehurst.wiremock.http.Response.response().body(newResponse).status(200).build();
}
 
Example #2
Source File: RedirectTest.java    From gradle-download-task with Apache License 2.0 6 votes vote down vote up
@Override
public Response transform(Request request, Response response,
        FileSource files, Parameters parameters) {
    if (redirects == null) {
        redirects = parameters.getInt("redirects");
    }
    String nl;
    if (redirects > 1) {
        redirects--;
        nl = "/" + REDIRECT + "?r=" + redirects;
    } else {
        nl = "/" + TEST_FILE_NAME;
    }
    return Response.Builder.like(response)
            .but().headers(response.getHeaders()
                    .plus(new HttpHeader("Location", nl)))
            .build();
}
 
Example #3
Source File: AbstractGitHubWireMockTest.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Override
public Response transform(Request request, Response response, FileSource files,
                          Parameters parameters) {
    if ("application/json"
        .equals(response.getHeaders().getContentTypeHeader().mimeTypePart())) {
        return Response.Builder.like(response)
            .but()
            .body(response.getBodyAsString()
                .replace("https://api.github.com/",
                    "http://localhost:" + githubApi.port() + "/")
                .replace("https://raw.githubusercontent.com/",
                    "http://localhost:" + githubRaw.port() + "/")
            )
            .build();
    }
    return response;
}
 
Example #4
Source File: WireMockBase.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
    // if gzipped, ungzip they body and discard the Content-Encoding header
    if (response.getHeaders().getHeader("Content-Encoding").containsValue("gzip")) {
        Iterable<HttpHeader> headers = Iterables.filter(
            response.getHeaders().all(),
            (HttpHeader header) -> header != null && !header.keyEquals("Content-Encoding") && !header.containsValue("gzip")
        );
        return Response.Builder.like(response)
            .but()
            .body(Gzip.unGzip(response.getBody()))
            .headers(new HttpHeaders(headers))
            .build();
    }
    return response;
}
 
Example #5
Source File: GithubMockBase.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public Response transform(Request request, Response response, FileSource files,
                          Parameters parameters) {
    if ("application/json"
            .equals(response.getHeaders().getContentTypeHeader().mimeTypePart())) {
        return Response.Builder.like(response)
                .but()
                .body(response.getBodyAsString()
                        .replace("https://api.github.com/",
                                "http://localhost:" + githubApi.port() + "/")
                )
                .build();
    }
    return response;
}
 
Example #6
Source File: SchemaRegistryMock.java    From fluent-kafka-streams-tests with MIT License 5 votes vote down vote up
@Override
public ResponseDefinition transform(final Request request, final ResponseDefinition responseDefinition,
        final FileSource files, final Parameters parameters) {
    final List<Integer> versions = SchemaRegistryMock.this.listVersions(this.getSubject(request));
    log.debug("Got versions {}", versions);
    return ResponseDefinitionBuilder.jsonResponse(versions);
}
 
Example #7
Source File: SchemaRegistryMock.java    From fluent-kafka-streams-tests with MIT License 5 votes vote down vote up
@Override
public ResponseDefinition transform(final Request request, final ResponseDefinition responseDefinition,
        final FileSource files, final Parameters parameters) {
    final String versionStr = Iterables.get(this.urlSplitter.split(request.getUrl()), 3);
    final SchemaMetadata metadata;
    if (versionStr.equals("latest")) {
        metadata = SchemaRegistryMock.this.getSubjectVersion(this.getSubject(request), versionStr);
    } else {
        final int version = Integer.parseInt(versionStr);
        metadata = SchemaRegistryMock.this.getSubjectVersion(this.getSubject(request), version);
    }
    return ResponseDefinitionBuilder.jsonResponse(metadata);
}
 
Example #8
Source File: Webhooks.java    From wiremock-webhooks-extension with Apache License 2.0 5 votes vote down vote up
@Override
public void doAction(final ServeEvent serveEvent, final Admin admin, final Parameters parameters) {
    final Notifier notifier = notifier();

    scheduler.schedule(
        new Runnable() {
            @Override
            public void run() {
                WebhookDefinition definition = parameters.as(WebhookDefinition.class);
                for (WebhookTransformer transformer: transformers) {
                    definition = transformer.transform(serveEvent, definition);
                }
                HttpUriRequest request = buildRequest(definition);

                try {
                    HttpResponse response = httpClient.execute(request);
                    notifier.info(
                        String.format("Webhook %s request to %s returned status %s\n\n%s",
                            definition.getMethod(),
                            definition.getUrl(),
                            response.getStatusLine(),
                            EntityUtils.toString(response.getEntity())
                        )
                    );
                } catch (IOException e) {
                    throwUnchecked(e);
                }
            }
        },
        0L,
        SECONDS
    );
}
 
Example #9
Source File: BasicMappingBuilder.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
@Override
public <P> BasicMappingBuilder withPostServeAction(String extensionName,
		P parameters) {
	Parameters params = parameters instanceof Parameters ? (Parameters) parameters
			: Parameters.of(parameters);
	this.postServeActions.put(extensionName, params);
	return this;
}
 
Example #10
Source File: WireMockBase.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
    if ("application/json"
        .equals(response.getHeaders().getContentTypeHeader().mimeTypePart())) {
        return Response.Builder.like(response)
            .but()
            .body(response.getBodyAsString().replace(sourceUrl, getServerUrl(rule)))
            .build();
    }
    return response;
}
 
Example #11
Source File: WireMockBaseTest.java    From airtable.java with MIT License 5 votes vote down vote up
public static void startRecording() {

        wireMockServer.startRecording(recordSpec()
                .forTarget(prop.getTargetUrl())
                .captureHeader("Accept")
                .captureHeader("Content-Type", true)
                .extractBinaryBodiesOver(0)
                .extractTextBodiesOver(0)
                .makeStubsPersistent(true)
                .transformers("modify-response-header")
                .transformerParameters(Parameters.one("headerValue", "123"))
                .matchRequestBodyWithEqualToJson(false, true));
    }
 
Example #12
Source File: SchemaRegistryMock.java    From fluent-kafka-streams-tests with MIT License 5 votes vote down vote up
@Override
public ResponseDefinition transform(final Request request, final ResponseDefinition responseDefinition,
        final FileSource files, final Parameters parameters) {
    final String subject = Iterables.get(this.urlSplitter.split(request.getUrl()), 1);
    try {
        final int id = SchemaRegistryMock.this.register(subject,
                new Schema.Parser()
                        .parse(RegisterSchemaRequest.fromJson(request.getBodyAsString()).getSchema()));
        final RegisterSchemaResponse registerSchemaResponse = new RegisterSchemaResponse();
        registerSchemaResponse.setId(id);
        return ResponseDefinitionBuilder.jsonResponse(registerSchemaResponse);
    } catch (final IOException e) {
        throw new IllegalArgumentException("Cannot parse schema registration request", e);
    }
}
 
Example #13
Source File: AbstractInstancesProxyControllerIntegrationTest.java    From Moss with Apache License 2.0 5 votes vote down vote up
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
    return Response.Builder.like(response)
                           .headers(HttpHeaders.copyOf(response.getHeaders())
                                               .plus(new HttpHeader("Connection", "Close")))
                           .build();
}
 
Example #14
Source File: SchemaRegistryMock.java    From schema-registry-transfer-smt with Apache License 2.0 5 votes vote down vote up
@Override
public ResponseDefinition transform(final Request request, final ResponseDefinition responseDefinition,
                                    final FileSource files, final Parameters parameters) {
    String versionStr = Iterables.get(this.urlSplitter.split(request.getUrl()), 3);
    SchemaMetadata metadata;
    if (versionStr.equals("latest")) {
        metadata = SchemaRegistryMock.this.getSubjectVersion(getSubject(request), versionStr);
    } else {
        int version = Integer.parseInt(versionStr);
        metadata = SchemaRegistryMock.this.getSubjectVersion(getSubject(request), version);
    }
    return ResponseDefinitionBuilder.jsonResponse(metadata);
}
 
Example #15
Source File: SchemaRegistryMock.java    From schema-registry-transfer-smt with Apache License 2.0 5 votes vote down vote up
@Override
public ResponseDefinition transform(final Request request, final ResponseDefinition responseDefinition,
                                    final FileSource files, final Parameters parameters) {
    final List<Integer> versions = SchemaRegistryMock.this.listVersions(getSubject(request));
    log.debug("Got versions {}", versions);
    return ResponseDefinitionBuilder.jsonResponse(versions);
}
 
Example #16
Source File: SchemaRegistryMock.java    From schema-registry-transfer-smt with Apache License 2.0 5 votes vote down vote up
@Override
public ResponseDefinition transform(final Request request, final ResponseDefinition responseDefinition,
                                    final FileSource files, final Parameters parameters) {
    try {
        final int id = SchemaRegistryMock.this.register(getSubject(request),
                new Schema.Parser()
                        .parse(RegisterSchemaRequest.fromJson(request.getBodyAsString()).getSchema()));
        final RegisterSchemaResponse registerSchemaResponse = new RegisterSchemaResponse();
        registerSchemaResponse.setId(id);
        return ResponseDefinitionBuilder.jsonResponse(registerSchemaResponse);
    } catch (final IOException e) {
        throw new IllegalArgumentException("Cannot parse schema registration request", e);
    }
}
 
Example #17
Source File: CustomVelocityResponseTransformer.java    From AuTe-Framework with Apache License 2.0 4 votes vote down vote up
@Override
public ResponseDefinition transform(
    final Request request,
    final ResponseDefinition responseDefinition,
    final FileSource files,
    final Parameters parameters
) throws RuntimeException {
    final VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.init();
    final ToolManager toolManager = new ToolManager();
    toolManager.setVelocityEngine(velocityEngine);

    context = toolManager.createContext();
    URL url = getRequestUrl(request);
    addBodyToContext(request.getBodyAsString());
    addHeadersToContext(request.getHeaders());
    context.put("requestAbsoluteUrl", request.getAbsoluteUrl());
    context.put("requestUrl", request.getUrl());
    context.put("requestMethod", request.getMethod());
    context.put("queryParams", urlExtractor.extractQueryParameters(url));
    context.put("pathSegments", urlExtractor.extractPathSegments(url));
    context.put("staticContext", staticContexts.computeIfAbsent(url.getPath(), k -> new HashMap<>()));
    context.put(ExtendedScript.MQ_PROPERTIES, properties);

    String body;
    if (responseDefinition.specifiesBodyFile() && templateDeclared(responseDefinition)) {
        body = getRenderedBody(responseDefinition);
    } else if (responseDefinition.specifiesBodyContent()) {
        try {
            body = getRenderedBodyFromFile(responseDefinition);
        } catch (ParseException e) {
            body = e.getMessage();
            e.printStackTrace();
        }
    } else {
        return responseDefinition;
    }
    if (isPresentConvertCommand(responseDefinition.getHeaders().getHeader(MultipartConstant.CONVERT_BASE64_IN_MULTIPART.getValue()))) {
        log.info(" >>> ");
        log.info(body);
        return buildResponse(parseConvertBody(body), body, responseDefinition, true);
    }
    return buildResponse(null, body, responseDefinition, false);
}
 
Example #18
Source File: SchemaRegistryMock.java    From fluent-kafka-streams-tests with MIT License 4 votes vote down vote up
@Override
public ResponseDefinition transform(final Request request, final ResponseDefinition responseDefinition,
        final FileSource files, final Parameters parameters) {
    final Collection<String> body = SchemaRegistryMock.this.listAllSubjects();
    return ResponseDefinitionBuilder.jsonResponse(body);
}
 
Example #19
Source File: SchemaRegistryMock.java    From fluent-kafka-streams-tests with MIT License 4 votes vote down vote up
@Override
public ResponseDefinition transform(final Request request, final ResponseDefinition responseDefinition,
        final FileSource files, final Parameters parameters) {
    final List<Integer> ids = SchemaRegistryMock.this.delete(this.getSubject(request));
    return ResponseDefinitionBuilder.jsonResponse(ids);
}
 
Example #20
Source File: BasicMappingBuilder.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
BasicMappingBuilder(String customRequestMatcherName, Parameters parameters) {
	this.requestPatternBuilder = new RequestPatternBuilder(customRequestMatcherName,
			parameters);
}
 
Example #21
Source File: SchemaRegistryMock.java    From schema-registry-transfer-smt with Apache License 2.0 4 votes vote down vote up
@Override
public ResponseDefinition transform(final Request request, final ResponseDefinition responseDefinition,
                                    final FileSource files, final Parameters parameters) {
    Config config = new Config(SchemaRegistryMock.this.getCompatibility(getSubject(request)));
    return ResponseDefinitionBuilder.jsonResponse(config);
}
 
Example #22
Source File: BasicMappingBuilder.java    From spring-cloud-contract with Apache License 2.0 4 votes vote down vote up
@Override
public MappingBuilder andMatching(String customRequestMatcherName,
		Parameters parameters) {
	this.requestPatternBuilder.andMatching(customRequestMatcherName, parameters);
	return this;
}
 
Example #23
Source File: ConnectionCloseExtension.java    From spring-boot-admin with Apache License 2.0 4 votes vote down vote up
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
	return Response.Builder.like(response)
			.headers(HttpHeaders.copyOf(response.getHeaders()).plus(new HttpHeader("Connection", "Close"))).build();
}