Simple blogging system written in Go https://goblog.app
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.
 
 
 
 
 
 

44 lines
841 B

package main
import (
"net/http"
"net/http/httptest"
)
type fakeHttpClient struct {
httpClient
req *http.Request
res *http.Response
handler http.Handler
}
var _ httpClient = &fakeHttpClient{}
func (c *fakeHttpClient) Do(req *http.Request) (*http.Response, error) {
if c.handler == nil {
return nil, nil
}
rec := httptest.NewRecorder()
c.handler.ServeHTTP(rec, req)
c.req = req
c.res = rec.Result()
return c.res, nil
}
func (c *fakeHttpClient) clean() {
c.req = nil
c.res = nil
c.handler = nil
}
func (c *fakeHttpClient) setHandler(handler http.Handler) {
c.clean()
c.handler = handler
}
func (c *fakeHttpClient) setFakeResponse(statusCode int, body string) {
c.setHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(statusCode)
_, _ = rw.Write([]byte(body))
}))
}