GoLang 模拟 CSV REST 响应

huangapple go评论91阅读模式
英文:

GoLang Mocking CSV Rest Response

问题

我正在尝试对返回CSV响应的API进行单元测试。我习惯于测试返回JSON响应的API,做法类似于:

resBody := `{"access_token": "token","instance_url": "url","id": "id","token_type": "Bearer"}`
r := ioutil.NopCloser(bytes.NewReader([]byte(resBody)))

clientMock.GetMock = func(url string) (*http.Response, error) {
    return &http.Response{
        StatusCode: 200,
        Body:       r,
    }, nil
}

在这里,resBody是我模拟的JSON响应,但是我无法确定用于测试返回CSV响应的等效响应结构会是什么。

英文:

I am trying to unit test an API which returns a CSV response. I am used to testing APIs that return Json response by doing something like this :

resBody := `{"access_token": "token","instance_url": "url","id": "id","token_type": "Bearer"}`
r := ioutil.NopCloser(bytes.NewReader([]byte(resBody)))

clientMock.GetMock = func(url string) (*http.Response, error) {
		return &http.Response{
			StatusCode: 200,
			Body:       r,
		}, nil
	}

Here, the resBody is my mocked Json response, however unable to figure out what would be an equivalent response structure for testing APIs that return csv response.

答案1

得分: 1

没问题,我会帮你翻译以下内容:

不用担心,我现在能够解决了。
下面的代码可以用来模拟 CSV 响应:

resBody := `"access_token","instance_url","id","token_type"` + "\n" + `"token","url","id","Bearer"`
r := ioutil.NopCloser(bytes.NewReader([]byte(resBody)))

clientMock.GetMock = func(url string) (*http.Response, error) {
        return &http.Response{
            StatusCode: 200,
            Body:       r,
        }, nil
    }

希望对你有帮助!

英文:

Never mind, able to figure it out now.
Below can be used to mock CSV response :

resBody := `"access_token","instance_url","id","token_type"` + "\n" + `"token","url","id","Bearer"`
r := ioutil.NopCloser(bytes.NewReader([]byte(resBody)))

clientMock.GetMock = func(url string) (*http.Response, error) {
        return &http.Response{
            StatusCode: 200,
            Body:       r,
        }, nil
    }

huangapple
  • 本文由 发表于 2021年5月28日 03:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/67728546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定