mirror-to-gitea/vendor/github.com/onsi/gomega/matchers/have_http_status_matcher.go
vendor/github.com/onsi/gomega/matchers/have_http_status_matcher.goRaw
1package matchers
2
3import (
4 "fmt"
5 "net/http"
6 "net/http/httptest"
7
8 "github.com/onsi/gomega/format"
9)
10
11type HaveHTTPStatusMatcher struct {
12 Expected interface{}
13}
14
15func (matcher *HaveHTTPStatusMatcher) Match(actual interface{}) (success bool, err error) {
16 var resp *http.Response
17 switch a := actual.(type) {
18 case *http.Response:
19 resp = a
20 case *httptest.ResponseRecorder:
21 resp = a.Result()
22 default:
23 return false, fmt.Errorf("HaveHTTPStatus matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n%s", format.Object(actual, 1))
24 }
25
26 switch e := matcher.Expected.(type) {
27 case int:
28 return resp.StatusCode == e, nil
29 case string:
30 return resp.Status == e, nil
31 }
32
33 return false, fmt.Errorf("HaveHTTPStatus matcher must be passed an int or a string. Got:\n%s", format.Object(matcher.Expected, 1))
34}
35
36func (matcher *HaveHTTPStatusMatcher) FailureMessage(actual interface{}) (message string) {
37 return format.Message(actual, "to have HTTP status", matcher.Expected)
38}
39
40func (matcher *HaveHTTPStatusMatcher) NegatedFailureMessage(actual interface{}) (message string) {
41 return format.Message(actual, "not to have HTTP status", matcher.Expected)
42}