本文整理汇总了Java中com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder类的典型用法代码示例。如果您正苦于以下问题:Java ResponseDefinitionBuilder类的具体用法?Java ResponseDefinitionBuilder怎么用?Java ResponseDefinitionBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ResponseDefinitionBuilder类属于com.github.tomakehurst.wiremock.client包,在下文中一共展示了ResponseDefinitionBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resetWireMock
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
/**
* Reset wire mock and re-configure stubbing.
*/
private void resetWireMock() {
WireMock.reset();
// Stub to return 200 for all requests
final ResponseDefinitionBuilder responseDefBuilder = aResponse().withStatus(200);
// XML Unmarshallers expect at least one level in the XML document.
if (model.getMetadata().isXmlProtocol()) {
responseDefBuilder.withBody("<foo></foo>");
}
stubFor(any(urlMatching(".*")).willReturn(responseDefBuilder));
}
示例2: toResponseBuilder
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
private ResponseDefinitionBuilder toResponseBuilder(GivenResponse givenResponse) {
final ResponseDefinitionBuilder responseBuilder = aResponse().withStatus(200);
if (givenResponse.getHeaders() != null) {
givenResponse.getHeaders().forEach(responseBuilder::withHeader);
}
if (givenResponse.getStatusCode() != null) {
responseBuilder.withStatus(givenResponse.getStatusCode());
}
if (givenResponse.getBody() != null) {
responseBuilder.withBody(givenResponse.getBody());
} else if (metadata.isXmlProtocol()) {
// XML Unmarshallers expect at least one level in the XML document. If no body is explicitly
// set by the test add a fake one here.
responseBuilder.withBody("<foo></foo>");
}
return responseBuilder;
}
示例3: test_resp
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_resp() {
MappingBuilder mockReq = get(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse().withStatus(200).withBody("data");
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.get(url);
httpClient.setResultReader(TextReader.INSTANCE);
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 200);
Assert.assertEquals(httpClient.getResultObject(), "data");
Assert.assertNull(httpClient.getErrorObject());
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例4: test_jsonResp
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_jsonResp() {
MappingBuilder mockReq = get(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse().withStatus(200).withBody("{\"prop1\": \"value1\"}");
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.get(url);
httpClient.setResultReader(new JsonReader()).setResultClass(HashMap.class);
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 200);
Map<String, Object> result = httpClient.getResultObject();
Assert.assertEquals(result.get("prop1"), "value1");
Assert.assertNull(httpClient.getErrorObject());
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例5: test_errorResp
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_errorResp() {
MappingBuilder mockReq = get(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse().withStatus(500).withBody("Error");
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.get(url);
httpClient.setResultReader(TextReader.INSTANCE);
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 500);
Assert.assertEquals(httpClient.getErrorObject(), "Error");
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例6: test_queryParams
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_queryParams() {
MappingBuilder mockReq = get(urlPathMatching("/action"));
mockReq.withQueryParam("p1", equalTo("v1"));
mockReq.withQueryParam("p2", equalTo("v 2"));
ResponseDefinitionBuilder mockResp = aResponse().withStatus(200);
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl() + "?" + new QueryParams().param("p1", "v1").param("p2", "v 2");
httpClient = HttpClient.get(url);
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 200);
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例7: test_resp
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_resp() {
MappingBuilder mockReq = post(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse().withStatus(200).withBody("data");
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.post(url);
httpClient.setResultReader(TextReader.INSTANCE);
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 200);
String result = httpClient.getResultObject();
Assert.assertEquals(result, "data");
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例8: test_jsonResp
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_jsonResp() {
MappingBuilder mockReq = post(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse().withStatus(200).withBody("{\"prop1\": \"value1\"}");
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.post(url);
httpClient.setResultReader(new JsonReader()).setResultClass(HashMap.class);
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 200);
Map<String, Object> result = httpClient.getResultObject();
Assert.assertEquals(result.get("prop1"), "value1");
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例9: test_jsonErrorResp
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_jsonErrorResp() {
MappingBuilder mockReq = post(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse().withStatus(500).withBody("{\"success\": false}");
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.post(url);
httpClient.setErrorReader(new JsonReader()).setErrorClass(HashMap.class);
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 500);
Assert.assertNull(httpClient.getResultObject());
Map<String, Object> result = httpClient.getErrorObject();
Assert.assertEquals(result.get("success"), Boolean.FALSE);
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例10: test_errorResp
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_errorResp() {
MappingBuilder mockReq = post(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse().withStatus(500).withBody("Error");
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.post(url);
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 500);
Assert.assertEquals(httpClient.getErrorObject(), "Error");
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例11: test_formBody
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_formBody() {
MappingBuilder mockReq = post(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse();
mockReq.withRequestBody(containing("p1=v1"));
mockReq.withRequestBody(containing("p2=v+2"));
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.post(url);
httpClient.setRequestBody(new FormBody().param("p1", "v1").param("p2", "v 2"));
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 200);
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例12: test_jsonBody
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_jsonBody() {
MappingBuilder mockReq = post(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse();
mockReq.withRequestBody(equalTo("{\"id\":1,\"name\":\"user1\"}"));
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.post(url);
httpClient.setRequestBody(new JsonBody(new User(1, "user1"), CharsetUtils.UTF_8));
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 200);
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例13: test_textBody
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test_textBody() {
MappingBuilder mockReq = post(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse();
mockReq.withRequestBody(equalTo("textBody"));
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.post(url);
httpClient.setRequestBody(new TextBody("textBody"));
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 200);
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例14: test
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Test
public void test() {
MappingBuilder mockReq = head(urlPathMatching("/action"));
ResponseDefinitionBuilder mockResp = aResponse().withStatus(200).withHeader("header1", "value1");
httpMock.stubFor(mockReq.willReturn(mockResp));
HttpClient httpClient = null;
try {
String url = baseUrl();
httpClient = HttpClient.head(url);
httpClient.execute();
Assert.assertEquals(httpClient.getResponseCode(), 200);
Assert.assertEquals(httpClient.getResponseHeader("header1"), "value1");
Assert.assertNull(httpClient.getResultObject());
} catch (IOException ex) {
Assert.fail(ex.getMessage());
}
}
示例15: transform
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder; //导入依赖的package包/类
@Override
public ResponseDefinition transform(Request request,
ResponseDefinition responseDefinition,
FileSource files,
Parameters parameters) {
ResponseDefinitionBuilder builder = new ResponseDefinitionBuilder().like(responseDefinition);
String templatePath = responseDefinition.getBodyFileName();
if (templatePath != null && templatePath.contains(".template")) {
try {
Map scope = new ResponseScope(session, new HashMap(this.config), RequestReader.read(request));
responseWriter = responseWriter == null ? new ResponseWriter(files.getPath()) : responseWriter;
new BeforeBlock().run(responseDefinition, scope);
responseWriter.writeTo(builder, scope, templatePath);
} catch (Exception e) {
String errorMessage = "************* Jeyson Error *******************" + System.getProperty("line.separator");
errorMessage += e.getMessage() + System.getProperty("line.separator");
System.err.println(errorMessage);
e.printStackTrace();
}
}
return builder.build();
}