Java Code Examples for com.github.tomakehurst.wiremock.WireMockServer#verify()

The following examples show how to use com.github.tomakehurst.wiremock.WireMockServer#verify() . 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: HttpSenderCompatibilityKit.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@DisplayName("successfully send a request with NO body and receive a response with NO body")
@EnumSource(HttpSender.Method.class)
void successfulRequestSentWithNoBody(HttpSender.Method method, @WiremockResolver.Wiremock WireMockServer server) throws Throwable {
    server.stubFor(any(urlEqualTo("/metrics")));

    HttpSender.Response response = httpSender.newRequest(server.baseUrl() + "/metrics")
            .withMethod(method)
            .send();

    assertThat(response.code()).isEqualTo(200);
    assertThat(response.body()).isEqualTo(HttpSender.Response.NO_RESPONSE_BODY);

    server.verify(WireMock.requestMadeFor(request ->
            MatchResult.aggregate(
                    MatchResult.of(request.getMethod().getName().equals(method.name())),
                    MatchResult.of(request.getUrl().equals("/metrics"))
            )));
}
 
Example 2
Source File: HttpSenderCompatibilityKit.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@DisplayName("successfully send a request with a body and receive a response with a body")
@EnumSource(value = HttpSender.Method.class, names = {"POST", "PUT"})
void successfulRequestSentWithBody(HttpSender.Method method, @WiremockResolver.Wiremock WireMockServer server) throws Throwable {
    server.stubFor(any(urlEqualTo("/metrics"))
            .willReturn(ok("a body")));

    HttpSender.Response response = httpSender.newRequest(server.baseUrl() + "/metrics")
            .withMethod(method)
            .accept("customAccept")
            .withContent("custom/type", "this is a line")
            .send();

    assertThat(response.code()).isEqualTo(200);
    assertThat(response.body()).isEqualTo("a body");

    server.verify(WireMock.requestMadeFor(request ->
            MatchResult.aggregate(
                    MatchResult.of(request.getMethod().getName().equals(method.name())),
                    MatchResult.of(request.getUrl().equals("/metrics"))
            ))
            .withHeader("Accept", equalTo("customAccept"))
            .withHeader("Content-Type", containing("custom/type")) // charset may be added to the type
            .withRequestBody(equalTo("this is a line")));
}
 
Example 3
Source File: HttpSenderCompatibilityKit.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@EnumSource(HttpSender.Method.class)
void basicAuth(HttpSender.Method method, @WiremockResolver.Wiremock WireMockServer server) throws Throwable {
    server.stubFor(any(urlEqualTo("/metrics"))
            .willReturn(unauthorized()));

    HttpSender.Response response = httpSender.newRequest(server.baseUrl() + "/metrics")
            .withMethod(method)
            .withBasicAuthentication("superuser", "superpassword")
            .send();

    assertThat(response.code()).isEqualTo(401);

    server.verify(WireMock.requestMadeFor(request ->
            MatchResult.aggregate(
                    MatchResult.of(request.getMethod().getName().equals(method.name())),
                    MatchResult.of(request.getUrl().equals("/metrics"))
            ))
            .withBasicAuth(new BasicCredentials("superuser", "superpassword")));
}
 
Example 4
Source File: HttpSenderCompatibilityKit.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@EnumSource(HttpSender.Method.class)
void customHeader(HttpSender.Method method, @WiremockResolver.Wiremock WireMockServer server) throws Throwable {
    server.stubFor(any(urlEqualTo("/metrics"))
            .willReturn(unauthorized()));

    HttpSender.Response response = httpSender.newRequest(server.baseUrl() + "/metrics")
            .withMethod(method)
            .withHeader("customHeader", "customHeaderValue")
            .send();

    assertThat(response.code()).isEqualTo(401);

    server.verify(WireMock.requestMadeFor(request ->
            MatchResult.aggregate(
                    MatchResult.of(request.getMethod().getName().equals(method.name())),
                    MatchResult.of(request.getUrl().equals("/metrics"))
            ))
            .withHeader("customHeader", equalTo("customHeaderValue")));
}
 
Example 5
Source File: AtlasMeterRegistryTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Issue("#484")
@Test
void publishOneLastTimeOnClose(@WiremockResolver.Wiremock WireMockServer server) {
    AtlasConfig config = new AtlasConfig() {
        @Nullable
        @Override
        public String get(String k) {
            return null;
        }

        @Override
        public String uri() {
            return server.baseUrl() + "/api/v1/publish";
        }
    };

    server.stubFor(any(anyUrl()));
    new AtlasMeterRegistry(config, Clock.SYSTEM).close();
    server.verify(postRequestedFor(urlEqualTo("/api/v1/publish")));
}
 
Example 6
Source File: OkHttpSenderTests.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void appendUtf8CharsetContentType(@WiremockResolver.Wiremock WireMockServer server) throws Throwable {
    server.stubFor(any(urlEqualTo("/metrics")));

    this.httpSender.post(server.baseUrl() + "/metrics")
            .withContent("application/xml", "<xml></xml>")
            .send();

    server.verify(postRequestedFor(urlEqualTo("/metrics"))
            .withHeader("Content-Type", equalTo("application/xml; charset=utf-8")));
}
 
Example 7
Source File: HumioMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void writeTimer(@WiremockResolver.Wiremock WireMockServer server) {
    HumioMeterRegistry registry = humioMeterRegistry(server);
    registry.timer("my.timer", "status", "success");

    server.stubFor(any(anyUrl()));
    registry.publish();
    server.verify(postRequestedFor(urlMatching("/api/v1/ingest/humio-structured"))
            .withRequestBody(equalTo("[{\"events\": [{\"timestamp\":\"1970-01-01T00:00:00.001Z\",\"attributes\":{\"name\":\"my_timer\",\"count\":0,\"sum\":0,\"avg\":0,\"max\":0,\"status\":\"success\"}}]}]")));
}
 
Example 8
Source File: HumioMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void datasourceTags(@WiremockResolver.Wiremock WireMockServer server) {
    HumioMeterRegistry registry = humioMeterRegistry(server, "name", "micrometer");
    registry.counter("my.counter").increment();

    server.stubFor(any(anyUrl()));
    registry.publish();
    server.verify(postRequestedFor(urlMatching("/api/v1/ingest/humio-structured"))
            .withRequestBody(containing("\"tags\":{\"name\": \"micrometer\"}")));
}
 
Example 9
Source File: DatadogMeterRegistryTest.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Issue("#463")
@Test
void encodeMetricName(@WiremockResolver.Wiremock WireMockServer server) {
    Clock clock = new MockClock();
    DatadogMeterRegistry registry = new DatadogMeterRegistry(new DatadogConfig() {
        @Override
        public String uri() {
            return server.baseUrl();
        }

        @Override
        public String get(String key) {
            return null;
        }

        @Override
        public String apiKey() {
            return "fake";
        }

        @Override
        public String applicationKey() {
            return "fake";
        }

        @Override
        public boolean descriptions() {
            return false;
        }

        @Override
        public boolean enabled() {
            return false;
        }
    }, clock);

    server.stubFor(any(anyUrl()));

    Counter.builder("my.counter#abc")
        .baseUnit(TimeUnit.MICROSECONDS.toString().toLowerCase())
        .description("metric description")
        .register(registry)
        .increment(Math.PI);
    registry.publish();

    server.verify(postRequestedFor(
            urlEqualTo("/api/v1/series?api_key=fake"))
            .withRequestBody(equalToJson("{\"series\":[{\"metric\":\"my.counter#abc\",\"points\":[[0,0.0]],\"type\":\"count\",\"unit\":\"microsecond\",\"tags\":[\"statistic:count\"]}]}")
            ));

    registry.close();
}
 
Example 10
Source File: DatadogMeterRegistryTest.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Test
void testWithDescriptionEnabled(@WiremockResolver.Wiremock WireMockServer server) {
    Clock clock = new MockClock();
    DatadogMeterRegistry registry = new DatadogMeterRegistry(new DatadogConfig() {
        @Override
        public String uri() {
            return server.baseUrl();
        }

        @Override
        public String get(String key) {
            return null;
        }

        @Override
        public String apiKey() {
            return "fake";
        }

        @Override
        public String applicationKey() {
            return "fake";
        }

        @Override
        public boolean descriptions() {
            return true;
        }

        @Override
        public boolean enabled() {
            return false;
        }
    }, clock);

    server.stubFor(any(anyUrl()));

    Counter.builder("my.counter#abc")
            .baseUnit(TimeUnit.MICROSECONDS.toString().toLowerCase())
            .description("metric description")
            .register(registry)
            .increment(Math.PI);
    registry.publish();

    server.verify(postRequestedFor(
            urlEqualTo("/api/v1/series?api_key=fake"))
            .withRequestBody(equalToJson("{\"series\":[{\"metric\":\"my.counter#abc\",\"points\":[[0,0.0]],\"type\":\"count\",\"unit\":\"microsecond\",\"tags\":[\"statistic:count\"]}]}")
            ));

    server.verify(putRequestedFor(
            urlEqualTo("/api/v1/metrics/my.counter%23abc?api_key=fake&application_key=fake"))
            .withRequestBody(equalToJson("{\"description\":\"metric description\"}")
            ));

    registry.close();
}