com.github.tomakehurst.wiremock.junit.WireMockRule Java Examples

The following examples show how to use com.github.tomakehurst.wiremock.junit.WireMockRule. 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: BitbucketWireMockBase.java    From blueocean-plugin with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
    super.setup();
    this.authenticatedUser = login();
    WireMockRule bitbucketApi = getWireMockRule();

    String files = wireMockFileSystemPath()+"__files";
    String mappings = wireMockFileSystemPath()+"mappings";
    String proxyUrl = wireMockProxyUrl();

    new File(mappings).mkdirs();
    new File(files).mkdirs();
    bitbucketApi.enableRecordMappings(new SingleRootFileSource(mappings),
            new SingleRootFileSource(files));

    if (useProxy) {
        bitbucketApi.stubFor(
            WireMock.get(urlMatching(".*")).atPriority(10).willReturn(aResponse()
                .proxiedFrom(proxyUrl)));
    }

    this.apiUrl = String.format("http://localhost:%s",bitbucketApi.port());
}
 
Example #2
Source File: TestRealm.java    From oic-auth-plugin with MIT License 6 votes vote down vote up
public TestRealm(WireMockRule wireMockRule, String userInfoServerUrl, String emailFieldName, String groupFieldName) throws IOException {
    super(
         CLIENT_ID,
        "secret",
        null,
        "http://localhost:" + wireMockRule.port() + "/token",
        "http://localhost:" + wireMockRule.port() + "/authorization",
         userInfoServerUrl,
        null,
        null,
        null,
         FULL_NAME_FIELD,
         emailFieldName,
        null,
         groupFieldName,
        false,
        false,
        null,
        null,
        false,
        null,
        null,
        null,
        "manual"
    );
}
 
Example #3
Source File: HttpRequestTest.java    From blueocean-plugin with MIT License 6 votes vote down vote up
private static WireMockRule createWireMockServerRule(String resourceFolder) {
    final WireMockRule rule = new WireMockRule(
        wireMockConfig()
            .dynamicPort()
            .usingFilesUnderClasspath(resourceFolder)
    );

    String mappingsPath = String.format("src/test/resources/%s/mappings", resourceFolder);
    String filesPath = String.format("src/test/resources/%s/__files", resourceFolder);

    new File(mappingsPath).mkdirs();
    new File(filesPath).mkdirs();

    rule.enableRecordMappings(
        new SingleRootFileSource(mappingsPath),
        new SingleRootFileSource(filesPath)
    );

    return rule;
}
 
Example #4
Source File: MockRegistry.java    From TeaStore with Apache License 2.0 6 votes vote down vote up
private void initializeServiceQueryStubs(WireMockRule rule,
		List<Integer> persistencePorts, List<Integer> recommenderPorts)
		throws JsonProcessingException {
	List<String> persistences = new LinkedList<String>();
	for (int persistencePort: persistencePorts) {
		persistences.add("localhost:" + persistencePort);
	}
	String json = new ObjectMapper().writeValueAsString(persistences);
	rule.stubFor(WireMock.get(WireMock.urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.PERSISTENCE.getServiceName() + "/"))
					.willReturn(WireMock.okJson(json)));
	List<String> recommenders = new LinkedList<String>();
	for (int recommenderPort: recommenderPorts) {
		recommenders.add("localhost:" + recommenderPort);
	}
	json = new ObjectMapper().writeValueAsString(recommenders);
	rule.stubFor(WireMock.get(WireMock.urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.RECOMMENDER.getServiceName() + "/"))
					.willReturn(WireMock.okJson(json)));
}
 
Example #5
Source File: ClientAuthenticationPKCS12Test.java    From gravitee-gateway with Apache License 2.0 5 votes vote down vote up
@Override
protected WireMockRule getWiremockRule() {
    return new WireMockRule(wireMockConfig()
            .dynamicPort()
            .dynamicHttpsPort()
            .needClientAuth(true)
            .trustStorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/truststore01.jks"))
            .trustStorePassword("password")
            .keystorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/keystore01.jks"))
            .keystorePassword("password"));
}
 
Example #6
Source File: SSLJKSTrustStoreTest.java    From gravitee-gateway with Apache License 2.0 5 votes vote down vote up
@Override
protected WireMockRule getWiremockRule() {
    return new WireMockRule(wireMockConfig()
            .dynamicPort()
            .dynamicHttpsPort()
            .keystorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/keystore01.jks"))
            .keystorePassword("password"));
}
 
Example #7
Source File: TomcatTestHandler.java    From TeaStore with Apache License 2.0 5 votes vote down vote up
private void initializeMockRegistry(WireMockRule wireMockRule, int count, int startport)
		throws JsonProcessingException {
	List<String> strings = new LinkedList<String>();
	for (int i = 0; i < count; i++) {
		strings.add("localhost:" + (startport + i));
	}
	String json = new ObjectMapper().writeValueAsString(strings);
	wireMockRule.stubFor(WireMock.get(WireMock.urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.PERSISTENCE.getServiceName()))
					.willReturn(WireMock.okJson(json)));
	wireMockRule.stubFor(WireMock.post(WireMock.urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/"
					+ Service.PERSISTENCE.getServiceName() + "/*"))
	.willReturn(WireMock.ok()));
}
 
Example #8
Source File: ClientAuthenticationPEMTest.java    From gravitee-gateway with Apache License 2.0 5 votes vote down vote up
@Override
protected WireMockRule getWiremockRule() {
    return new WireMockRule(wireMockConfig()
            .dynamicPort()
            .dynamicHttpsPort()
            .needClientAuth(true)
            .trustStorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/truststore01.jks"))
            .trustStorePassword("password")
            .keystorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/keystore01.jks"))
            .keystorePassword("password"));
}
 
Example #9
Source File: MockRegistry.java    From TeaStore with Apache License 2.0 5 votes vote down vote up
private void initializeUpdateAndHeartbeatStubs(WireMockRule rule) {
	rule.stubFor(WireMock.put(WireMock.urlMatching(
			"/tools.descartes.teastore.registry/rest/services/.*"))
					.willReturn(WireMock.ok()));
	rule.stubFor(WireMock.delete(WireMock.urlMatching(
			"/tools.descartes.teastore.registry/rest/services/.*"))
					.willReturn(WireMock.ok()));
}
 
Example #10
Source File: ClientAuthenticationJKSTest.java    From gravitee-gateway with Apache License 2.0 5 votes vote down vote up
@Override
protected WireMockRule getWiremockRule() {
    return new WireMockRule(wireMockConfig()
            .dynamicPort()
            .dynamicHttpsPort()
            .needClientAuth(true)
            .trustStorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/truststore01.jks"))
            .trustStorePassword("password")
            .keystorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/keystore01.jks"))
            .keystorePassword("password"));
}
 
Example #11
Source File: SSLPEMTrustStoreTest.java    From gravitee-gateway with Apache License 2.0 5 votes vote down vote up
@Override
protected WireMockRule getWiremockRule() {
    return new WireMockRule(wireMockConfig()
            .dynamicPort()
            .dynamicHttpsPort()
            .keystorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/keystore01.jks"))
            .keystorePassword("password"));
}
 
Example #12
Source File: MockPersistenceProvider.java    From TeaStore with Apache License 2.0 5 votes vote down vote up
/**
 * Create a mock persistence using a wire mock rule.
 * Recommended: Use {@link #DEFAULT_MOCK_PERSISTENCE_PORT} as port.
 * @param rule The wire mock rule to create the mocking stubs for.
 */
public MockPersistenceProvider(WireMockRule rule) {
	this.port = rule.port();
	rule.stubFor(WireMock.get(WireMock.urlEqualTo(
			"/" + Service.PERSISTENCE.getServiceName() + "/rest/orders"))
					.willReturn(WireMock.okJson(getOrders())));
	rule.stubFor(WireMock.get(WireMock.urlEqualTo(
			"/" + Service.PERSISTENCE.getServiceName() + "/rest/orderitems"))
					.willReturn(WireMock.okJson(getOrderItems())));
	rule.stubFor(WireMock.get(WireMock.urlEqualTo(
			"/" + Service.PERSISTENCE.getServiceName() + "/rest/generatedb/finished"))
					.willReturn(WireMock.ok("true")));
}
 
Example #13
Source File: ClientAuthenticationPEMInlineTest.java    From gravitee-gateway with Apache License 2.0 5 votes vote down vote up
@Override
protected WireMockRule getWiremockRule() {
    return new WireMockRule(wireMockConfig()
            .dynamicPort()
            .dynamicHttpsPort()
            .needClientAuth(true)
            .trustStorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/truststore01.jks"))
            .trustStorePassword("password")
            .keystorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/keystore01.jks"))
            .keystorePassword("password"));
}
 
Example #14
Source File: SSLPKCS12TrustStoreTest.java    From gravitee-gateway with Apache License 2.0 5 votes vote down vote up
@Override
protected WireMockRule getWiremockRule() {
    return new WireMockRule(wireMockConfig()
            .dynamicPort()
            .dynamicHttpsPort()
            .keystorePath(ResourceUtils.toPath("io/gravitee/gateway/standalone/keystore01.jks"))
            .keystorePassword("password"));
}
 
Example #15
Source File: WiremockStubbing.java    From pay-publicapi with MIT License 5 votes vote down vote up
public static void stubPublicAuthV1ApiAuth(WireMockRule wireMockRule, Account account, String token) throws JsonProcessingException {
    Map<String, String> entity = ImmutableMap.of("account_id", account.getAccountId(), "token_type", account.getPaymentType().name());
    String json = new ObjectMapper().writeValueAsString(entity);
    wireMockRule.stubFor(get(urlEqualTo("/v1/api/auth"))
            .withHeader(AUTHORIZATION, equalTo("Bearer " + token))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", APPLICATION_JSON)
                    .withBody(json)));
}
 
Example #16
Source File: WireMockRuleFactory.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
public WireMockRule getRule(Options options) {
    if (urlToMock != null && !urlToMock.isEmpty()) {
        return new WireMockRecorderRule(options, urlToMock);
    } else {
        return new WireMockRule(options);
    }
}
 
Example #17
Source File: WireMockBase.java    From blueocean-plugin with MIT License 5 votes vote down vote up
protected static WireMockRule createWireMockServerRule(String resourceFolder, String baseUrl) {
    ReplaceUrlTransformer replaceUrlTransformer = new ReplaceUrlTransformer();

    final WireMockRule rule = new WireMockRule(
        wireMockConfig()
            .dynamicPort()
            .dynamicHttpsPort()
            .usingFilesUnderClasspath(resourceFolder)
            .extensions(
                new GzipDecompressTransformer(),
                replaceUrlTransformer
            )
    );

    String mappingsPath = String.format("src/test/resources/%s/mappings", resourceFolder);
    String filesPath = String.format("src/test/resources/%s/__files", resourceFolder);

    new File(mappingsPath).mkdirs();
    new File(filesPath).mkdirs();

    rule.enableRecordMappings(
        new SingleRootFileSource(mappingsPath),
        new SingleRootFileSource(filesPath)
    );

    if (useProxy) {
        rule.stubFor(
            WireMock.get(urlMatching(".*"))
                .atPriority(10)
                .willReturn(aResponse().proxiedFrom(baseUrl)));
    }

    replaceUrlTransformer.configure(baseUrl, rule);

    return rule;
}
 
Example #18
Source File: SecurityTestRule.java    From cloud-security-xsuaa-integration with Apache License 2.0 5 votes vote down vote up
/**
 * @deprecated use {@link #getWireMockServer()} method instead. Note that
 *             WireMockServer is the base class of WireMockRule.
 *
 */
@Nullable
@Deprecated
public WireMockRule getWireMockRule() {
	throw new UnsupportedOperationException(
			"Deprecated since version 2.6.0. Please use getWireMockServer instead. WireMockServer is the base class of WireMockRule.");
}
 
Example #19
Source File: TestRealm.java    From oic-auth-plugin with MIT License 4 votes vote down vote up
public TestRealm(WireMockRule wireMockRule) throws IOException {
    this(wireMockRule, null);
}
 
Example #20
Source File: TestRealm.java    From oic-auth-plugin with MIT License 4 votes vote down vote up
public TestRealm(WireMockRule wireMockRule, String userInfoServerUrl) throws IOException {
    this(wireMockRule, userInfoServerUrl, EMAIL_FIELD, GROUPS_FIELD);
}
 
Example #21
Source File: BbServerWireMock.java    From blueocean-plugin with MIT License 4 votes vote down vote up
@Override
protected WireMockRule getWireMockRule() {
    return bitbucketApi;
}
 
Example #22
Source File: ConfigConvergenceCheckerTest.java    From vespa with Apache License 2.0 4 votes vote down vote up
private URI testServer(WireMockRule wireMock) {
    return URI.create("http://127.0.0.1:" + wireMock.port());
}
 
Example #23
Source File: AbstractGatewayTest.java    From gravitee-gateway with Apache License 2.0 4 votes vote down vote up
protected WireMockRule getWiremockRule() {
    return new WireMockRule(
            wireMockConfig()
                    .dynamicPort()
                    .extensions(new ResponseTemplateTransformer(true)));
}
 
Example #24
Source File: WireMockRuleFactory.java    From github-branch-source-plugin with MIT License 4 votes vote down vote up
public WireMockRule getRule(int port) {
    return getRule(wireMockConfig().port(port));
}
 
Example #25
Source File: GHMockRule.java    From github-integration-plugin with MIT License 4 votes vote down vote up
public GHMockRule(WireMockRule mocked) {
    this.service = mocked;
}
 
Example #26
Source File: GHMockRule.java    From github-integration-plugin with MIT License 4 votes vote down vote up
/**
 * @return wiremock rule
 */
public WireMockRule service() {
    return service;
}
 
Example #27
Source File: APIProviderHostObjectTest.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Test
public void testMutualSSLEnabledBackend() {
    wireMockRule = new WireMockRule(wireMockConfig()
            .port(18082)
            .httpsPort(18081)
            .needClientAuth(true)
            .trustStoreType("JKS")
            .keystoreType("JKS")
            .keystorePath(KEYSTORE_FILE_PATH)
            .trustStorePath(TRUSTSTORE_FILE_PATH)
            .trustStorePassword("wso2carbon")
            .keystorePassword("wso2carbon"));
    wireMockRule.start();
    // Mock service for key manager token endpoint
    wireMockRule.stubFor(head(urlEqualTo("/test"))
            .willReturn(aResponse()
                    .withStatus(200)
                    .withBody("{success}")
                    .withHeader("Content-Type", "application/json")
            ));
    System.setProperty("javax.net.ssl.keyStoreType", "JKS");
    System.setProperty("javax.net.ssl.keyStore", KEYSTORE_FILE_PATH_CLIENT);
    System.setProperty("javax.net.ssl.keyStorePassword", "wso2carbon");
    System.setProperty("javax.net.ssl.trustStore", TRUSTSTORE_FILE_PATH_CLIENT);
    System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
    //Success case
    org.mozilla.javascript.NativeObject obj =
            HostObjectUtils.sendHttpHEADRequest("https://localhost:18081/test",
                    "404");
    Assert.assertEquals("success", obj.get("response"));
    //Non available path
    org.mozilla.javascript.NativeObject objError =
            HostObjectUtils.sendHttpHEADRequest("https://localhost:18081/best",
                    "404");
    Assert.assertNotEquals("success", objError.get("response"));
    //Error Port
    org.mozilla.javascript.NativeObject objErrorPort =
            HostObjectUtils.sendHttpHEADRequest("https://localhost:8082/best",
                    "404");
    Assert.assertNotEquals("success", objErrorPort.get("response"));
    //Invalid credentials
    System.setProperty("javax.net.ssl.trustStorePassword", "Wrong-Password");
    org.mozilla.javascript.NativeObject objErrorSSL =
            HostObjectUtils.sendHttpHEADRequest("https://localhost:18081/best",
                    "404");
    Assert.assertNotEquals("success", objErrorSSL.get("response"));
    //With Proxy host and port
    System.setProperty(APIConstants.HTTP_PROXY_HOST, "localhost");
    System.setProperty(APIConstants.HTTP_PROXY_PORT, "18081");
    obj = HostObjectUtils.sendHttpHEADRequest("https://localhost:18081/test",
                    "404");
    Assert.assertEquals("success", obj.get("response"));
    wireMockRule.resetAll();
    wireMockRule.stop();

}
 
Example #28
Source File: WireMockUtils.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public static void verifyRequestCount(int expectedCount, WireMockRule wireMock) {
    wireMock.verify(expectedCount, anyRequestedFor(anyUrl()));
}
 
Example #29
Source File: CucumberRestStepsTestCase.java    From cucumber-rest-steps with MIT License 4 votes vote down vote up
@BeforeClass
public static void CucumberRestStepsTestCase() {
  wm = new WireMockRule(8080);
  wm.start();

  stubFor(
      get("/")
          .willReturn(ok())
  );

  stubFor(
      post("/withData")
          .withRequestBody(equalToJson("{\"test\": \"test\"}"))
          .willReturn(okJson("{\"foo\": \"bar\"}"))
  );

  stubFor(
      post("/withData")
          .withRequestBody(equalToJson("[{\"test\": \"test\"}]"))
          .willReturn(okJson("{\"foo\": \"bar\"}"))
  );

  stubFor(
      get(urlPathEqualTo("/withParams"))
          .withQueryParam("param", equalTo("paramValue"))
          .willReturn(okJson("[]"))
  );

  stubFor(
      get("/withHeaders")
          .withHeader("MyHeader", equalTo("MyHeaderValue"))
          .willReturn(
              aResponse()
                  .withHeader("MyHeader", "MyHeaderValue")
                  .withHeader("MultiHeader", "MultiHeaderValue1", "MultiHeaderValue2")
          )
  );

  stubFor(
      get("/withArray")
          .willReturn(okJson("[{\"foo\": \"bar\"}, {\"foo\": 3}, {\"foos\": [\"bar\", \"wee\"]}]"))
  );

  stubFor(
      get("/response")
          .willReturn(okJson("{\"foo\": \"bar\"}"))
  );

  stubFor(
      get("/response/bar")
          .willReturn(okJson("{\"bar\": \"wee\"}"))
  );

  stubFor(
      post("/withFile")
          .withMultipartRequestBody(
              aMultipart()
                  .withName("file")
          )
          .willReturn(ok())
  );
}
 
Example #30
Source File: IntegrationTestBase.java    From syndesis with Apache License 2.0 4 votes vote down vote up
protected void verifyWireMock(WireMockRule wiremock) {
    // override in derived class for extra verification
}