Merge pull request #1382 from cli/checkscopes-crash

Fix printing network error in case for failed HTTP requests
This commit is contained in:
Mislav Marohnić 2020-07-17 19:09:29 +02:00 committed by GitHub
commit 73889e0c84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -104,11 +104,15 @@ func CheckScopes(wantedScope string, cb func(string) error) ClientOption {
return func(tr http.RoundTripper) http.RoundTripper {
return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
res, err := tr.RoundTrip(req)
_, hasHeader := res.Header[httpOAuthAppID]
if err != nil || res.StatusCode > 299 || !hasHeader || issuedScopesWarning {
if err != nil || res.StatusCode > 299 || issuedScopesWarning {
return res, err
}
_, hasHeader := res.Header[httpOAuthAppID]
if !hasHeader {
return res, nil
}
appID := res.Header.Get(httpOAuthAppID)
hasScopes := strings.Split(res.Header.Get(httpOAuthScopes), ",")

View file

@ -102,6 +102,7 @@ func Test_CheckScopes(t *testing.T) {
wantScope string
responseApp string
responseScopes string
responseError error
expectCallback bool
}{
{
@ -132,11 +133,22 @@ func Test_CheckScopes(t *testing.T) {
responseScopes: "",
expectCallback: false,
},
{
name: "errored response",
wantScope: "read:org",
responseApp: "",
responseScopes: "",
responseError: errors.New("Network Failed"),
expectCallback: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &httpmock.Registry{}
tr.Register(httpmock.MatchAny, func(*http.Request) (*http.Response, error) {
if tt.responseError != nil {
return nil, tt.responseError
}
if tt.responseScopes == "" {
return &http.Response{StatusCode: 200}, nil
}
@ -165,7 +177,7 @@ func Test_CheckScopes(t *testing.T) {
issuedScopesWarning = false
_, err = rt.RoundTrip(req)
if err != nil {
if err != nil && !errors.Is(err, tt.responseError) {
t.Fatalf("unexpected error: %v", err)
}