Skip to content

Commit

Permalink
Fix default error formatting for mocked responses
Browse files Browse the repository at this point in the history
  • Loading branch information
migueleliasweb committed Jan 26, 2023
1 parent da8a4af commit 26249d1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.16
require (
github.com/buger/jsonparser v1.1.1
github.com/go-kit/log v0.2.0
github.com/google/go-github/v41 v41.0.0
github.com/google/go-github/v50 v50.0.0
github.com/gorilla/mux v1.8.0
)
12 changes: 8 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ github.com/go-kit/log v0.2.0/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBj
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-github/v41 v41.0.0 h1:HseJrM2JFf2vfiZJ8anY2hqBjdfY1Vlj/K27ueww4gg=
github.com/google/go-github/v41 v41.0.0/go.mod h1:XgmCA5H323A9rtgExdTcnDkcqp6S30AVACCBDOonIxg=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v50 v50.0.0 h1:gdO1AeuSZZK4iYWwVbjni7zg8PIQhp7QfmPunr016Jk=
github.com/google/go-github/v50 v50.0.0/go.mod h1:Ev4Tre8QoKiolvbpOSG3FIi4Mlon3S2Nt9W5JYqKiwA=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
Expand All @@ -19,7 +20,9 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -30,4 +33,5 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
2 changes: 1 addition & 1 deletion src/mock/endpointpattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"testing"

"github.com/google/go-github/v41/github"
"github.com/google/go-github/v50/github"
)

func TestRepoGetContents(t *testing.T) {
Expand Down
50 changes: 48 additions & 2 deletions src/mock/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"testing"

"github.com/google/go-github/v41/github"
"github.com/google/go-github/v50/github"
)

func TestNewMockedHTTPClient(t *testing.T) {
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestNewMockedHTTPClient(t *testing.T) {
}
}

func TestMockErrors(t *testing.T) {
func TestMockErrorSimple(t *testing.T) {
mockedHTTPClient := NewMockedHTTPClient(
WithRequestMatchHandler(
GetUsersByUsername,
Expand Down Expand Up @@ -123,6 +123,52 @@ func TestMockErrors(t *testing.T) {
if ghErr.Message != "github went belly up or something" {
t.Errorf("user err is %s, want 'github went belly up or something'", userErr.Error())
}

}

func TestMockErrorToError(t *testing.T) {
mockedHTTPClient := NewMockedHTTPClient(
WithRequestMatchHandler(
GetUsersByUsername,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
WriteError(
w,
http.StatusInternalServerError,
"github went belly up or something",
)
}),
),
)
c := github.NewClient(mockedHTTPClient)

ctx := context.Background()

user, _, userErr := c.Users.Get(ctx, "someUser")

if userV := reflect.ValueOf(user); !userV.IsZero() {
t.Errorf("user is %s, want nil", user)
}

if userErr == nil {
t.Fatal("user err is nil, want *github.ErrorResponse")
}

ghErr, ok := userErr.(*github.ErrorResponse)

if !ok {
t.Fatal("couldn't cast userErr to *github.ErrorResponse")
}

if ghErr.Message != "github went belly up or something" {
t.Errorf("user err is %s, want 'github went belly up or something'", userErr.Error())
}

// We should be able to use the default formatting for the error interface.
// This was initially failing due to the `github.ErrorResponse` containing `Response: nil`
// thus making the folling call panic due to `nil pointer`.
if fmtdErr := userErr.Error(); len(fmtdErr) == 0 {
t.Fatal("couldn't use default error formattung for github.ErrorResponse")
}
}

func TestMocksNotConfiguredError(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion src/mock/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/google/go-github/v41/github"
"github.com/google/go-github/v50/github"
)

// MustMarshal helper function that wraps json.Marshal
Expand Down

0 comments on commit 26249d1

Please sign in to comment.