From a5ab1fea295661b9ce98a95b7b991125eb4302a7 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 29 Nov 2022 10:08:18 -0800 Subject: [PATCH] httptest --- lib/imgbundler/imgbundler_test.go | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/lib/imgbundler/imgbundler_test.go b/lib/imgbundler/imgbundler_test.go index 2c20784b9..fe5754ee9 100644 --- a/lib/imgbundler/imgbundler_test.go +++ b/lib/imgbundler/imgbundler_test.go @@ -1,11 +1,10 @@ package imgbundler import ( - "bytes" _ "embed" "fmt" - "io/ioutil" "net/http" + "net/http/httptest" "os" "path/filepath" "strings" @@ -20,9 +19,9 @@ import ( //go:embed test_png.png var testPNGFile []byte -type RoundTripFunc func(req *http.Request) *http.Response +type roundTripFunc func(req *http.Request) *http.Response -func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { +func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req), nil } @@ -77,25 +76,18 @@ width="328" height="587" viewBox="-100 -131 328 587"> `, svgURL, pngURL) - transport = RoundTripFunc(func(req *http.Request) *http.Response { - if req.URL.String() != svgURL && req.URL.String() != pngURL { - t.Fatal(req.URL.String()) - } - var body string + transport = roundTripFunc(func(req *http.Request) *http.Response { + respRecorder := httptest.NewRecorder() switch req.URL.String() { case svgURL: - body = `\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n` + respRecorder.WriteString(`\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n`) case pngURL: - body = string(testPNGFile) + respRecorder.Write(testPNGFile) default: t.Fatal(req.URL) } - return &http.Response{ - StatusCode: 200, - Body: ioutil.NopCloser(bytes.NewBufferString(body)), - ContentLength: int64(len(body)), - Header: make(http.Header), - } + respRecorder.WriteHeader(200) + return respRecorder.Result() }) ms := &xmain.State{