- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好 Stack Overflow 社区!
我正在尝试在一些 Java 集成测试中模拟微服务。
为此,我使用 MockServer 5.5.1 版。为了完成其余请求,我使用的是 OkHttp 版本 3.13.1
java中的代码:
final SomeDTO requestObject = new SomeDTO(someParams);
final String jsonObject = objectMapper.writeValueAsString(requestObject);
final MediaType MEDIA_TYPE_JSON = MediaType.get("application/json; charset=utf-8");
final RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, jsonObject);
final Request request = new Request.Builder().url("serverUrl").post(requestBody).build();
final Response response = client.newCall(request).execute();
final String responseJson = response.body().string();
final ResultDTO result = objectMapper.readValue(responseJson, ResultDTO.class);
这很好用。但是,当我将 MockServer 附加到匹配器时:
final MockServerClient client = new MockServerClient("127.0.0.1", 1080);
client.when(request().withMethod("POST") //
.withPath("serverUrl") //
.withBody(json(correctJsonString, MatchType.ONLY_MATCHING_FIELDS))) //
.respond(response().withStatusCode(200) //
.withHeaders(new Header("Content-Type", "application/json; charset=utf-8"),
new Header("Cache-Control", "public, max-age=86400"))
.withBody(responseJson));
我得到一个request didn't match expectation because: body didn't match
, body之间的区别是:
要求:
"body" : {
"type" : "STRING",
"string" : "{\"id\":33611,\"prop1\":28,\"prop2\":\"value2\",\"graph\":[...]}",
"contentType" : "text/plain; charset=utf-8"
}
请求应该匹配:
"body" : {
"type" : "JSON",
"json" : "{\"prop2\":\"value2\",\"prop1\":28,\"graph\":[...]}"
}
所以我的问题:
"JSON"
<-> "STRING"
类型,正文不匹配是否正确?最佳答案
这是一个 JSON 演示:
@Test
public void testRemote3() {
String host = "localhost";
int port = 1080;
String url = String.format("http://%s:%d",host,port);
MockServerClient mockServerClient = new MockServerClient(host,port);
mockServerClient.when(
request()
.withMethod("POST")
.withPath("/order/completed/notify")
.withBody(new JsonBody("{\"username\":\"foo1\", \"age\": 13}", Charset.forName("UTF-8"),MatchType.STRICT))
).respond(
response().withStatusCode(200)
.withCookie("sessionId", "2By8LOhBmaW5nZXJwcmludCIlMDAzMW")
.withHeader("Location", "https://www.mock-server.com")
.withBody("{\"username\":\"wang\", \"status\": 1}")
);
mockServerClient.when(
request()
.withMethod("POST")
.withPath("/order/completed/notify")
.withBody(new JsonBody("{\"username\":\"zhao\", \"age\": 3}", Charset.forName("UTF-8"),MatchType.STRICT))
).respond(
response().withStatusCode(200)
.withCookie("sessionId", "2By8LOhBmaW5nZXJwcmludCIlMDAzMW")
.withHeader("Location", "https://www.mock-server.com")
.withBody("{\"username\":\"wang\", \"status\": true}")
);
log.info("----------------->calling ");
Map<String,Object> userInfo = new HashMap<>();
userInfo.put("age",13);
userInfo.put("username","foo1");
String result = OkHttpUtils.postJson(url+"/order/completed/notify",userInfo);
log.info(result);
Map<String,Object> fool = new HashMap<>();
fool.put("age",3);
fool.put("username","zhao");
result = OkHttpUtils.postJson(url+"/order/completed/notify",fool);
log.info(result);
}
关于json - MockServer 和 OkHttp : body type missmatch (json vs string),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54946479/
当我打电话 instanceID.getToken(default_senderId, scope, null) 这个函数我看到了IOException Missmatched messenger n
看看这个例子: #include int main() { int i= 16777217; float f = 16777216.0; float g = i; if( i ==
现在任何文档都没有描述此错误消息,我在这里发布此帖子。 我目前正在从头开始实现 Bittorrent 客户端C. 我与跟踪服务器通信时遇到问题。它向我发送消息“ackeConnection ID 不匹
h2o 之前在我的笔记本电脑上工作,但我有一段时间没有使用它(同时安装了新的软件包并更新了一些东西)。昨天我尝试使用它,但没有用。我删除了 R h2o 打包并从头开始重新安装 h2o install.
你好 Stack Overflow 社区! 我正在尝试在一些 Java 集成测试中模拟微服务。 为此,我使用 MockServer 5.5.1 版。为了完成其余请求,我使用的是 OkHttp 版本 3
我正在尝试实现 Bittorrent 客户端。但我很难与跟踪服务器进行通信。它不会发送 Bencoded 字典作为对我的请求的响应。 它始终发送以下字符串作为其响应。 "\x03\0\0\0r.opC
当我尝试进行以下 API 调用时: https://skillbasedmiddleware.azurewebsites.net/tables/useritem?$filter=(email eq '
我是一名优秀的程序员,十分优秀!