-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance reliability of how we make requests and parse errors (#108)
- Loading branch information
Showing
9 changed files
with
127 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package management | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewError(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
givenResponse http.Response | ||
expectedError managementError | ||
}{ | ||
{ | ||
name: "it fails to decode if body is not a json", | ||
givenResponse: http.Response{ | ||
StatusCode: http.StatusForbidden, | ||
Body: io.NopCloser(strings.NewReader("Hello, I'm not a JSON.")), | ||
}, | ||
expectedError: managementError{ | ||
StatusCode: 403, | ||
Err: "Forbidden", | ||
Message: "failed to decode json error response payload: invalid character 'H' looking for beginning of value", | ||
}, | ||
}, | ||
{ | ||
name: "it correctly decodes the error response payload", | ||
givenResponse: http.Response{ | ||
StatusCode: http.StatusBadRequest, | ||
Body: io.NopCloser(strings.NewReader(`{"statusCode":400,"error":"Bad Request","message":"One of 'client_id' or 'name' is required."}`)), | ||
}, | ||
expectedError: managementError{ | ||
StatusCode: 400, | ||
Err: "Bad Request", | ||
Message: "One of 'client_id' or 'name' is required.", | ||
}, | ||
}, | ||
{ | ||
name: "it will still post the correct status code if the body doesn't have the correct structure", | ||
givenResponse: http.Response{ | ||
StatusCode: http.StatusInternalServerError, | ||
Body: io.NopCloser(strings.NewReader(`{"errorMessage":"wrongStruct"}`)), | ||
}, | ||
expectedError: managementError{ | ||
StatusCode: 500, | ||
Err: "Internal Server Error", | ||
Message: "", | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
actualError := newError(&testCase.givenResponse) | ||
assert.Equal(t, &testCase.expectedError, actualError) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters