简体   繁体   English

使用github.com/jarcoal/httpmock验证请求正文

[英]Validate request Body using github.com/jarcoal/httpmock

The function I am trying to test accepts data structs, forms a query_dsl and then makes a /_search call to elastic search with the formed query. 我试图测试的函数接受数据结构,形成一个query_dsl ,然后用形成的查询对弹性搜索进行/_search调用。 Hence I want to assert on the query_dsl and url which gets formed. 因此,我想断言在形成的query_dslurl上。

I am using github.com/jarcoal/httpmock to mock net/http requests in my unit tests. 我正在使用github.com/jarcoal/httpmock在我的单元测试中模拟net/http请求。 As per the doc it exposes func GetCallCountInfo() map[string]int to validate how many times a particular endpoint was hit. 根据文档,它公开func GetCallCountInfo() map[string]int以验证特定端点被击中的次数。 But I am also interested in knowing what was the request body when this call was made. 但是我也有兴趣知道在进行此调用时请求体是什么。

http.Client is not exposed, hence can not override/mock that for testing. http.Client未公开,因此无法覆盖/模拟测试。

If it is not possible using this package then is there any other library which can mock the network request and also gives hold of request body? 如果无法使用此软件包,那么是否有任何其他库可以模拟网络请求并且还提供请求正文?

Following @georgeok Suggestion, We can create a mock http server and capture the request body when request is made. 在@georgeok Suggestion之后,我们可以创建一个模拟http服务器并在请求时捕获请求体。 Following is the code snippet to create a server and store the request body. 以下是用于创建服务器和存储请求正文的代码段。

server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
        actualRequestBody, err = ioutil.ReadAll(req.Body)
        check(err)
        // Send mock response to be tested
        _, err := rw.Write(bytes)
        check(err)
    }))

    defer server.Close()

Now our request body is stored on actualRequestBody variable, and we can assert on this for correctness. 现在我们的请求体存储在actualRequestBody变量中,我们可以断言它的正确性。

The only thing necessary to make sure this works is to make the call at host server.URL . 确保其工作的唯一必要条件是在主机server.URL上进行调用。 As it spins up a server on the address mentioned at server.URL . 因为它在server.URL上提到的地址上旋转服务器。 So if the call from your code is being made to different server this will not catch it. 因此,如果您的代码中的调用是针对不同的服务器进行的,那么这将无法捕获它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在验证请求正文并跳转到 class 时模拟服务有@ControllerAdvice - How to mock a service when validate request body and jump into class have @ControllerAdvice 如何使用Moq使用JSON正文创建模拟HTTP发布请求 - How to create mock HTTP post request with a JSON body using Moq 使用詹金斯获取Github Pull请求的倍数检查 - Getting Multiples checks for Github Pull Request using Jenkins 使用 WebClient 时对 POST 请求正文进行单元测试 - Unit Testing POST request body while using WebClient Angular:使用多个参数 expectOne() 测试 HttpMock 失败 - Angular: Testing HttpMock with multiple parameters expectOne() fails 无法从“加载数据集”<filepath> " 使用 class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader</filepath> - Unable to load dataset from "<filePath>" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader 问题单元使用HttpMock测试ActiveResource模型 - Problem Unit Testing ActiveResource Model with HttpMock 如何使用环形模拟请求以JSON作为正文模拟测试POST请求? - How to mock test POST requests with body as JSON using ring mock request? 将请求发布到服务,获取响应,验证响应 - Post request to a service, get response ,validate response Mocha要求我阅读HTTP Request Body - Mocha Requires me to read HTTP Request Body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM