Java Code Examples for com.github.tomakehurst.wiremock.client.WireMock#configureFor()

The following examples show how to use com.github.tomakehurst.wiremock.client.WireMock#configureFor() . 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: DynamicActionSalesforceITCase.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Before
public void setupMocks() {
    WireMock.configureFor(wireMock.port());
    stubFor(WireMock

        .post(urlEqualTo(
            "/api/v1/connectors/salesforce/actions/io.syndesis:salesforce-create-or-update-connector:latest"))//
        .withHeader("Accept", equalTo("application/json"))//
        .withRequestBody(equalToJson("{\"clientId\":\"a-client-id\",\"sObjectName\":null,\"sObjectIdName\":null}"))
        .willReturn(aResponse()//
            .withStatus(200)//
            .withHeader("Content-Type", "application/json")//
            .withBody(read("/verifier-response-salesforce-no-properties.json"))));

    stubFor(WireMock
        .post(urlEqualTo(
            "/api/v1/connectors/salesforce/actions/io.syndesis:salesforce-create-or-update-connector:latest"))//
        .withHeader("Accept", equalTo("application/json"))//
        .withRequestBody(
            equalToJson("{\"clientId\":\"a-client-id\",\"sObjectName\":\"Contact\",\"sObjectIdName\":null}"))
        .willReturn(aResponse()//
            .withStatus(200)//
            .withHeader("Content-Type", "application/json")//
            .withBody(read("/verifier-response-salesforce-type-contact.json"))));
}
 
Example 2
Source File: DynamicActionSqlStoredITCase.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Before
public void setupMocks() {
    WireMock.configureFor(wireMock.port());
    stubFor(WireMock
        .post(urlEqualTo(
            "/api/v1/connectors/sql/actions/sql-stored-connector"))//
        .withHeader("Accept", containing("application/json"))//
        .withRequestBody(
                equalToJson("{\"template\":null,\"Pattern\":\"To\",\"procedureName\":null,\"user\":\"sa\"}"))
        .willReturn(aResponse()//
            .withStatus(200)//
            .withHeader("Content-Type", "application/json")//
            .withBody(read("/verifier-response-sql-stored-list.json"))));

    stubFor(WireMock
        .post(urlEqualTo(
            "/api/v1/connectors/sql/actions/sql-stored-connector"))//
        .withHeader("Accept", equalTo("application/json"))//
        .withRequestBody(
                equalToJson("{\"template\":null,\"Pattern\":\"To\",\"user\":\"sa\",\"procedureName\":\"DEMO_ADD\"}"))
        .willReturn(aResponse()//
            .withStatus(200)//
            .withHeader("Content-Type", "application/json")//
            .withBody(read("/verifier-response-sql-stored-demo-add-metadata.json"))));
}
 
Example 3
Source File: RestEndPointMockerTest.java    From zerocode with Apache License 2.0 6 votes vote down vote up
@Test
public void willMockASimpleGetEndPoint() throws Exception {

    final MockStep mockStep = mockSteps.getMocks().get(0);
    String jsonBodyRequest = mockStep.getResponse().get("body").toString();

    WireMock.configureFor(9073);
    givenThat(get(urlEqualTo(mockStep.getUrl()))
            .willReturn(aResponse()
                    .withStatus(mockStep.getResponse().get("status").asInt())
                    .withHeader("Content-Type", APPLICATION_JSON)
                    .withBody(jsonBodyRequest)));

    ApacheHttpClientExecutor httpClientExecutor = new ApacheHttpClientExecutor();
    ClientRequest clientExecutor = httpClientExecutor.createRequest("http://localhost:9073" + mockStep.getUrl());
    clientExecutor.setHttpMethod("GET");
    ClientResponse serverResponse = clientExecutor.execute();

    final String respBodyAsString = (String) serverResponse.getEntity(String.class);
    JSONAssert.assertEquals(jsonBodyRequest, respBodyAsString, true);

    System.out.println("### zerocode: \n" + respBodyAsString);

}
 
Example 4
Source File: BasicHttpClientTest.java    From zerocode with Apache License 2.0 6 votes vote down vote up
@Test
public void willMockUTF8Response() throws Exception {

    final String response = "utf-8 encoded text";
    WireMock.configureFor(9073);
    givenThat(get(urlEqualTo("/charset/utf8"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "application/json; charset=UTF-8")
                    .withBody(response)));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet request = new HttpGet("http://localhost:9073" + "/charset/utf8");

    CloseableHttpResponse closeableHttpResponse = httpClient.execute(request);

    BasicHttpClient basicHttpClient = new BasicHttpClient();
    final String responseBodyActual = (String) basicHttpClient.handleResponse(closeableHttpResponse).getEntity();
    assertThat(responseBodyActual, CoreMatchers.is(response));
}
 
Example 5
Source File: WebhooksAcceptanceTest.java    From wiremock-webhooks-extension with Apache License 2.0 6 votes vote down vote up
@Before
public void init() {
    targetServer.addMockServiceRequestListener(new RequestListener() {
        @Override
        public void requestReceived(Request request, Response response) {
            if (request.getUrl().startsWith("/callback")) {
                latch.countDown();
            }
        }
    });
    reset();
    notifier.reset();
    targetServer.stubFor(any(anyUrl())
        .willReturn(aResponse().withStatus(200)));
    latch = new CountDownLatch(1);
    client = new WireMockTestClient(rule.port());
    WireMock.configureFor(targetServer.port());

    System.out.println("Target server port: " + targetServer.port());
    System.out.println("Under test server port: " + rule.port());
}
 
Example 6
Source File: FailingWebhookTest.java    From wiremock-webhooks-extension with Apache License 2.0 6 votes vote down vote up
@Before
public void init() {
  targetServer.addMockServiceRequestListener(new RequestListener() {
    @Override
    public void requestReceived(Request request, Response response) {
      if (request.getUrl().startsWith("/callback")) {
        latch.countDown();
      }
    }
  });
  reset();
  notifier.reset();
  targetServer.stubFor(any(anyUrl())
      .willReturn(aResponse().withStatus(200)));
  latch = new CountDownLatch(1);
  client = new WireMockTestClient(rule.port());
  WireMock.configureFor(targetServer.port());
}
 
Example 7
Source File: RestEndPointMockerTest.java    From zerocode with Apache License 2.0 5 votes vote down vote up
@Test
public void willMockASoapEndPoint() throws Exception {

    WireMock.configureFor(9073);

    String soapRequest = smartUtils.getJsonDocumentAsString("unit_test_files/soap_stub/soap_request.xml");

    final MappingBuilder requestBuilder = post(urlEqualTo("/samples/testcomplete12/webservices/Service.asmx"));
    requestBuilder.withRequestBody(equalToXml(soapRequest));
    requestBuilder.withHeader("Content-Type", equalTo("application/soap+xml; charset=utf-8"));

    String soapResponseExpected = smartUtils.getJsonDocumentAsString("unit_test_files/soap_stub/soap_response.xml");
    stubFor(requestBuilder
            .willReturn(aResponse()
                    .withStatus(200)
                    //.withHeader("Content-Type", APPLICATION_JSON)
                    .withBody(soapResponseExpected)));

    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost request = new HttpPost("http://localhost:9073" + "/samples/testcomplete12/webservices/Service.asmx");
    request.addHeader("Content-Type", "application/soap+xml; charset=utf-8");
    StringEntity entity = new StringEntity(soapRequest);
    request.setEntity(entity);
    HttpResponse response = httpClient.execute(request);

    final String responseBodyActual = IOUtils.toString(response.getEntity().getContent(), "UTF-8");

    assertThat(responseBodyActual, is(soapResponseExpected));
}
 
Example 8
Source File: BrowserStackWebDriverBuilderTest.java    From justtestlah with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws IOException {
  // Wiremock setup
  wireMockPort = getAvailablePort();
  wireMockServer = new WireMockServer(options().port(wireMockPort));
  wireMockServer.start();
  WireMock.configureFor("localhost", wireMockPort);
}
 
Example 9
Source File: WnsTest.java    From java-wns with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();
    wireMockServer = new WireMockServer(wireMockConfig().port(8089));
    wireMockServer.start();
    WireMock.configureFor("localhost", 8089);

    // Stub server response for access token
    stubFor(post(urlEqualTo("/accesstoken.srf"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", MediaType.APPLICATION_JSON)
                    .withBody("{\"access_token\":\"access_token\",\"token_type\":\"token_type\",\"expires_in\":9999}")));
}
 
Example 10
Source File: RestEndPointMockerTest.java    From zerocode with Apache License 2.0 5 votes vote down vote up
@Before
public void beforeMethod() throws Exception {
    restEndPointMocker = new RestEndPointMocker();

    WireMock.configureFor(9073);

    jsonDocumentAsString = smartUtils.getJsonDocumentAsString("integration_test_files/wiremock_integration/wiremock_end_point_json_body.json");
    scenarioDeserialized = objectMapper.readValue(jsonDocumentAsString, ScenarioSpec.class);
    mockSteps = smartUtils.getMapper().readValue(scenarioDeserialized.getSteps().get(0).getRequest().toString(), MockSteps.class);

}
 
Example 11
Source File: WireMockExtension.java    From junit-servers with MIT License 5 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) {
	WireMockServer wireMockServer = new WireMockServer(wireMockConfig()
		.dynamicPort()
	);

	wireMockServer.start();

	WireMock.configureFor("localhost", wireMockServer.port());

	getStore(context).put(STORE_KEY, wireMockServer);
}
 
Example 12
Source File: WireMockConfiguration.java    From spring-cloud-contract with Apache License 2.0 5 votes vote down vote up
private void updateCurrentServer() {
	WireMock.configureFor(new WireMock(this.server));
	this.running = true;
	if (log.isDebugEnabled() && this.server.isRunning()) {
		log.debug("Started WireMock at port [" + this.server.port() + "]. It has ["
				+ this.server.getStubMappings().size() + "] mappings registered");
	}
}
 
Example 13
Source File: WireMockExtension.java    From spring-data-dev-tools with Apache License 2.0 4 votes vote down vote up
@Override
public void beforeEach(ExtensionContext context) {
	WireMock.configureFor(configuration.portNumber());
	this.start();
}
 
Example 14
Source File: RemoteUrlTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
    wireMockServer = new WireMockServer(WIRE_MOCK_PORT);
    wireMockServer.start();
    WireMock.configureFor(WIRE_MOCK_PORT);
}
 
Example 15
Source File: ValidatorTest.java    From validator-badge with Apache License 2.0 4 votes vote down vote up
@BeforeClass
private void setUpWireMockServer() throws IOException {
    this.wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
    this.wireMockServer.start();
    this.serverPort = wireMockServer.port();
    WireMock.configureFor(this.serverPort);

    String pathFile = FileUtils.readFileToString(new File("src/test/resources/valid_oas3.yaml"));

    WireMock.stubFor(get(urlPathMatching("/valid/yaml"))
            .willReturn(aResponse()
                    .withStatus(HttpURLConnection.HTTP_OK)
                    .withHeader("Content-type", "application/yaml")
                    .withBody(pathFile
                            .getBytes(StandardCharsets.UTF_8))));

    pathFile = FileUtils.readFileToString(new File("src/test/resources/invalid_oas3.yaml"));

    WireMock.stubFor(get(urlPathMatching("/invalid/yaml"))
            .willReturn(aResponse()
                    .withStatus(HttpURLConnection.HTTP_OK)
                    .withHeader("Content-type", "application/yaml")
                    .withBody(pathFile
                            .getBytes(StandardCharsets.UTF_8))));

    pathFile = FileUtils.readFileToString(new File("src/test/resources/valid_swagger2.yaml"));

    WireMock.stubFor(get(urlPathMatching("/validswagger/yaml"))
            .willReturn(aResponse()
                    .withStatus(HttpURLConnection.HTTP_OK)
                    .withHeader("Content-type", "application/yaml")
                    .withBody(pathFile
                            .getBytes(StandardCharsets.UTF_8))));

    pathFile = FileUtils.readFileToString(new File("src/test/resources/invalid_swagger2.yaml"));

    WireMock.stubFor(get(urlPathMatching("/invalidswagger/yaml"))
            .willReturn(aResponse()
                    .withStatus(HttpURLConnection.HTTP_OK)
                    .withHeader("Content-type", "application/yaml")
                    .withBody(pathFile
                            .getBytes(StandardCharsets.UTF_8))));

}
 
Example 16
Source File: SwaggerHubUploadTest.java    From swaggerhub-gradle-plugin with Apache License 2.0 4 votes vote down vote up
private void startMockServer(int port) {
    wireMockServer = new WireMockServer(options().port(port));
    wireMockServer.start();
    WireMock.configureFor("localhost", wireMockServer.port());
}
 
Example 17
Source File: WiremockArquillianTest.java    From microprofile-rest-client with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void setupServer() {
    setupWireMockConnection();
    WireMock.configureFor(scheme, host, port, context);
    WireMock.reset();
}
 
Example 18
Source File: WireMockUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public static void startWireMockServer() {
    WIRE_MOCK.start();
    WireMock.configureFor(WIRE_MOCK.port());
}
 
Example 19
Source File: SwaggerHubUploadTest.java    From swaggerhub-maven-plugin with Apache License 2.0 4 votes vote down vote up
private void startMockServer(int port) {
    wireMockServer = new WireMockServer(options().port(port));
    wireMockServer.start();
    WireMock.configureFor("localhost", wireMockServer.port());
}
 
Example 20
Source File: WireMockExtension.java    From wiremock-extension with MIT License 4 votes vote down vote up
private static void startServer(final WireMockServer server) {
	if (!server.isRunning()) {
		server.start();
		WireMock.configureFor("localhost", server.port());
	}
}