diff options
author | kinggo <1963359402@qq.com> | 2022-09-18 15:20:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-18 09:20:03 +0200 |
commit | bcf7e8e94422403a93145c6ae7a8eb2224e0436b (patch) | |
tree | 65e9103667b59438e5ef122c6e8986a8a7647152 /fasthttpadaptor | |
parent | resolve CVE-2022-27664 (#1377) (diff) | |
download | fasthttp-bcf7e8e94422403a93145c6ae7a8eb2224e0436b.tar.gz fasthttp-bcf7e8e94422403a93145c6ae7a8eb2224e0436b.tar.bz2 fasthttp-bcf7e8e94422403a93145c6ae7a8eb2224e0436b.zip |
test: merge test in adaptor_test.go (#1381)
* test: merge test in adaptor_test.go
* Fix lint
Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
Diffstat (limited to 'fasthttpadaptor')
-rw-r--r-- | fasthttpadaptor/adaptor_test.go | 43 |
1 files changed, 8 insertions, 35 deletions
diff --git a/fasthttpadaptor/adaptor_test.go b/fasthttpadaptor/adaptor_test.go index ba23e60..23e2801 100644 --- a/fasthttpadaptor/adaptor_test.go +++ b/fasthttpadaptor/adaptor_test.go @@ -1,7 +1,6 @@ package fasthttpadaptor import ( - "fmt" "io" "net" "net/http" @@ -20,7 +19,7 @@ func TestNewFastHTTPHandler(t *testing.T) { expectedProtoMajor := 1 expectedProtoMinor := 1 expectedRequestURI := "/foo/bar?baz=123" - expectedBody := "body 123 foo bar baz" + expectedBody := "<!doctype html><html>" expectedContentLength := len(expectedBody) expectedHost := "foobar.com" expectedRemoteAddr := "1.2.3.4:6789" @@ -35,6 +34,7 @@ func TestNewFastHTTPHandler(t *testing.T) { } expectedContextKey := "contextKey" expectedContextValue := "contextValue" + expectedContentType := "text/html; charset=utf-8" callsCount := 0 nethttpH := func(w http.ResponseWriter, r *http.Request) { @@ -91,7 +91,7 @@ func TestNewFastHTTPHandler(t *testing.T) { w.Header().Set("Header1", "value1") w.Header().Set("Header2", "value2") w.WriteHeader(http.StatusBadRequest) - fmt.Fprintf(w, "request body is %q", body) + w.Write(body) //nolint:errcheck } fasthttpH := NewFastHTTPHandler(http.HandlerFunc(nethttpH)) fasthttpH = setContextValueMiddleware(fasthttpH, expectedContextKey, expectedContextValue) @@ -129,9 +129,11 @@ func TestNewFastHTTPHandler(t *testing.T) { if string(resp.Header.Peek("Header2")) != "value2" { t.Fatalf("unexpected header value: %q. Expecting %q", resp.Header.Peek("Header2"), "value2") } - expectedResponseBody := fmt.Sprintf("request body is %q", expectedBody) - if string(resp.Body()) != expectedResponseBody { - t.Fatalf("unexpected response body %q. Expecting %q", resp.Body(), expectedResponseBody) + if string(resp.Body()) != expectedBody { + t.Fatalf("unexpected response body %q. Expecting %q", resp.Body(), expectedBody) + } + if string(resp.Header.Peek("Content-Type")) != expectedContentType { + t.Fatalf("unexpected response content-type %q. Expecting %q", string(resp.Header.Peek("Content-Type")), expectedBody) } } @@ -141,32 +143,3 @@ func setContextValueMiddleware(next fasthttp.RequestHandler, key string, value i next(ctx) } } - -func TestContentType(t *testing.T) { - t.Parallel() - - nethttpH := func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("<!doctype html><html>")) //nolint:errcheck - } - fasthttpH := NewFastHTTPHandler(http.HandlerFunc(nethttpH)) - - var ctx fasthttp.RequestCtx - var req fasthttp.Request - - req.SetRequestURI("http://example.com") - - remoteAddr, err := net.ResolveTCPAddr("tcp", "1.2.3.4:80") - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - ctx.Init(&req, remoteAddr, nil) - - fasthttpH(&ctx) - - resp := &ctx.Response - got := string(resp.Header.Peek("Content-Type")) - expected := "text/html; charset=utf-8" - if got != expected { - t.Errorf("expected %q got %q", expected, got) - } -} |