如何使用MockWebServer验证POST正文的内容? [英] How can I verify the content of a POST Body with MockWebServer?

查看:88
本文介绍了如何使用MockWebServer验证POST正文的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个使用Squares OkHttp s MockWebServer的单元测试.测试运行正常,一切都很好.到目前为止,我无法做的一件事就是验证POST请求的内容.

I have several unit tests that use Squares OkHttps MockWebServer. The tests run all fine, and everything works very well. The one thing I couldn't do so far is to verify the content of the POST requests.

为什么要这么做?
我正在开发的REST Api有一些方法,要求将数据对象发送到POST请求正文中,其他方法要求将对象作为FormUrlEncoded数据字段发送.因此,我想确保Retrofit接口已正确设置为acc.符合规范.

Why do I want to do that?
The REST Api I'm developing against has some methods, that require the data objects to be sent in the POST requests body, other methods require the objects to be sent as a FormUrlEncoded data field. So I want to ensure, that the Retrofit interface was set up correctly acc. to the spec.

以下单元测试将通过,但第一个单元测试将错误地将数据发送到体内:

The following unit test will pass, but the first one sends the data incorrectly inside the body:

//Incorrect
@POST("api/v1/user/senddata")
Observable<Void> senddata (
        @Path("VIN") String vin,
        @Body PoiWrapper wrappedPoi);

//Correct
@FormUrlEncoded
@POST("api/v1/user/senddata")
Observable<Void> senddata(
        @Path("VIN") String vin,
        @Field("data") PoiWrapper wrappedPoi);

我知道MockWebServer具有takeRequest()方法,但是我一直坚持从该请求正文获取实际字段和数据.

I know the MockWebServer has the takeRequest() method, but I'm stuck with getting the actual field and data from that requests body.

谢谢您的帮助!

推荐答案

尝试遵循

这篇关于如何使用MockWebServer验证POST正文的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆