mirror of https://github.com/dexidp/dex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1019 B
52 lines
1019 B
package http |
|
|
|
import ( |
|
"io/ioutil" |
|
"net/http" |
|
"net/http/httptest" |
|
) |
|
|
|
type Client interface { |
|
Do(*http.Request) (*http.Response, error) |
|
} |
|
|
|
type HandlerClient struct { |
|
Handler http.Handler |
|
} |
|
|
|
func (hc *HandlerClient) Do(r *http.Request) (*http.Response, error) { |
|
w := httptest.NewRecorder() |
|
hc.Handler.ServeHTTP(w, r) |
|
|
|
resp := http.Response{ |
|
StatusCode: w.Code, |
|
Header: w.Header(), |
|
Body: ioutil.NopCloser(w.Body), |
|
} |
|
|
|
return &resp, nil |
|
} |
|
|
|
type RequestRecorder struct { |
|
Response *http.Response |
|
Error error |
|
|
|
Request *http.Request |
|
} |
|
|
|
func (rr *RequestRecorder) Do(req *http.Request) (*http.Response, error) { |
|
rr.Request = req |
|
|
|
if rr.Response == nil && rr.Error == nil { |
|
panic("RequestRecorder Response and Error cannot both be nil") |
|
} |
|
if rr.Response != nil && rr.Error != nil { |
|
panic("RequestRecorder Response and Error cannot both be non-nil") |
|
} |
|
|
|
return rr.Response, rr.Error |
|
} |
|
|
|
func (rr *RequestRecorder) RoundTrip(req *http.Request) (*http.Response, error) { |
|
return rr.Do(req) |
|
}
|
|
|