migueleliasweb / go-github-mock

A library to aid unittesting code that uses Golang's Github SDK
MIT License
98 stars 24 forks source link

Support for empty result #5

Closed antonlisovenko closed 3 years ago

antonlisovenko commented 3 years ago

This PR adds support for the cases when empty data should be returned (for example if no releases exists - Github returns http 200 with empty array in this case )

migueleliasweb commented 3 years ago

Hi @antonlisovenko ,

Since this is a specific case for the empty results, you could use mock.WithRequestMatchHandler instead:

E.g.

mock.WithRequestMatchHandler(
  mock.GetUsersByUsername,
  http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
)
antonlisovenko commented 3 years ago

hi @migueleliasweb Yes, this works thanks. I'm still think getting the library panicing on "index out of bounds" if the empty result is returned is not great - but as there's a workaround I'll leave itto you.

Maybe we need to mention somewhere in README that the result cannot be empty or about the workaround with WithRequestMatchHandler

migueleliasweb commented 3 years ago

The trick with the WithRequestMatchHandler option will solve it for now but there might still be a legit use case where you want to return an empty result amongst results with data in them. Like when you have multiple calls to a certain method in the SDK and it might return an empty set depending on a given parameter you passed in one of the calls.

I will have to do some testing to check a few behaviours on the API/SDK and I will come back to you.

migueleliasweb commented 3 years ago

@antonlisovenko turns out, empty results were already supported. I've added an example and a test.

https://github.com/migueleliasweb/go-github-mock/pull/7