當前位置: 首頁>>代碼示例>>Java>>正文


Java WireMock.stubFor方法代碼示例

本文整理匯總了Java中com.github.tomakehurst.wiremock.client.WireMock.stubFor方法的典型用法代碼示例。如果您正苦於以下問題:Java WireMock.stubFor方法的具體用法?Java WireMock.stubFor怎麽用?Java WireMock.stubFor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.github.tomakehurst.wiremock.client.WireMock的用法示例。


在下文中一共展示了WireMock.stubFor方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: customerByIdShouldReturnACustomer

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void customerByIdShouldReturnACustomer() {

    WireMock.stubFor(WireMock.get(WireMock.urlMatching("/customers/1"))
            .willReturn(
                    WireMock.aResponse()
                            .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
                            .withStatus(HttpStatus.OK.value())
                            .withBody(asJson(customerById))
            ));

    Customer customer = client.getCustomerById(1L);
    BDDAssertions.then(customer.getFirstName()).isEqualToIgnoringCase("first");
    BDDAssertions.then(customer.getLastName()).isEqualToIgnoringCase("last");
    BDDAssertions.then(customer.getEmail()).isEqualToIgnoringCase("email");
    BDDAssertions.then(customer.getId()).isEqualTo(1L);
}
 
開發者ID:applied-continuous-delivery-livelessons,項目名稱:testing-101,代碼行數:18,代碼來源:CustomerClientWiremockTest.java

示例2: testDoubleEncodedUrlForGetForObject

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void testDoubleEncodedUrlForGetForObject() {
    initialAuthenticationMock();
    mockCsrfTokenEndpoint();

    WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/api/assets?path=abc%5Cdef"))
            .willReturn(WireMock.aResponse()
                    .withStatus(HttpStatus.FOUND.value())
                    .withHeader("Location", "/api/assets?path=abc%5Cdefs")
                    .withBody("")));

    Map<String, String> uriVariables = new HashMap<>();
    uriVariables.put("path", "abc\\def");
    authenticatedRestTemplate.getForObjectWithQueryStringParams("/api/assets", String.class, uriVariables);

    WireMock.verify(WireMock.getRequestedFor(WireMock.urlEqualTo("/api/assets?path=abc%5Cdef")));
}
 
開發者ID:box,項目名稱:mojito,代碼行數:18,代碼來源:AuthenticatedRestTemplateTest.java

示例3: testAuthRestTemplateForSessionTimeoutWithPostForObject

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void testAuthRestTemplateForSessionTimeoutWithPostForObject() {
    initialAuthenticationMock();
    mockCsrfTokenEndpoint();

    logger.debug("Mock returns 403 for POST request when session timeout");
    WireMock.stubFor(
            WireMock.post(WireMock.urlEqualTo("/random-403-endpoint"))
                    .willReturn(WireMock.aResponse().withStatus(HttpStatus.FORBIDDEN.value()))
    );

    String response = null;
    try {
        response = authenticatedRestTemplate.postForObject("random-403-endpoint", new HashMap<String, String>(), String.class);
    } catch (RestClientException e) {
        logger.debug("Expecting this to fail because the response for /random-403-endpoint has been stubbed to be always returning a 403");
        Assert.assertEquals("Tried to re-authenticate but the response remains to be unauthenticated", e.getMessage());
    }

    Assert.assertNull(response);

    WireMock.verify(2, WireMock.getRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*")));
    WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*")));
    WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlMatching("/random-403-endpoint")).withHeader("Accept", WireMock.matching("text/plain.*")).withHeader("X-CSRF-TOKEN", WireMock.matching("madeup-csrf-value")));
}
 
開發者ID:box,項目名稱:mojito,代碼行數:26,代碼來源:AuthenticatedRestTemplateTest.java

示例4: testAuthRestTemplateFor401

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void testAuthRestTemplateFor401() {
    initialAuthenticationMock();
    mockCsrfTokenEndpoint();

    logger.debug("Mock returns 401 for POST request when session timeout");
    WireMock.stubFor(
            WireMock.post(WireMock.urlEqualTo("/random-401-endpoint"))
                    .willReturn(WireMock.aResponse().withStatus(HttpStatus.UNAUTHORIZED.value()))
    );

    String response = null;
    try {
        response = authenticatedRestTemplate.postForObject("random-401-endpoint", new HashMap<String, String>(), String.class);
    } catch (RestClientException e) {
        logger.debug("Expecting this to fail because the response for /random-401-endpoint has been stubbed to be always returning a 401");
        Assert.assertEquals("Tried to re-authenticate but the response remains to be unauthenticated", e.getMessage());
    }

    Assert.assertNull(response);

    WireMock.verify(2, WireMock.getRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*")));
    WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*")));
    WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlMatching("/random-401-endpoint")).withHeader("Accept", WireMock.matching("text/plain.*")).withHeader("X-CSRF-TOKEN", WireMock.matching("madeup-csrf-value")));
}
 
開發者ID:box,項目名稱:mojito,代碼行數:26,代碼來源:AuthenticatedRestTemplateTest.java

示例5: initialAuthenticationMock

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
protected void initialAuthenticationMock() {
    String expectedResponse = "expected content";

    WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/" + formLoginConfig.getLoginFormPath()))
            .willReturn(WireMock.aResponse()
                    .withStatus(HttpStatus.OK.value())
                    .withHeader("Content-Type", "text/html")
                    .withBody(LOGIN_PAGE_HTML)));

    WireMock.stubFor(WireMock.post(WireMock.urlEqualTo("/" + formLoginConfig.getLoginPostPath()))
            .willReturn(WireMock.aResponse()
                    .withStatus(HttpStatus.FOUND.value())
                    .withHeader("Content-Type", "text/html")
                    .withHeader("Location", authenticatedRestTemplate.getURIForResource(formLoginConfig.getLoginRedirectPath()))
                    .withBody(expectedResponse)));
}
 
開發者ID:box,項目名稱:mojito,代碼行數:17,代碼來源:AuthenticatedRestTemplateTest.java

示例6: testGetItemsRequestCorrectFields

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
@Category(UnitTest.class)
public void testGetItemsRequestCorrectFields() {
    BoxAPIConnection api = new BoxAPIConnection("");
    api.setBaseURL("http://localhost:53620/");

    WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo("/collections/0/items/"))
            .withQueryParam("fields", WireMock.containing("name"))
            .withQueryParam("fields", WireMock.containing("description"))
            .willReturn(WireMock.aResponse()
                    .withHeader("Content-Type", "application/json")
                    .withBody("{}")));

    BoxCollection collection = new BoxCollection(api, "0");
    collection.getItems("name", "description");
}
 
開發者ID:box,項目名稱:box-java-sdk,代碼行數:17,代碼來源:BoxCollectionTest.java

示例7: testGetItemsRangeRequestsCorrectOffsetLimitAndFields

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
@Category(UnitTest.class)
public void testGetItemsRangeRequestsCorrectOffsetLimitAndFields() {
    BoxAPIConnection api = new BoxAPIConnection("");
    api.setBaseURL("http://localhost:53620/");

    WireMock.stubFor(WireMock.get(WireMock.urlPathEqualTo("/collections/0/items/"))
            .withQueryParam("offset", WireMock.equalTo("0"))
            .withQueryParam("limit", WireMock.equalTo("2"))
            .withQueryParam("fields", WireMock.containing("name"))
            .withQueryParam("fields", WireMock.containing("description"))
            .willReturn(WireMock.aResponse()
                    .withHeader("Content-Type", "application/json")
                    .withBody("{\"total_count\": 3, \"entries\":[]}")));

    BoxCollection collection = new BoxCollection(api, "0");
    PartialCollection<BoxItem.Info> items = collection.getItemsRange(0, 2, "name", "description");
    Assert.assertEquals(3L, items.fullSize());
    Assert.assertEquals(0, items.offset());
    Assert.assertEquals(2L, items.limit());

}
 
開發者ID:box,項目名稱:box-java-sdk,代碼行數:23,代碼來源:BoxCollectionTest.java

示例8: simpleTestGet

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void simpleTestGet() throws Exception {
    WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/test"))
        .willReturn(WireMock.aResponse()
            .withStatus(200)
            .withBody("OK")));

    StringHttpClient stringHttpClient = createStringHttpClient();
    String url = "http://localhost:8089/test";
    HttpResponse<String> response = stringHttpClient.request(
        Requests
        .get(url)
    ).await();

    assertEquals("OK", response.getBody());
}
 
開發者ID:julman99,項目名稱:futuristic,代碼行數:17,代碼來源:AbstractHttpAsyncEngineTest.java

示例9: simpleTestPost

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void simpleTestPost() throws Exception {
    WireMock.stubFor(WireMock.post(WireMock.urlEqualTo("/test"))
        .withRequestBody(WireMock.matching("a=1&b=2"))
        .willReturn(WireMock.aResponse()
            .withStatus(200)
            .withBody("OK")));

    StringHttpClient stringHttpClient = createStringHttpClient();
    String url = "http://localhost:8089/test";

    HttpResponse<String> response = stringHttpClient.request(
        Requests
            .post(url)
            .body(
                Bodies.withForm()
                .param("a", 1)
                .param("b", "2")
            )
    ).await();

    assertEquals("OK", response.getBody());
}
 
開發者ID:julman99,項目名稱:futuristic,代碼行數:24,代碼來源:AbstractHttpAsyncEngineTest.java

示例10: simpleTestPut

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void simpleTestPut() throws Exception {
    WireMock.stubFor(WireMock.put(WireMock.urlEqualTo("/test"))
        .willReturn(WireMock.aResponse()
            .withStatus(200)
            .withBody("OK")));

    StringHttpClient stringHttpClient = createStringHttpClient();
    String url = "http://localhost:8089/test";
    HttpResponse<String> response = stringHttpClient.request(
        Requests
        .put(url)
    )
    .await();

    assertEquals("OK", response.getBody());
}
 
開發者ID:julman99,項目名稱:futuristic,代碼行數:18,代碼來源:AbstractHttpAsyncEngineTest.java

示例11: simpleTestDelete

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void simpleTestDelete() throws Exception {
    WireMock.stubFor(WireMock.delete(WireMock.urlEqualTo("/test"))
        .willReturn(WireMock.aResponse()
            .withStatus(200)
            .withBody("OK")));

    StringHttpClient stringHttpClient = createStringHttpClient();
    String url = "http://localhost:8089/test";
    HttpResponse<String> response = stringHttpClient.request(
        Requests
        .delete(url)
    ).await();

    assertEquals("OK", response.getBody());
}
 
開發者ID:julman99,項目名稱:futuristic,代碼行數:17,代碼來源:AbstractHttpAsyncEngineTest.java

示例12: testHeaderSending

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void testHeaderSending() throws Exception {
    WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/test"))
        .withHeader("Test", WireMock.matching("ok"))
        .withHeader("Test2", WireMock.matching("ok2"))
        .willReturn(WireMock.aResponse()
            .withStatus(200)
            .withBody("OK")));

    StringHttpClient stringHttpClient = createStringHttpClient();
    String url = "http://localhost:8089/test";

    HttpResponse<String> response = stringHttpClient.request(
        Requests
            .get(url)
            .header("Test", "ok")
            .header("Test2", "ok2")
    ).await();

    assertEquals("OK", response.getBody());
}
 
開發者ID:julman99,項目名稱:futuristic,代碼行數:22,代碼來源:AbstractHttpAsyncEngineTest.java

示例13: testHttpCalls

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void testHttpCalls() throws IOException {
    WireMock.stubFor(WireMock
            .get(WireMock.urlEqualTo("/my/resource"))
            .withHeader("Accept", WireMock.equalTo("text/xml"))
            .willReturn(
                    WireMock.aResponse().withStatus(200).withHeader("Content-Type", "text/xml")
                            .withBody("<response>Some content</response>")));

    Geckoboard geckoboard = new Geckoboard("test");
    geckoboard.setBaseUrl("http://localhost:2020/");
    geckoboard.push(new MockedPush());
    /*
     * assertTrue(result.wasSuccessFul());
     * 
     * verify(postRequestedFor(urlMatching("/my/resource/[a-z0-9]+"))
     * .withRequestBody(matching(".*<message>1234</message>.*")) .withHeader("Content-Type",
     * notMatching("application/json")));
     */
}
 
開發者ID:geckoboard-java-api,項目名稱:geckoboard-java-api,代碼行數:21,代碼來源:GeckoboardTest.java

示例14: testUnPreparedAuthRestTemplateAndSessionTimeoutForGetForObject

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
@Test
public void testUnPreparedAuthRestTemplateAndSessionTimeoutForGetForObject() {
    initialAuthenticationMock();
    mockCsrfTokenEndpoint();

    WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/session-timeout-endpoint"))
            .willReturn(WireMock.aResponse()
                    .withStatus(HttpStatus.FOUND.value())
                    .withHeader("Location", "/login")
                    .withBody("")));

    String response = null;
    try {
        response = authenticatedRestTemplate.getForObject("session-timeout-endpoint", String.class);
    } catch (RestClientException e) {
        logger.debug("Expecting this to fail because the response for /session-timeout-endpoint has been stubbed to be always returning a 302");
        Assert.assertEquals("Tried to re-authenticate but the response remains to be unauthenticated", e.getMessage());
    }

    Assert.assertNull("There should have been no response", response);

    // Expecting 2 requests for each because initially, the AuthenticatedRestTemplate has not been prepared,
    // there hasn't been any session set or csrf token, so it'll authenticate first.
    // Then it'll resume the original request (ie. session-timeout-endpoint) but the response session timeout, so it
    // retries the auth flow again, and lastly, tries the session-timeout-endpoint again.
    // So in summary, this is the following expected flow:
    // 1. GET /login - because it has not been prepped, it starts auth flow
    // 2. POST /login
    // 3. GET /session-timeout-endpoint - response 302/login
    // 4. GET /login
    // 5. POST /login
    // 6. GET /session-timeout-endpoint
    WireMock.verify(2, WireMock.getRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*")));
    WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlMatching("/login")).withHeader("Accept", WireMock.matching("text/plain.*")));
    WireMock.verify(2, WireMock.getRequestedFor(WireMock.urlMatching("/session-timeout-endpoint")).withHeader("Accept", WireMock.matching("text/plain.*")).withHeader("X-CSRF-TOKEN", WireMock.matching("madeup-csrf-value")));
}
 
開發者ID:box,項目名稱:mojito,代碼行數:37,代碼來源:AuthenticatedRestTemplateTest.java

示例15: mockCsrfTokenEndpoint

import com.github.tomakehurst.wiremock.client.WireMock; //導入方法依賴的package包/類
protected void mockCsrfTokenEndpoint() {
    WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/" + formLoginConfig.getCsrfTokenPath()))
            .willReturn(WireMock.aResponse()
                    .withStatus(HttpStatus.OK.value())
                    .withHeader("Content-Type", "text/html")
                    .withBody("madeup-csrf-value")));
}
 
開發者ID:box,項目名稱:mojito,代碼行數:8,代碼來源:AuthenticatedRestTemplateTest.java


注:本文中的com.github.tomakehurst.wiremock.client.WireMock.stubFor方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。