dnaeon / go-vcr

Record and replay your HTTP interactions for fast, deterministic and accurate tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use go-vcr with "github.com/satori/go.uuid" getting back Nil values for uuid

lauraeci opened this issue · comments

When I use go-vcr with structs containing uuid.UUID github.com/satori/go.uuid I get back no values. Testing other structs without uuid.UUID types works as expected. Any advice is appreciated.

Thank you!

Testing with Ginkgo and Gomega (ommitted test code for brevity)

Go version:
go version 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
}

Fixture:

---
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: ""

Test code:

	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, 18]�[0m

Apparently, this repo does not support JSON. Only Yaml. We need this upgraded since most code in GO will try to unmarshall the json from an api response.

Hey @joncodo , @lauraeci ,

Finally managed to get some spare time and work on some long standing issues (I know, this one been really old one :)). One thing that I've noticed in your fixture file is that the body is wrongly formatted. Was that generated by go-vcr? One thing that stands out is that the body is not properly escaped, which makes me thing that this might be the root cause.

Also, I've started working on some refactoring of go-vcr and will be introducing v3 soon. I've added a test case specifically for unmarshalling JSON bodies (and tested it out with UUIDs as well) and things work as expected.

I will close this one out, as I'm not able to reproduce it and will recommend you that you try out v3 of go-vcr.

If you still experience this issue with v3, please re-open this one.

Thanks!