com.github.tomakehurst.wiremock.client.WireMock.equalToJson()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(19.2k)|赞(0)|评价(0)|浏览(111)

本文整理了Java中com.github.tomakehurst.wiremock.client.WireMock.equalToJson()方法的一些代码示例,展示了WireMock.equalToJson()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WireMock.equalToJson()方法的具体详情如下:
包路径:com.github.tomakehurst.wiremock.client.WireMock
类名称:WireMock
方法名:equalToJson

WireMock.equalToJson介绍

暂无

代码示例

代码示例来源:origin: apache/drill

private void setupPostStubs() {
 wireMockRule.stubFor(post(urlEqualTo("/api/query"))
   .withRequestBody(equalToJson(POST_REQUEST_WITHOUT_TAGS))
   .willReturn(aResponse()
     .withStatus(200)
   .withRequestBody(equalToJson(POST_REQUEST_WITH_TAGS))
   .willReturn(aResponse()
     .withStatus(200)
   .withRequestBody(equalToJson(DOWNSAMPLE_REQUEST_WTIHOUT_TAGS))
   .willReturn(aResponse()
     .withStatus(200)
     .withRequestBody(equalToJson(END_PARAM_REQUEST_WTIHOUT_TAGS))
     .willReturn(aResponse()
         .withStatus(200)
   .withRequestBody(equalToJson(DOWNSAMPLE_REQUEST_WITH_TAGS))
   .willReturn(aResponse()
     .withStatus(200)
     .withRequestBody(equalToJson(END_PARAM_REQUEST_WITH_TAGS))
     .willReturn(aResponse()
         .withStatus(200)
   .withRequestBody(equalToJson(REQUEST_TO_NONEXISTENT_METRIC))
   .willReturn(aResponse()
     .withStatus(400)

代码示例来源:origin: authorjapps/zerocode

private static MappingBuilder createRequestBuilderWithHeaders(MockStep mockStep, MappingBuilder requestBuilder) {
  final String bodyJson = mockStep.getBody();
  // -----------------------------------------------
  // read request body and set to request builder
  // -----------------------------------------------
  if(StringUtils.isNotEmpty(bodyJson)){
    requestBuilder.withRequestBody(equalToJson(bodyJson));
  }
  final Map<String, Object> headersMap = mockStep.getHeadersMap();
  // -----------------------------------------------
  // read request headers and set to request builder
  // -----------------------------------------------
  if (headersMap.size() > 0) {
    for (Object key : headersMap.keySet()) {
      requestBuilder.withHeader((String) key, equalTo((String) headersMap.get(key)));
    }
  }
  return requestBuilder;
}

代码示例来源:origin: com.github.tomakehurst/wiremock-jre8

private StringValuePattern valuePatternForContentType(Request request) {
  String contentType = request.getHeader("Content-Type");
  if (contentType != null) {
    if (contentType.contains("json")) {
      return equalToJson(request.getBodyAsString(), true, true);
    } else if (contentType.contains("xml")) {
      return equalToXml(request.getBodyAsString());
    }
  }
  return equalTo(request.getBodyAsString());
}

代码示例来源:origin: uber/rides-java-sdk

@Test
public void testGetRideEstimate_withUberPoolProductId_andV1EstimateSchema() throws Exception {
  stubFor(post(urlPathEqualTo("/v1.2/requests/estimate"))
      .withRequestBody(equalToJson(V1_RIDE_ESTIMATE_UBER_POOL, true, false))
      .willReturn(aResponse().withBodyFile("v1_request_estimate_UberPool.json")));
  final RideEstimate rideEstimate = service.estimateRide(
      createUberPoolRideRequestV1Estimate()).execute().body();
  assertThat(rideEstimate.getEstimate().getFareId()).isEqualTo(FARE_ID);
  assertThat(rideEstimate.getPickupEstimate()).isEqualTo(4);
  assertThat(rideEstimate.getEstimate().getHighEstimate()).isEqualTo(
      new BigDecimal(Float.valueOf(5f).toString()));
  assertThat(rideEstimate.getEstimate().getLowEstimate()).isEqualTo(
      new BigDecimal(Float.valueOf(4f).toString()));
  assertThat(rideEstimate.getEstimate().getDisplay()).isEqualTo("$4.87");
  assertThat(rideEstimate.getTrip()).isNotNull();
  assertThat(rideEstimate.getTrip().getDistanceUnit()).isEqualTo("mile");
  assertThat(rideEstimate.getTrip().getDurationEstimate()).isEqualTo(720);
  assertThat(rideEstimate.getTrip().getDistanceEstimate()).isEqualTo(1.88f);
}

代码示例来源:origin: uber/rides-java-sdk

@Test
public void testGetRideEstimate_withoutProductId_andV1EstimateSchema() throws Exception {
  stubFor(post(urlPathEqualTo("/v1.2/requests/estimate"))
      .withRequestBody(equalToJson(V1_RIDE_ESTIMATE, true, false))
      .willReturn(aResponse().withBodyFile("v1_requests_estimate.json")));
  final RideEstimate rideEstimate = service.estimateRide(
      createRideRequestV1Estimate()).execute().body();
  assertThat(rideEstimate.getEstimate().getFareId()).isNull();
  assertThat(rideEstimate.getEstimate().getHighEstimate()).isEqualTo(
      new BigDecimal(Float.valueOf(10f).toString()));
  assertThat(rideEstimate.getEstimate().getLowEstimate()).isEqualTo(
      new BigDecimal(Float.valueOf(7f).toString()));
  assertThat(rideEstimate.getEstimate().getDisplay()).isEqualTo("$7-10");
  assertThat(rideEstimate.getTrip()).isNotNull();
  assertThat(rideEstimate.getTrip().getDistanceUnit()).isEqualTo("mile");
  assertThat(rideEstimate.getTrip().getDurationEstimate()).isEqualTo(720);
  assertThat(rideEstimate.getTrip().getDistanceEstimate()).isEqualTo(1.88f);
}

代码示例来源:origin: uber/rides-java-sdk

@Test
public void testGetRideEstimate_withoutProductId() throws Exception {
  stubFor(post(urlPathEqualTo("/v1.2/requests/estimate"))
      .withRequestBody(equalToJson(RIDE_REQUEST, true, false))
      .willReturn(aResponse().withBodyFile("v1.2_request_estimate_UberPool.json")));
  final RideEstimate rideEstimate = service.estimateRide(
      createRideRequest()).execute().body();
  assertThat(rideEstimate.getFare().getCurrencyCode()).isEqualTo("USD");
  assertThat(rideEstimate.getFare().getValue()).isEqualTo(new BigDecimal("9.99"));
  assertThat(rideEstimate.getFare().getExpiresAt()).isEqualTo(1474919953);
  assertThat(rideEstimate.getFare().getFareId()).isEqualTo(
      "9b071e64ec5001d50afaa4f28ed7040450c10edc73fdc477844dfb6dd194263c");
  assertThat(rideEstimate.getTrip()).isNotNull();
  assertThat(rideEstimate.getTrip().getDistanceUnit()).isEqualTo("mile");
  assertThat(rideEstimate.getTrip().getDurationEstimate()).isEqualTo(720);
  assertThat(rideEstimate.getTrip().getDistanceEstimate()).isEqualTo(1.88f);
  assertThat(rideEstimate.getPickupEstimate()).isEqualTo(4);
}

代码示例来源:origin: uber/rides-java-sdk

@Test
public void testRequestRide_withoutProductId() throws Exception {
  stubFor(post(urlPathEqualTo("/v1.2/requests"))
      .withRequestBody(equalToJson(RIDE_REQUEST, true, false))
      .willReturn(aResponse().withBodyFile("requests_current.json")));
  final Ride ride = service.requestRide(createRideRequest()).execute().body();
  assertThat(ride.getStatus()).isEqualTo(Ride.Status.PROCESSING);
  assertThat(ride.getProductId()).isEqualTo(UBER_X_PRODUCT_ID);
  assertThat(ride.getRideId()).isNotEmpty();
  assertThat(ride.isShared()).isFalse();
  assertThat(ride.getPickup().getEta()).isEqualTo(5);
  assertThat(ride.getPickup().getLatitude()).isEqualTo(37.7872486012f);
  assertThat(ride.getPickup().getLongitude()).isEqualTo(-122.4026315287f);
  assertThat(ride.getDestination().getEta()).isEqualTo(19);
  assertThat(ride.getDestination().getLatitude()).isEqualTo(37.7766874f);
  assertThat(ride.getDestination().getLongitude()).isEqualTo(-122.394857f);
}

代码示例来源:origin: uber/rides-java-sdk

@Test
  public void testRequestRide_withUberPoolProductId() throws Exception {
    stubFor(post(urlPathEqualTo("/v1.2/requests"))
        .withRequestBody(equalToJson(RIDE_REQUEST_UBER_POOL, true, false))
        .willReturn(aResponse().withBodyFile("requests_current_UberPool.json")));

    final Ride ride = service.requestRide(createUberPoolRideRequest()).execute().body();
    assertThat(ride.getStatus()).isEqualTo(Ride.Status.PROCESSING);

    assertThat(ride.getProductId()).isEqualTo(UBER_POOL_PRODUCT_ID);
    assertThat(ride.getRideId()).isNotEmpty();
    assertThat(ride.isShared()).isTrue();

    assertThat(ride.getPickup().getEta()).isEqualTo(5);
    assertThat(ride.getPickup().getLatitude()).isEqualTo(37.7872486012f);
    assertThat(ride.getPickup().getLongitude()).isEqualTo(-122.4026315287f);

    assertThat(ride.getDestination().getEta()).isEqualTo(19);
    assertThat(ride.getDestination().getLatitude()).isEqualTo(37.7766874f);
    assertThat(ride.getDestination().getLongitude()).isEqualTo(-122.394857f);
  }
}

代码示例来源:origin: com.github.tomakehurst/wiremock-jre8

private MultipartValuePattern valuePatternForPart(Request.Part part) {
  MultipartValuePatternBuilder builder = new MultipartValuePatternBuilder().withName(part.getName()).matchingType(MultipartValuePattern.MatchingType.ALL);
  if (!headersToMatch.isEmpty()) {
    Collection<HttpHeader> all = part.getHeaders().all();
    for (HttpHeader httpHeader : all) {
      if (headersToMatch.contains(httpHeader.caseInsensitiveKey())) {
        builder.withHeader(httpHeader.key(), equalTo(httpHeader.firstValue()));
      }
    }
  }
  HttpHeader contentType = part.getHeader("Content-Type");
  if (!contentType.isPresent() || contentType.firstValue().contains("text")) {
    builder.withBody(equalTo(part.getBody().asString()));
  } else if (contentType.firstValue().contains("json")) {
    builder.withBody(equalToJson(part.getBody().asString(), true, true));
  } else if (contentType.firstValue().contains("xml")) {
    builder.withBody(equalToXml(part.getBody().asString()));
  } else {
    builder.withBody(binaryEqualTo(part.getBody().asBytes()));
  }
  return builder.build();
}

代码示例来源:origin: georocket/georocket

/**
 * Test if the client can create a mapping for an index
 * @param context the test context
 */
@Test
public void putMapping(TestContext context) {
 wireMockRule1.stubFor(put(urlEqualTo("/" + INDEX + "/_mapping/" + TYPE))
  .willReturn(aResponse()
   .withStatus(200)
   .withBody(ACKNOWLEDGED.encode())));
 JsonObject mappings = new JsonObject()
  .put("properties", new JsonObject()
   .put("name", new JsonObject()
    .put("type", "text")));
 Async async = context.async();
 client.putMapping(TYPE, mappings).subscribe(ack -> {
  wireMockRule1.verify(putRequestedFor(urlEqualTo("/" + INDEX + "/_mapping/" + TYPE))
   .withRequestBody(equalToJson("{\"properties\":{\"name\":{\"type\":\"text\"}}}}}")));
  context.assertTrue(ack);
  async.complete();
 }, context::fail);
}

代码示例来源:origin: georocket/georocket

/**
 * Test if the client can insert multiple documents in one request
 * @param context the test context
 */
@Test
public void bulkInsert(TestContext context) {
 String url = "/" + INDEX + "/" + TYPE + "/_bulk";
 wireMockRule1.stubFor(post(urlEqualTo(url))
   .willReturn(aResponse()
     .withStatus(200)
     .withBody("{}")));
 
 List<Tuple2<String, JsonObject>> documents = new ArrayList<>();
 documents.add(Tuple.tuple("A", new JsonObject().put("name", "Elvis")));
 documents.add(Tuple.tuple("B", new JsonObject().put("name", "Max")));
 
 Async async = context.async();
 client.bulkInsert(TYPE, documents).subscribe(res -> {
  wireMockRule1.verify(postRequestedFor(urlEqualTo(url))
    .withRequestBody(equalToJson("{\"index\":{\"_id\":\"A\"}}\n" + 
      "{\"name\":\"Elvis\"}\n" + 
      "{\"index\":{\"_id\":\"B\"}}\n" + 
      "{\"name\":\"Max\"}\n")));
  context.assertEquals(0, res.size());
  async.complete();
 }, context::fail);
}

代码示例来源:origin: hibernate/hibernate-search

@Test
@TestForIssue(jiraKey = "HSEARCH-2453")
public void authentication_error() throws Exception {
  SearchConfigurationForTest configuration = SearchConfigurationForTest.noTestDefaults()
      .addProperty( ElasticsearchEnvironment.SERVER_URI, httpUrlFor( wireMockRule1 ) );
  String payload = "{ \"foo\": \"bar\" }";
  String statusMessage = "StatusMessageUnauthorized";
  wireMockRule1.stubFor( post( urlPathLike( "/myIndex/myType/_search" ) )
      .withRequestBody( equalToJson( payload ) )
      .willReturn(
          elasticsearchResponse().withStatus( 401 /* Unauthorized */ )
          .withStatusMessage( statusMessage )
      ) );
  try ( ElasticsearchClient client = createClient( configuration ) ) {
    ElasticsearchResponse result = doPost( client, "/myIndex/myType/_search", payload );
    assertThat( result.getStatusCode() ).as( "status code" ).isEqualTo( 401 );
    assertThat( result.getStatusMessage() ).as( "status message" ).isEqualTo( statusMessage );
  }
}

代码示例来源:origin: hibernate/hibernate-search

@Test
public void error() throws Exception {
  SearchConfigurationForTest configuration = SearchConfigurationForTest.noTestDefaults()
      .addProperty( ElasticsearchEnvironment.SERVER_URI, httpUrlFor( wireMockRule1 ) );
  String payload = "{ \"foo\": \"bar\" }";
  String responseBody = "{ \"error\": \"ErrorMessageExplainingTheError\" }";
  wireMockRule1.stubFor( post( urlPathLike( "/myIndex/myType" ) )
      .withRequestBody( equalToJson( payload ) )
      .willReturn(
          elasticsearchResponse().withStatus( 500 )
          .withBody( responseBody )
      ) );
  try ( ElasticsearchClient client = createClient( configuration ) ) {
    ElasticsearchResponse result = doPost( client, "/myIndex/myType", payload );
    assertThat( result.getStatusCode() ).as( "status code" ).isEqualTo( 500 );
    JsonHelper.assertJsonEquals( responseBody, result.getBody().toString() );
  }
}

代码示例来源:origin: hibernate/hibernate-search

@Test
@TestForIssue(jiraKey = "HSEARCH-2274")
public void simple() throws Exception {
  SearchConfigurationForTest configuration = SearchConfigurationForTest.noTestDefaults()
      .addProperty( ElasticsearchEnvironment.SERVER_URI, httpUrlFor( wireMockRule1 ) );
  String payload = "{ \"foo\": \"bar\" }";
  String statusMessage = "StatusMessage";
  String responseBody = "{ \"foo\": \"bar\" }";
  wireMockRule1.stubFor( post( urlPathLike( "/myIndex/myType" ) )
      .withRequestBody( equalToJson( payload ) )
      .willReturn( elasticsearchResponse().withStatus( 200 )
          .withStatusMessage( statusMessage )
          .withBody( responseBody ) ) );
  try ( ElasticsearchClient client = createClient( configuration ) ) {
    ElasticsearchResponse result = doPost( client, "/myIndex/myType", payload );
    assertThat( result.getStatusCode() ).as( "status code" ).isEqualTo( 200 );
    assertThat( result.getStatusMessage() ).as( "status message" ).isEqualTo( statusMessage );
    JsonHelper.assertJsonEquals( responseBody, result.getBody().toString() );
    wireMockRule1.verify( postRequestedFor( urlPathLike( "/myIndex/myType" ) ) );
  }
}

代码示例来源:origin: hibernate/hibernate-search

@Test
@TestForIssue(jiraKey = "HSEARCH-2235")
public void multipleHosts() throws Exception {
  SearchConfigurationForTest configuration = SearchConfigurationForTest.noTestDefaults()
      .addProperty( ElasticsearchEnvironment.SERVER_URI,
          httpUrlFor( wireMockRule1 ) + " " + httpUrlFor( wireMockRule2 ) );
  String payload = "{ \"foo\": \"bar\" }";
  wireMockRule1.stubFor( post( urlPathLike( "/myIndex/myType" ) )
      .withRequestBody( equalToJson( payload ) )
      .willReturn( elasticsearchResponse().withStatus( 200 ) ) );
  wireMockRule2.stubFor( post( urlPathLike( "/myIndex/myType" ) )
      .withRequestBody( equalToJson( payload ) )
      .willReturn( elasticsearchResponse().withStatus( 200 ) ) );
  try ( ElasticsearchClient client = createClient( configuration ) ) {
    ElasticsearchResponse result = doPost( client, "/myIndex/myType", payload );
    assertThat( result.getStatusCode() ).as( "status code" ).isEqualTo( 200 );
    result = doPost( client, "/myIndex/myType", payload );
    assertThat( result.getStatusCode() ).as( "status code" ).isEqualTo( 200 );
    wireMockRule1.verify( postRequestedFor( urlPathLike( "/myIndex/myType" ) ) );
    wireMockRule2.verify( postRequestedFor( urlPathLike( "/myIndex/myType" ) ) );
  }
}

代码示例来源:origin: hibernate/hibernate-search

@Test
@TestForIssue(jiraKey = "HSEARCH-2453")
public void authentication() throws Exception {
  String username = "ironman";
  String password = "j@rV1s";
  SearchConfigurationForTest configuration = SearchConfigurationForTest.noTestDefaults()
      .addProperty( ElasticsearchEnvironment.SERVER_URI, httpUrlFor( wireMockRule1 ) )
      .addProperty( ElasticsearchEnvironment.SERVER_USERNAME, username )
      .addProperty( ElasticsearchEnvironment.SERVER_PASSWORD, password );
  String payload = "{ \"foo\": \"bar\" }";
  /*
   * Jest (actually, the Apache HTTP client) always tries an unauthenticated request first,
   * and only provides an authentication token if it gets a 401 status.
   * Thus we must stub the response for unauthenticated requests too.
   */
  wireMockRule1.stubFor( post( urlPathLike( "/myIndex/myType/_search" ) )
      .withRequestBody( equalToJson( payload ) )
      .willReturn(
          elasticsearchResponse().withStatus( 401 )
          .withHeader( HttpHeader.WWW_AUTHENTICATE.asString(), "Basic" ) )
      );
  wireMockRule1.stubFor( post( urlPathLike( "/myIndex/myType/_search" ) )
      .withBasicAuth( username, password )
      .withRequestBody( equalToJson( payload ) )
      .willReturn( elasticsearchResponse().withStatus( 200 ) ) );
  try ( ElasticsearchClient client = createClient( configuration ) ) {
    ElasticsearchResponse result = doPost( client, "/myIndex/myType/_search", payload );
    assertThat( result.getStatusCode() ).as( "status code" ).isEqualTo( 200 );
  }
}

代码示例来源:origin: hibernate/hibernate-search

@Test
public void timeout_request() throws Exception {
  SearchConfigurationForTest configuration = SearchConfigurationForTest.noTestDefaults()
      .addProperty( ElasticsearchEnvironment.SERVER_URI, httpUrlFor( wireMockRule1 ) )
      .addProperty( ElasticsearchEnvironment.SERVER_READ_TIMEOUT, "99999" )
      .addProperty( ElasticsearchEnvironment.SERVER_REQUEST_TIMEOUT, "1000" );
  String payload = "{ \"foo\": \"bar\" }";
  wireMockRule1.stubFor( post( urlPathLike( "/myIndex/myType" ) )
      .withRequestBody( equalToJson( payload ) )
      .willReturn(
          elasticsearchResponse()
          .withFixedDelay( 2000 )
      ) );
  thrown.expect(
      isException( CompletionException.class )
          .causedBy( TimeoutException.class )
      .build()
  );
  try ( ElasticsearchClient client = createClient( configuration ) ) {
    doPost( client, "/myIndex/myType", payload );
  }
}

代码示例来源:origin: georocket/georocket

/**
 * Test if the client can create an index with settings
 * @param context the test context
 */
@Test
public void createIndexWithSettings(TestContext context) {
 StubMapping settings = wireMockRule1.stubFor(put(urlEqualTo("/" + INDEX))
  .withRequestBody(equalToJson(SETTINGS_WRAPPER.encode()))
  .willReturn(aResponse()
   .withBody(ACKNOWLEDGED.encode())
   .withStatus(200)));
 Async async = context.async();
 client.createIndex(SETTINGS).subscribe(ok -> {
  context.assertTrue(ok);
  wireMockRule1.verify(putRequestedFor(settings.getRequest().getUrlMatcher()));
  async.complete();
 }, context::fail);
}

代码示例来源:origin: hibernate/hibernate-search

@Test
public void timeout_read() throws Exception {
  SearchConfigurationForTest configuration = SearchConfigurationForTest.noTestDefaults()
      .addProperty( ElasticsearchEnvironment.SERVER_URI, httpUrlFor( wireMockRule1 ) )
      .addProperty( ElasticsearchEnvironment.SERVER_READ_TIMEOUT, "1000" )
      .addProperty( ElasticsearchEnvironment.SERVER_REQUEST_TIMEOUT, "99999" );
  String payload = "{ \"foo\": \"bar\" }";
  wireMockRule1.stubFor( post( urlPathLike( "/myIndex/myType" ) )
      .withRequestBody( equalToJson( payload ) )
      .willReturn(
          elasticsearchResponse()
          .withFixedDelay( 2000 )
      ) );
  thrown.expect(
      isException( CompletionException.class )
          .causedBy( IOException.class )
      .build()
  );
  try ( ElasticsearchClient client = createClient( configuration ) ) {
    doPost( client, "/myIndex/myType", payload );
  }
}

代码示例来源:origin: hibernate/hibernate-search

@Test
public void unparseable() throws Exception {
  SearchConfigurationForTest configuration = SearchConfigurationForTest.noTestDefaults()
      .addProperty( ElasticsearchEnvironment.SERVER_URI, httpUrlFor( wireMockRule1 ) );
  String payload = "{ \"foo\": \"bar\" }";
  wireMockRule1.stubFor( post( urlPathLike( "/myIndex/myType" ) )
      .withRequestBody( equalToJson( payload ) )
      .willReturn(
          elasticsearchResponse()
          .withBody( "'unparseable" )
          .withFixedDelay( 2000 )
      ) );
  thrown.expect(
      isException( CompletionException.class )
          .causedBy( SearchException.class )
              .withMessage( "HSEARCH400089" )
          .causedBy( JsonSyntaxException.class )
      .build()
  );
  try ( ElasticsearchClient client = createClient( configuration ) ) {
    doPost( client, "/myIndex/myType", payload );
  }
}

相关文章