Search in sources :

Example 1 with ExpectationSerializer

use of org.mockserver.serialization.ExpectationSerializer in project mockserver by mock-server.

the class AbstractExtendedMockingIntegrationTest method shouldRetrieveRecordedExpectationsAsJson.

@Test
public void shouldRetrieveRecordedExpectationsAsJson() {
    // when
    mockServerClient.when(request().withPath(calculatePath("some_path.*")), exactly(4)).forward(forward().withHost("127.0.0.1").withPort(insecureEchoServer.getPort()));
    assertEquals(response("some_body_one"), makeRequest(request().withPath(calculatePath("some_path_one")).withBody("some_body_one"), HEADERS_TO_IGNORE));
    assertEquals(response("some_body_three"), makeRequest(request().withPath(calculatePath("some_path_three")).withBody("some_body_three"), HEADERS_TO_IGNORE));
    // then
    Expectation[] recordedExpectations = new ExpectationSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedExpectations(request().withPath(calculatePath("some_path_one")), Format.JSON), false);
    assertThat(recordedExpectations.length, is(1));
    verifyRequestsMatches(new RequestDefinition[] { recordedExpectations[0].getHttpRequest() }, request(calculatePath("some_path_one")).withBody("some_body_one"));
    assertThat(recordedExpectations[0].getHttpResponse().getBodyAsString(), is("some_body_one"));
    // and
    recordedExpectations = new ExpectationSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedExpectations(request(), Format.JSON), false);
    assertThat(recordedExpectations.length, is(2));
    verifyRequestsMatches(new RequestDefinition[] { recordedExpectations[0].getHttpRequest(), recordedExpectations[1].getHttpRequest() }, request(calculatePath("some_path_one")).withBody("some_body_one"), request(calculatePath("some_path_three")).withBody("some_body_three"));
    assertThat(recordedExpectations[0].getHttpResponse().getBodyAsString(), is("some_body_one"));
    assertThat(recordedExpectations[1].getHttpResponse().getBodyAsString(), is("some_body_three"));
    // and
    recordedExpectations = new ExpectationSerializer(new MockServerLogger()).deserializeArray(mockServerClient.retrieveRecordedExpectations(null, Format.JSON), false);
    assertThat(recordedExpectations.length, is(2));
    verifyRequestsMatches(new RequestDefinition[] { recordedExpectations[0].getHttpRequest(), recordedExpectations[1].getHttpRequest() }, request(calculatePath("some_path_one")).withBody("some_body_one"), request(calculatePath("some_path_three")).withBody("some_body_three"));
    assertThat(recordedExpectations[0].getHttpResponse().getBodyAsString(), is("some_body_one"));
    assertThat(recordedExpectations[1].getHttpResponse().getBodyAsString(), is("some_body_three"));
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) Expectation(org.mockserver.mock.Expectation) ExpectationSerializer(org.mockserver.serialization.ExpectationSerializer) Test(org.junit.Test)

Example 2 with ExpectationSerializer

use of org.mockserver.serialization.ExpectationSerializer in project mockserver by mock-server.

the class AbstractBasicMockingIntegrationTest method shouldSupportBatchedExpectations.

@Test
public void shouldSupportBatchedExpectations() throws Exception {
    // when
    HttpResponse httpResponse = makeRequest(request().withMethod("PUT").withSecure(isSecureControlPlane()).withHeader(HOST.toString(), "localhost:" + this.getServerPort()).withHeader(authorisationHeader()).withPath(addContextToPath("expectation")).withBody("" + "[" + new ExpectationSerializer(new MockServerLogger()).serialize(new Expectation(request("/path_one"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_one"))) + "," + new ExpectationSerializer(new MockServerLogger()).serialize(new Expectation(request("/path_two"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_two"))) + "," + new ExpectationSerializer(new MockServerLogger()).serialize(new Expectation(request("/path_three"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_three"))) + "]"), HEADERS_TO_IGNORE);
    assertThat(httpResponse.getStatusCode(), equalTo(201));
    // then
    Expectation[] upsertedExpectations = new ExpectationSerializer(new MockServerLogger()).deserializeArray(httpResponse.getBodyAsString(), true);
    assertThat(upsertedExpectations.length, is(3));
    assertThat(upsertedExpectations[0], is(new Expectation(request("/path_one"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_one"))));
    assertThat(upsertedExpectations[1], is(new Expectation(request("/path_two"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_two"))));
    assertThat(upsertedExpectations[2], is(new Expectation(request("/path_three"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_three"))));
    assertEquals(response().withStatusCode(OK_200.code()).withReasonPhrase(OK_200.reasonPhrase()).withBody("some_body_one"), makeRequest(request().withPath(calculatePath("/path_one")), HEADERS_TO_IGNORE));
    assertEquals(response().withStatusCode(OK_200.code()).withReasonPhrase(OK_200.reasonPhrase()).withBody("some_body_two"), makeRequest(request().withPath(calculatePath("/path_two")), HEADERS_TO_IGNORE));
    assertEquals(response().withStatusCode(OK_200.code()).withReasonPhrase(OK_200.reasonPhrase()).withBody("some_body_three"), makeRequest(request().withPath(calculatePath("/path_three")), HEADERS_TO_IGNORE));
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) ExpectationSerializer(org.mockserver.serialization.ExpectationSerializer) Expectation(org.mockserver.mock.Expectation) OpenAPIExpectation.openAPIExpectation(org.mockserver.mock.OpenAPIExpectation.openAPIExpectation) Test(org.junit.Test)

Example 3 with ExpectationSerializer

use of org.mockserver.serialization.ExpectationSerializer in project mockserver by mock-server.

the class AbstractBasicMockingIntegrationTest method shouldSupportBatchedExpectations.

@Test
public void shouldSupportBatchedExpectations() throws Exception {
    // when
    HttpResponse httpResponse = httpClient.sendRequest(request().withMethod("PUT").withHeader(HOST.toString(), "localhost:" + this.getServerPort()).withPath(addContextToPath("mockserver/expectation")).withBody("" + "[" + new ExpectationSerializer(new MockServerLogger()).serialize(new Expectation(request("/path_one"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_one"))) + "," + new ExpectationSerializer(new MockServerLogger()).serialize(new Expectation(request("/path_two"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_two"))) + "," + new ExpectationSerializer(new MockServerLogger()).serialize(new Expectation(request("/path_three"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_three"))) + "]")).get(10, SECONDS);
    // then
    Expectation[] upsertedExpectations = new ExpectationSerializer(new MockServerLogger()).deserializeArray(httpResponse.getBodyAsString(), true);
    assertThat(upsertedExpectations.length, is(3));
    assertThat(upsertedExpectations[0], is(new Expectation(request("/path_one"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_one"))));
    assertThat(upsertedExpectations[1], is(new Expectation(request("/path_two"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_two"))));
    assertThat(upsertedExpectations[2], is(new Expectation(request("/path_three"), once(), TimeToLive.unlimited(), 0).thenRespond(response().withBody("some_body_three"))));
    assertEquals(response().withStatusCode(OK_200.code()).withReasonPhrase(OK_200.reasonPhrase()).withBody("some_body_one"), makeRequest(request().withPath(calculatePath("/path_one")), headersToIgnore));
    assertEquals(response().withStatusCode(OK_200.code()).withReasonPhrase(OK_200.reasonPhrase()).withBody("some_body_two"), makeRequest(request().withPath(calculatePath("/path_two")), headersToIgnore));
    assertEquals(response().withStatusCode(OK_200.code()).withReasonPhrase(OK_200.reasonPhrase()).withBody("some_body_three"), makeRequest(request().withPath(calculatePath("/path_three")), headersToIgnore));
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) ExpectationSerializer(org.mockserver.serialization.ExpectationSerializer) Expectation(org.mockserver.mock.Expectation) OpenAPIExpectation.openAPIExpectation(org.mockserver.mock.OpenAPIExpectation.openAPIExpectation) Test(org.junit.jupiter.api.Test)

Example 4 with ExpectationSerializer

use of org.mockserver.serialization.ExpectationSerializer in project mockserver by mock-server.

the class MockServerClientIntegrationTest method shouldRetrieveActiveExpectations.

@Test
public void shouldRetrieveActiveExpectations() {
    // given
    echoServerOne.withNextResponse(response().withStatusCode(201).withContentType(APPLICATION_JSON).withBody(new StringBody(new ExpectationSerializer(MOCK_SERVER_LOGGER).serialize(new Expectation(request("/some_request_one")).thenRespond(response()), new Expectation(request("/some_request_two")).thenRespond(response())))));
    // when
    Expectation[] actualResponse = mockServerClientOne.retrieveActiveExpectations(request().withPath("/some_path").withBody(new StringBody("some_request_body")));
    // then
    assertThat(Arrays.asList(actualResponse), hasItems(new Expectation(request("/some_request_one")).thenRespond(response()), new Expectation(request("/some_request_two")).thenRespond(response())));
    assertThat(retrieveRequests(request()).size(), is(1));
    String result = verify(verification().withRequest(request().withMethod("PUT").withPath("/mockserver/retrieve").withQueryStringParameter("type", RetrieveType.ACTIVE_EXPECTATIONS.name()).withQueryStringParameter("format", Format.JSON.name()).withHeaders(new Header("host", "localhost:" + echoServerOne.getPort()), new Header("accept-encoding", "gzip,deflate"), new Header("connection", "keep-alive"), new Header("content-type", "application/json; charset=utf-8")).withSecure(false).withKeepAlive(true).withBody(new StringBody("{" + NEW_LINE + "  \"path\" : \"/some_path\"," + NEW_LINE + "  \"body\" : \"some_request_body\"" + NEW_LINE + "}"))));
    if (result != null && !result.isEmpty()) {
        throw new AssertionError(result);
    }
}
Also used : ExpectationSerializer(org.mockserver.serialization.ExpectationSerializer) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Example 5 with ExpectationSerializer

use of org.mockserver.serialization.ExpectationSerializer in project mockserver by mock-server.

the class MockServerClientIntegrationTest method shouldRetrieveRecordedExpectationsWithNullRequest.

@Test
public void shouldRetrieveRecordedExpectationsWithNullRequest() {
    // given
    echoServerOne.withNextResponse(response().withStatusCode(201).withContentType(APPLICATION_JSON).withBody(new StringBody(new ExpectationSerializer(MOCK_SERVER_LOGGER).serialize(new Expectation(request("/some_request_one")).thenRespond(response()), new Expectation(request("/some_request_two")).thenRespond(response())))));
    // when
    Expectation[] actualResponse = mockServerClientOne.retrieveRecordedExpectations(null);
    // then
    assertThat(Arrays.asList(actualResponse), hasItems(new Expectation(request("/some_request_one")).thenRespond(response()), new Expectation(request("/some_request_two")).thenRespond(response())));
    assertThat(retrieveRequests(request()).size(), is(1));
    String result = verify(verification().withRequest(request().withMethod("PUT").withPath("/mockserver/retrieve").withQueryStringParameter("type", RetrieveType.RECORDED_EXPECTATIONS.name()).withQueryStringParameter("format", Format.JSON.name()).withHeaders(new Header("host", "localhost:" + echoServerOne.getPort()), new Header("accept-encoding", "gzip,deflate"), new Header("content-length", "0"), new Header("connection", "keep-alive"), new Header("content-type", "application/json; charset=utf-8")).withSecure(false).withKeepAlive(true)));
    if (result != null && !result.isEmpty()) {
        throw new AssertionError(result);
    }
}
Also used : ExpectationSerializer(org.mockserver.serialization.ExpectationSerializer) Expectation(org.mockserver.mock.Expectation) Test(org.junit.Test)

Aggregations

ExpectationSerializer (org.mockserver.serialization.ExpectationSerializer)20 Expectation (org.mockserver.mock.Expectation)19 Test (org.junit.Test)18 File (java.io.File)8 MockServer (org.mockserver.netty.MockServer)8 MockServerClient (org.mockserver.client.MockServerClient)6 MockServerLogger (org.mockserver.logging.MockServerLogger)6 OpenAPIExpectation.openAPIExpectation (org.mockserver.mock.OpenAPIExpectation.openAPIExpectation)2 Test (org.junit.jupiter.api.Test)1 HttpRequestSerializer (org.mockserver.serialization.HttpRequestSerializer)1