英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论