小编gau*_*tam的帖子

如何在go中测试http调用

我有以下代码:

// HTTPPost to post json messages to the specified url
func HTTPPost(message interface{}, url string) (*http.Response, error) {
    jsonValue, err := json.Marshal(message)
    if err != nil {
        logger.Error("Cannot Convert to JSON: ", err)
        return nil, err
    }
    logger.Info("Calling http post with url: ", url)
    resp, err := getClient().Post(url, "application/json", bytes.NewBuffer(jsonValue))
    if err != nil {
        logger.Error("Cannot post to the url: ", url, err)
        return nil, err
    }
    err = IsErrorResp(resp, url)
    return resp, err
}
Run Code Online (Sandbox Code Playgroud)

我想为此编写测试,但我不确定如何使用 httptest 包。

unit-testing go

4
推荐指数
1
解决办法
3677
查看次数

标签 统计

go ×1

unit-testing ×1