migueleliasweb / go-github-mock

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

mock.GetRateLimits returns nothing #16

Closed kalgurn closed 2 years ago

kalgurn commented 2 years ago

I am trying to mock the request which should return an amount of remaining requests

mockedHTTPClient := mock.NewMockedHTTPClient(
    mock.WithRequestMatch(
        mock.GetRateLimit,
        github.RateLimits{
            Core: &github.Rate{
                Limit:     *github.Int(1),
                Remaining: 1,
                Reset:     github.Timestamp{},
            },
            Search: &github.Rate{},
        },
    ),
)
c := github.NewClient(mockedHTTPClient)
ctx := context.Background()

limits, response, err := c.RateLimits(ctx)

if err != nil {
    log.Fatal(err)
}
fmt.Println(response, limits, err)

For some reason my output looks like this

github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}} <nil> <nil>

I was trying to return just a github.Rate, github.RateLimits. I've swapped values with github.Int. Still nothing. Use of WithRequestMatchHandler also did nothing. Am I missing something? Other requests I was trying to mock are working as expected and returns proper values.

migueleliasweb commented 2 years ago

Hi @kalgurn , see if this example works for you. I just had to adapt a couple of things:

mockedHTTPClient := NewMockedHTTPClient(
    WithRequestMatch(
        GetRateLimit,
        struct {
            Resources *github.RateLimits
        }{
            Resources: &github.RateLimits{
                Core: &github.Rate{
                    Limit:     *github.Int(1),
                    Remaining: 10,
                    Reset:     github.Timestamp{},
                },
                Search: &github.Rate{},
            },
        },
    ),
)
c := github.NewClient(mockedHTTPClient)
ctx := context.Background()

limits, response, err := c.RateLimits(ctx)

if err != nil {
    log.Fatal(err)
}
fmt.Println(response, limits, err)
migueleliasweb commented 2 years ago

For some reason, the upstream response for the rate limits endpoints doesn't seem to match what is defined in the OpenAPI definition for the API itself. Even go-github seems to hack a struct when reading the response from the API. See: https://github.com/google/go-github/blob/master/github/github.go#L1107

kalgurn commented 2 years ago

It actually does, thank you @migueleliasweb ! I did noticed this line before, but didn't give a much thought to it :( The response is a bit strange,

github.Response: github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}
github.RateLimits: github.RateLimits{Core:github.Rate{Limit:1, Remaining:10, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, Search:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}} 
err: <nil>

but I don't actually care about the github.Response, only about github.RateLimits which are now working. I think this issue can be closed