Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP status code response helpers #403

Merged
merged 1 commit into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions autorest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ type Response struct {
*http.Response `json:"-"`
}

// IsHTTPStatus returns true if the returned HTTP status code matches the provided status code.
// If there was no response (i.e. the underlying http.Response is nil) the return value is false.
func (r Response) IsHTTPStatus(statusCode int) bool {
if r.Response == nil {
return false
}
return r.Response.StatusCode == statusCode
}

// HasHTTPStatus returns true if the returned HTTP status code matches one of the provided status codes.
// If there was no response (i.e. the underlying http.Response is nil) or not status codes are provided
// the return value is false.
func (r Response) HasHTTPStatus(statusCodes ...int) bool {
return ResponseHasStatusCode(r.Response, statusCodes...)
}

// LoggingInspector implements request and response inspectors that log the full request and
// response to a supplied log.
type LoggingInspector struct {
Expand Down
31 changes: 31 additions & 0 deletions autorest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,37 @@ func TestCookies(t *testing.T) {
}
}

func TestResponseIsHTTPStatus(t *testing.T) {
r := Response{}
if r.IsHTTPStatus(http.StatusBadRequest) {
t.Fatal("autorest: expected false for nil response")
}
r.Response = &http.Response{StatusCode: http.StatusOK}
if r.IsHTTPStatus(http.StatusBadRequest) {
t.Fatal("autorest: expected false")
}
if !r.IsHTTPStatus(http.StatusOK) {
t.Fatal("autorest: expected true")
}
}

func TestResponseHasHTTPStatus(t *testing.T) {
r := Response{}
if r.HasHTTPStatus(http.StatusBadRequest, http.StatusInternalServerError) {
t.Fatal("autorest: expected false for nil response")
}
r.Response = &http.Response{StatusCode: http.StatusAccepted}
if r.HasHTTPStatus(http.StatusBadRequest, http.StatusInternalServerError) {
t.Fatal("autorest: expected false")
}
if !r.HasHTTPStatus(http.StatusOK, http.StatusCreated, http.StatusAccepted) {
t.Fatal("autorest: expected true")
}
if r.HasHTTPStatus() {
t.Fatal("autorest: expected false for no status codes")
}
}

func randomString(n int) string {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
Expand Down