Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add robust tests cases for middleware.NewWrapResponseWriter() #835

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
205 changes: 205 additions & 0 deletions middleware/wrap_writer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package middleware

import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

Expand All @@ -22,3 +28,202 @@ func TestHttp2FancyWriterRemembersWroteHeaderWhenFlushed(t *testing.T) {
t.Fatal("want Flush to have set wroteHeader=true")
}
}

func TestWrapResponseWriter(t *testing.T) {
tt := []struct {
w http.ResponseWriter
protoMajor int
fl bool // http.Flusher
hj bool // http.Hijacker
rf bool // io.ReaderFrom
ps bool // http.Pusher
}{
{protoMajor: 1, w: none{}},
{protoMajor: 1, w: flusher{}, fl: true},
{protoMajor: 1, w: hijacker{}, hj: true},
{protoMajor: 1, w: readerFrom{}, rf: true},
{protoMajor: 1, w: flusherHijacker{}, fl: true, hj: true},
{protoMajor: 1, w: flusherReaderFrom{}, fl: true, rf: true},
{protoMajor: 1, w: hijackerReaderFrom{}, hj: true, rf: true},
{protoMajor: 1, w: flusherHijackerReaderFrom{}, fl: true, hj: true, rf: true},

{protoMajor: 2, w: none{}},
{protoMajor: 2, w: flusher{}, fl: true},
{protoMajor: 2, w: hijacker{}, hj: true},
{protoMajor: 2, w: readerFrom{}, rf: true},
{protoMajor: 2, w: pusher{}, ps: true},
{protoMajor: 2, w: flusherPusher{}, fl: true, ps: true},
{protoMajor: 2, w: readerFromPusher{}, rf: true, ps: true},
{protoMajor: 2, w: hijackerPusher{}, hj: true, ps: true},
{protoMajor: 2, w: flusherHijacker{}, fl: true, hj: true},
{protoMajor: 2, w: flusherReaderFrom{}, fl: true, rf: true},
{protoMajor: 2, w: hijackerReaderFrom{}, hj: true, rf: true},
{protoMajor: 2, w: flusherHijackerReaderFrom{}, fl: true, hj: true, rf: true},
{protoMajor: 2, w: flusherHijackerPusher{}, fl: true, hj: true, ps: true},
{protoMajor: 2, w: flusherReaderFromPusher{}, fl: true, rf: true, ps: true},
{protoMajor: 2, w: hijackerReaderFromPusher{}, hj: true, rf: true, ps: true},
{protoMajor: 2, w: flusherHijackerReaderFromPusher{}, fl: true, hj: true, rf: true, ps: true},
Comment on lines +41 to +65
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=== RUN   TestWrapResponseWriter
    wrap_writer_test.go:110: *middleware.basicWriter(middleware.readerFrom).(io.ReaderFrom) is false
    wrap_writer_test.go:110: *middleware.flushWriter(middleware.flusherReaderFrom).(io.ReaderFrom) is false
    wrap_writer_test.go:110: *middleware.hijackWriter(middleware.hijackerReaderFrom).(io.ReaderFrom) is false
    wrap_writer_test.go:105: *middleware.basicWriter(middleware.hijacker).(http.Hijacker) is false
    wrap_writer_test.go:110: *middleware.basicWriter(middleware.readerFrom).(io.ReaderFrom) is false
    wrap_writer_test.go:115: *middleware.basicWriter(middleware.pusher).(http.Pusher) is false
    wrap_writer_test.go:110: *middleware.basicWriter(middleware.readerFromPusher).(io.ReaderFrom) is false
    wrap_writer_test.go:115: *middleware.basicWriter(middleware.readerFromPusher).(http.Pusher) is false
    wrap_writer_test.go:105: *middleware.basicWriter(middleware.hijackerPusher).(http.Hijacker) is false
    wrap_writer_test.go:115: *middleware.basicWriter(middleware.hijackerPusher).(http.Pusher) is false
    wrap_writer_test.go:105: *middleware.flushWriter(middleware.flusherHijacker).(http.Hijacker) is false
    wrap_writer_test.go:110: *middleware.flushWriter(middleware.flusherReaderFrom).(io.ReaderFrom) is false
    wrap_writer_test.go:105: *middleware.basicWriter(middleware.hijackerReaderFrom).(http.Hijacker) is false
    wrap_writer_test.go:110: *middleware.basicWriter(middleware.hijackerReaderFrom).(io.ReaderFrom) is false
    wrap_writer_test.go:105: *middleware.flushWriter(middleware.flusherHijackerReaderFrom).(http.Hijacker) is false
    wrap_writer_test.go:110: *middleware.flushWriter(middleware.flusherHijackerReaderFrom).(io.ReaderFrom) is false
    wrap_writer_test.go:105: *middleware.http2FancyWriter(middleware.flusherHijackerPusher).(http.Hijacker) is false
    wrap_writer_test.go:110: *middleware.http2FancyWriter(middleware.flusherReaderFromPusher).(io.ReaderFrom) is false
    wrap_writer_test.go:105: *middleware.basicWriter(middleware.hijackerReaderFromPusher).(http.Hijacker) is false
    wrap_writer_test.go:110: *middleware.basicWriter(middleware.hijackerReaderFromPusher).(io.ReaderFrom) is false
    wrap_writer_test.go:115: *middleware.basicWriter(middleware.hijackerReaderFromPusher).(http.Pusher) is false
    wrap_writer_test.go:105: *middleware.http2FancyWriter(middleware.flusherHijackerReaderFromPusher).(http.Hijacker) is false
    wrap_writer_test.go:110: *middleware.http2FancyWriter(middleware.flusherHijackerReaderFromPusher).(io.ReaderFrom) is false
    wrap_writer_test.go:121: 73/96 test cases passed

We currently don't handle couple of combinations.. Do we care to fix them?

}

// Double check the correctness of the test cases.
duplicates := map[interface{}]struct{}{}
for _, tc := range tt {
if fl := strings.Contains(strings.ToLower(fmt.Sprintf("%T", tc.w)), "flusher"); fl != tc.fl {
t.Fatalf("test case is wrong, %T{} should have fl: %v", tc.w, fl)
}
if hj := strings.Contains(strings.ToLower(fmt.Sprintf("%T", tc.w)), "hijacker"); hj != tc.hj {
t.Fatalf("test case is wrong, %T{} should have hj: %v", tc.w, hj)
}
if rf := strings.Contains(strings.ToLower(fmt.Sprintf("%T", tc.w)), "readerfrom"); rf != tc.rf {
t.Fatalf("test case is wrong, %T{} should have rf: %v", tc.w, rf)
}
if ps := strings.Contains(strings.ToLower(fmt.Sprintf("%T", tc.w)), "pusher"); ps != tc.ps {
t.Fatalf("test case is wrong, %T{} should have ps: %v", tc.w, ps)
}

if _, ok := duplicates[tc]; ok {
t.Fatalf("test case already exists: %+v", tc)
}
duplicates[tc] = struct{}{}
}

var countErrs int

for _, tc := range tt {
w := NewWrapResponseWriter(tc.w, tc.protoMajor)
_, fl := w.(http.Flusher)
_, hj := w.(http.Hijacker)
_, rf := w.(io.ReaderFrom)
_, ps := w.(http.Pusher)

if fl != tc.fl {
t.Errorf("%T(%T).(http.Flusher) is %v", w, tc.w, fl)
countErrs++
}

if hj != tc.hj {
t.Errorf("%T(%T).(http.Hijacker) is %v", w, tc.w, hj)
countErrs++
}

if rf != tc.rf {
t.Errorf("%T(%T).(io.ReaderFrom) is %v", w, tc.w, rf)
countErrs++
}

if ps != tc.ps {
t.Errorf("%T(%T).(http.Pusher) is %v", w, tc.w, ps)
countErrs++
}
}

if countErrs != 0 {
t.Errorf("%v/%v test cases passed", len(tt)*4-countErrs, len(tt)*4)
}
}

type none struct {
http.ResponseWriter
}

type flusher struct {
http.ResponseWriter
}

func (flusher) Flush() {}

type hijacker struct {
http.ResponseWriter
}

func (hijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return nil, nil, nil
}

type readerFrom struct {
http.ResponseWriter
}

func (readerFrom) ReadFrom(r io.Reader) (int64, error) {
return 0, nil
}

type pusher struct {
http.ResponseWriter
}

func (pusher) Push(target string, opts *http.PushOptions) error {
return nil
}

type flusherHijacker struct {
http.ResponseWriter
flusher
hijacker
}

type flusherReaderFrom struct {
http.ResponseWriter
flusher
readerFrom
}

type hijackerReaderFrom struct {
http.ResponseWriter
hijacker
readerFrom
}

type flusherHijackerReaderFrom struct {
http.ResponseWriter
flusher
hijacker
readerFrom
}

type flusherPusher struct {
http.ResponseWriter
flusher
pusher
}

type hijackerPusher struct {
http.ResponseWriter
hijacker
pusher
}

type readerFromPusher struct {
http.ResponseWriter
readerFrom
pusher
}

type flusherHijackerPusher struct {
http.ResponseWriter
flusher
hijacker
pusher
}

type flusherReaderFromPusher struct {
http.ResponseWriter
flusher
readerFrom
pusher
}

type hijackerReaderFromPusher struct {
http.ResponseWriter
hijacker
readerFrom
pusher
}

type flusherHijackerReaderFromPusher struct {
http.ResponseWriter
flusher
hijacker
readerFrom
pusher
}