Skip to content

Commit

Permalink
test: merge test in adaptor_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed Sep 18, 2022
1 parent 31fdc79 commit 9c6764e
Showing 1 changed file with 8 additions and 35 deletions.
43 changes: 8 additions & 35 deletions fasthttpadaptor/adaptor_test.go
@@ -1,7 +1,6 @@
package fasthttpadaptor

import (
"fmt"
"io"
"net"
"net/http"
Expand All @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
fasthttpH := NewFastHTTPHandler(http.HandlerFunc(nethttpH))
fasthttpH = setContextValueMiddleware(fasthttpH, expectedContextKey, expectedContextValue)
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

0 comments on commit 9c6764e

Please sign in to comment.