go-http-peering/test/util_test.go

37 lines
686 B
Go
Raw Normal View History

2019-02-03 20:56:58 +01:00
package test
import (
"crypto/rand"
"crypto/rsa"
"net/http"
2019-02-22 17:35:49 +01:00
"net/http/httptest"
2019-02-03 20:56:58 +01:00
)
func mustGeneratePrivateKey() *rsa.PrivateKey {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(err)
}
return privateKey
}
2019-02-22 17:35:49 +01:00
type HTTPClientMock struct {
handler http.Handler
recorder *httptest.ResponseRecorder
2019-02-03 20:56:58 +01:00
}
2019-02-22 17:35:49 +01:00
func (c *HTTPClientMock) Do(r *http.Request) (*http.Response, error) {
w := httptest.NewRecorder()
c.recorder = w
c.handler.ServeHTTP(w, r)
return w.Result(), nil
2019-02-03 20:56:58 +01:00
}
2019-02-22 17:35:49 +01:00
func (c *HTTPClientMock) Recorder() *httptest.ResponseRecorder {
return c.recorder
}
2019-02-03 20:56:58 +01:00
2019-02-22 17:35:49 +01:00
func NewHTTPClientMock(h http.Handler) *HTTPClientMock {
return &HTTPClientMock{h, nil}
2019-02-03 20:56:58 +01:00
}