本文整理汇总了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")));
}
示例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")));
}
示例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")));
}
示例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)));
}
示例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");
}
示例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());
}
示例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());
}
示例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());
}
示例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());
}
示例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());
}
示例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());
}
示例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")));
*/
}
示例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")));
}
示例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")));
}