1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
package http
import (
"strconv"
"testing"
)
func TestValidPortNumber(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{
{Input: "123", Valid: true},
{Input: "123.0", Valid: false},
{Input: "-123", Valid: false},
{Input: "65536", Valid: false},
{Input: "0", Valid: true},
}
for i, c := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
valid := ValidPortNumber(c.Input)
if e, a := c.Valid, valid; e != a {
t.Errorf("expect valid %v, got %v", e, a)
}
})
}
}
func TestValidHostLabel(t *testing.T) {
cases := []struct {
Input string
Valid bool
}{
{Input: "abc123", Valid: true},
{Input: "123", Valid: true},
{Input: "abc", Valid: true},
{Input: "123-abc", Valid: true},
{Input: "{thing}-abc", Valid: false},
{Input: "abc.123", Valid: false},
{Input: "abc/123", Valid: false},
{Input: "012345678901234567890123456789012345678901234567890123456789123", Valid: true},
{Input: "0123456789012345678901234567890123456789012345678901234567891234", Valid: false},
{Input: "", Valid: false},
}
for i, c := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
valid := ValidHostLabel(c.Input)
if e, a := c.Valid, valid; e != a {
t.Errorf("expect valid %v, got %v", e, a)
}
})
}
}
func TestValidateEndpointHostHandler(t *testing.T) {
cases := map[string]struct {
Input string
Valid bool
}{
"valid host": {Input: "abc.123", Valid: true},
"fqdn host": {Input: "abc.123.", Valid: true},
"empty label": {Input: "abc..", Valid: false},
"max host len": {
Input: "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.12345",
Valid: true,
},
"too long host": {
Input: "123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456",
Valid: false,
},
"valid host with port number": {Input: "abd.123:1234", Valid: true},
"valid host with invalid port number": {Input: "abc.123:99999", Valid: false},
"empty host with port number": {Input: ":1234", Valid: false},
"valid host with empty port number": {Input: "abc.123:", Valid: false},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
err := ValidateEndpointHost(c.Input)
if e, a := c.Valid, err == nil; e != a {
t.Errorf("expect valid %v, got %v, %v", e, a, err)
}
})
}
}
|