github.com/erda-project/erda-infra@v1.0.9/providers/httpserver/interceptors/interceptor_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package interceptors 16 17 import ( 18 "fmt" 19 "net/http" 20 "testing" 21 22 "github.com/labstack/echo" 23 "github.com/stretchr/testify/assert" 24 25 "github.com/erda-project/erda-infra/providers/httpserver/mock" 26 ) 27 28 func mustGetC(url string, header http.Header) echo.Context { 29 e := echo.New() 30 r, err := http.NewRequest(http.MethodGet, url, nil) 31 if err != nil { 32 panic(err) 33 } 34 r.Header = header 35 c := e.NewContext(r, nil) 36 resp := echo.NewResponse(mock.NewHTTPResponseWriter(), e) 37 *c.Response() = *resp 38 return c 39 } 40 41 func Test_judgeAnyEnable(t *testing.T) { 42 43 type args struct { 44 c echo.Context 45 enableFetchFuncs []EnableFetchFunc 46 } 47 tests := []struct { 48 name string 49 args args 50 want bool 51 }{ 52 { 53 name: "all and default is disabled", 54 args: args{ 55 c: mustGetC("localhost", nil), 56 enableFetchFuncs: nil, 57 }, 58 want: false, 59 }, 60 { 61 name: "first one is enabled", 62 args: args{ 63 c: mustGetC("localhost", nil), 64 enableFetchFuncs: []EnableFetchFunc{ 65 func(c echo.Context) bool { return true }, 66 }, 67 }, 68 want: true, 69 }, 70 { 71 name: "the last one is enabled", 72 args: args{ 73 c: mustGetC("localhost", nil), 74 enableFetchFuncs: []EnableFetchFunc{ 75 func(c echo.Context) bool { return false }, 76 func(c echo.Context) bool { return false }, 77 func(c echo.Context) bool { return true }, 78 }, 79 }, 80 want: true, 81 }, 82 { 83 name: "all disabled, but defined at url query", 84 args: args{ 85 c: mustGetC(fmt.Sprintf("localhost?%s", defaultDebugFlag), nil), 86 enableFetchFuncs: nil, 87 }, 88 want: true, 89 }, 90 { 91 name: "all disabled, but defined at header", 92 args: args{ 93 c: mustGetC("localhost", http.Header{defaultDebugFlag: []string{}}), 94 enableFetchFuncs: nil, 95 }, 96 want: true, 97 }, 98 } 99 for _, tt := range tests { 100 t.Run(tt.name, func(t *testing.T) { 101 if got := judgeAnyEnable(tt.args.c, tt.args.enableFetchFuncs); got != tt.want { 102 t.Errorf("judgeAnyEnable() = %v, want %v", got, tt.want) 103 } 104 }) 105 } 106 } 107 108 func TestPassThroughEnableFlag(t *testing.T) { 109 middlewareFunc := PassThroughDebugFlag() 110 // get handler func 111 handlerFunc := middlewareFunc(func(c echo.Context) error { return nil }) 112 113 // invoke handler func without flag 114 c := mustGetC("localhost", nil) 115 _, ok := c.Response().Header()[defaultDebugFlag] 116 assert.False(t, ok) 117 err := handlerFunc(c) 118 assert.NoError(t, err) 119 _, ok = c.Response().Header()[defaultDebugFlag] 120 assert.False(t, ok) 121 122 // invoke handler func with flag in url query 123 c = mustGetC(fmt.Sprintf("localhost?%s", defaultDebugFlag), nil) 124 _, ok = c.Response().Header()[defaultDebugFlag] 125 assert.False(t, ok) 126 err = handlerFunc(c) 127 assert.NoError(t, err) 128 _, ok = c.Response().Header()[defaultDebugFlag] 129 assert.True(t, ok) 130 131 // invoke handler func with flag in header 132 c = mustGetC("localhost", http.Header{defaultDebugFlag: []string{}}) 133 _, ok = c.Response().Header()[defaultDebugFlag] 134 assert.False(t, ok) 135 err = handlerFunc(c) 136 assert.NoError(t, err) 137 _, ok = c.Response().Header()[defaultDebugFlag] 138 assert.True(t, ok) 139 }