当我尝试对包含github.com/satori/go.uuid中的uuid.UUID的结构使用go-vcr时,我没有得到任何值。使用其他没有uuid的结构进行测试。UUID类型按预期工作。
编辑:go-vcr是一个go模块,用于记录要播放的HTTP交互:https://github.com/dnaeon/go-vcr
使用Ginkgo和Gomega进行测试(为简洁起见,省略了测试代码)
Go版本: go版本go1.12 darwin / amd64
type Test2 struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
func (s *UserService) Test2() *Test2 {
url := fmt.Sprintf("%v:%v/test", s.ServiceHost, s.ServicePort)
var client = &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", s.ServiceToken))
resp, err := client.Do(req)
if err != nil {
return nil
}
defer resp.Body.Close()
var t Test2
json.NewDecoder(resp.Body).Decode(&t)
return &t
}
修复:
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
Authorization:
- Bearer 123abc
Content-Type:
- application/json
url: http://localhost:8080/test2
method: GET
response:
body: |
{"id":"5600966e-4ad6-4f79-8e01-8db13ba5c212","name":"Foo","description":"Bar"}
headers:
Content-Length:
- "79"
Content-Type:
- application/json
Date:
- Fri, 19 Apr 2019 18:49:03 GMT
status: 200 OK
code: 200
duration: ""
测试代码:
us := UserService{
ServiceHost: "http://localhost",
ServicePort: 8080,
ServiceToken: "123abc",
}
url = fmt.Sprintf("%v:%v/test", us.ServiceHost, us.ServicePort)
req, err = http.NewRequest("GET", url, nil)
Expect(err).To(BeNil())
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", us.ServiceToken))
resp, err = client.Do(req)
Expect(err).To(BeNil())
defer resp.Body.Close()
t := us.Test()
Expect(t.Description).To(Equal("Bar"))
r, err = recorder.New("fixtures/test2")
Expect(err).To(BeNil())
defer r.Stop()
client = &http.Client{
Transport: r,
}
url = fmt.Sprintf("%v:%v/test2", us.ServiceHost, us.ServicePort)
req, err = http.NewRequest("GET", url, nil)
Expect(err).To(BeNil())
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", us.ServiceToken))
resp, err = client.Do(req)
Expect(err).To(BeNil())
defer resp.Body.Close()
t2 := us.Test2()
iDString := "5600966e-4ad6-4f79-8e01-8db13ba5c212"
id, err := uuid.FromString(iDString)
Expect(err).To(BeNil())
Expect(t2.ID).To(Equal(id))
Fails with
�[91mExpected
<uuid.UUID>: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
to equal
<uuid.UUID>: [86, 0, 150, 110, 74, 214, 79, 121, 142, 1, 141, 177, 59, 165, 194,
编辑:仍然对为什么它不起作用感兴趣,但是最终使用了另一个没有这个问题的HTTP Record播放库https://github.com/ad2games/vcr-go