github.com/go-graphite/carbonapi@v0.17.0/zipper/helper/requests_test.go (about) 1 package helper 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func Test_stripHtmlTags(t *testing.T) { 11 tests := []struct { 12 name string 13 s string 14 maxLen int 15 want string 16 }{ 17 { 18 name: "Empty", 19 s: "", 20 maxLen: 10, 21 want: "", 22 }, 23 { 24 name: "Broken #1", 25 s: "<html>\r\n<head", 26 maxLen: 0, 27 want: "head", 28 }, 29 { 30 name: "Broken #2", 31 s: "<html>\r\nhead>", 32 maxLen: 0, 33 want: "head>", 34 }, 35 { 36 name: "Nginx Gateway Timeout", 37 s: "<html>\r\n<head><title>504 Gateway Time-out</title></head>\r\n<body>\r\n<center><h1>504 Gateway Time-out</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>\r", 38 maxLen: 0, 39 want: "504 Gateway Time-out\r\n\r\n504 Gateway Time-out\r\nnginx", 40 }, 41 } 42 for _, tt := range tests { 43 t.Run(tt.name, func(t *testing.T) { 44 assert.Equal(t, tt.want, stripHtmlTags(tt.s, tt.maxLen)) 45 }) 46 } 47 } 48 49 func Test_recalcCode(t *testing.T) { 50 tests := []struct { 51 code int 52 newCode int 53 want int 54 }{ 55 {code: 500, newCode: 403, want: 403}, 56 {code: 403, newCode: 500, want: 403}, 57 {code: 403, newCode: 400, want: 400}, 58 {code: 400, newCode: 403, want: 400}, 59 {code: 500, newCode: 503, want: 500}, 60 {code: 503, newCode: 500, want: 500}, 61 {code: 503, newCode: 502, want: 503}, 62 {code: 0, newCode: 502, want: 503}, 63 } 64 for i, tt := range tests { 65 t.Run(strconv.Itoa(i), func(t *testing.T) { 66 if got := recalcCode(tt.code, tt.newCode); got != tt.want { 67 t.Errorf("recalcCode(%d, %d) = %d, want %d", tt.code, tt.newCode, got, tt.want) 68 } 69 }) 70 } 71 }