github.com/lingyao2333/mo-zero@v1.4.1/rest/chain/chain_test.go (about) 1 package chain 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "reflect" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 // A constructor for middleware 13 // that writes its own "tag" into the RW and does nothing else. 14 // Useful in checking if a chain is behaving in the right order. 15 func tagMiddleware(tag string) Middleware { 16 return func(h http.Handler) http.Handler { 17 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 18 w.Write([]byte(tag)) 19 h.ServeHTTP(w, r) 20 }) 21 } 22 } 23 24 // Not recommended (https://golang.org/pkg/reflect/#Value.Pointer), 25 // but the best we can do. 26 func funcsEqual(f1, f2 interface{}) bool { 27 val1 := reflect.ValueOf(f1) 28 val2 := reflect.ValueOf(f2) 29 return val1.Pointer() == val2.Pointer() 30 } 31 32 var testApp = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 33 w.Write([]byte("app\n")) 34 }) 35 36 func TestNew(t *testing.T) { 37 c1 := func(h http.Handler) http.Handler { 38 return nil 39 } 40 41 c2 := func(h http.Handler) http.Handler { 42 return http.StripPrefix("potato", nil) 43 } 44 45 slice := []Middleware{c1, c2} 46 c := New(slice...) 47 for k := range slice { 48 assert.True(t, funcsEqual(c.(chain).middlewares[k], slice[k]), 49 "New does not add constructors correctly") 50 } 51 } 52 53 func TestThenWorksWithNoMiddleware(t *testing.T) { 54 assert.True(t, funcsEqual(New().Then(testApp), testApp), 55 "Then does not work with no middleware") 56 } 57 58 func TestThenTreatsNilAsDefaultServeMux(t *testing.T) { 59 assert.Equal(t, http.DefaultServeMux, New().Then(nil), 60 "Then does not treat nil as DefaultServeMux") 61 } 62 63 func TestThenFuncTreatsNilAsDefaultServeMux(t *testing.T) { 64 assert.Equal(t, http.DefaultServeMux, New().ThenFunc(nil), 65 "ThenFunc does not treat nil as DefaultServeMux") 66 } 67 68 func TestThenFuncConstructsHandlerFunc(t *testing.T) { 69 fn := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 70 w.WriteHeader(200) 71 }) 72 chained := New().ThenFunc(fn) 73 rec := httptest.NewRecorder() 74 75 chained.ServeHTTP(rec, (*http.Request)(nil)) 76 77 assert.Equal(t, reflect.TypeOf((http.HandlerFunc)(nil)), reflect.TypeOf(chained), 78 "ThenFunc does not construct HandlerFunc") 79 } 80 81 func TestThenOrdersHandlersCorrectly(t *testing.T) { 82 t1 := tagMiddleware("t1\n") 83 t2 := tagMiddleware("t2\n") 84 t3 := tagMiddleware("t3\n") 85 86 chained := New(t1, t2, t3).Then(testApp) 87 88 w := httptest.NewRecorder() 89 r, err := http.NewRequest("GET", "/", http.NoBody) 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 chained.ServeHTTP(w, r) 95 96 assert.Equal(t, "t1\nt2\nt3\napp\n", w.Body.String(), 97 "Then does not order handlers correctly") 98 } 99 100 func TestAppendAddsHandlersCorrectly(t *testing.T) { 101 c := New(tagMiddleware("t1\n"), tagMiddleware("t2\n")) 102 c = c.Append(tagMiddleware("t3\n"), tagMiddleware("t4\n")) 103 h := c.Then(testApp) 104 105 w := httptest.NewRecorder() 106 r, err := http.NewRequest("GET", "/", http.NoBody) 107 assert.Nil(t, err) 108 109 h.ServeHTTP(w, r) 110 assert.Equal(t, "t1\nt2\nt3\nt4\napp\n", w.Body.String(), 111 "Append does not add handlers correctly") 112 } 113 114 func TestExtendAddsHandlersCorrectly(t *testing.T) { 115 c := New(tagMiddleware("t3\n"), tagMiddleware("t4\n")) 116 c = c.Prepend(tagMiddleware("t1\n"), tagMiddleware("t2\n")) 117 h := c.Then(testApp) 118 119 w := httptest.NewRecorder() 120 r, err := http.NewRequest("GET", "/", nil) 121 assert.Nil(t, err) 122 123 h.ServeHTTP(w, r) 124 assert.Equal(t, "t1\nt2\nt3\nt4\napp\n", w.Body.String(), 125 "Extend does not add handlers in correctly") 126 }