| 1 | package httpcaddyfile |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "sort" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestHostsFromKeys(t *testing.T) { |
| 10 | for i, tc := range []struct { |
| 11 | keys []Address |
| 12 | expectNormalMode []string |
| 13 | expectLoggerMode []string |
| 14 | }{ |
| 15 | { |
| 16 | []Address{ |
| 17 | {Original: "foo", Host: "foo"}, |
| 18 | }, |
| 19 | []string{"foo"}, |
| 20 | []string{"foo"}, |
| 21 | }, |
| 22 | { |
| 23 | []Address{ |
| 24 | {Original: "foo", Host: "foo"}, |
| 25 | {Original: "bar", Host: "bar"}, |
| 26 | }, |
| 27 | []string{"bar", "foo"}, |
| 28 | []string{"bar", "foo"}, |
| 29 | }, |
| 30 | { |
| 31 | []Address{ |
| 32 | {Original: ":2015", Port: "2015"}, |
| 33 | }, |
| 34 | []string{}, |
| 35 | []string{}, |
| 36 | }, |
| 37 | { |
| 38 | []Address{ |
| 39 | {Original: ":443", Port: "443"}, |
| 40 | }, |
| 41 | []string{}, |
| 42 | []string{}, |
| 43 | }, |
| 44 | { |
| 45 | []Address{ |
| 46 | {Original: "foo", Host: "foo"}, |
| 47 | {Original: ":2015", Port: "2015"}, |
| 48 | }, |
| 49 | []string{}, |
| 50 | []string{"foo"}, |
| 51 | }, |
| 52 | { |
| 53 | []Address{ |
| 54 | {Original: "example.com:2015", Host: "example.com", Port: "2015"}, |
| 55 | }, |
| 56 | []string{"example.com"}, |
| 57 | []string{"example.com:2015"}, |
| 58 | }, |
| 59 | { |
| 60 | []Address{ |
| 61 | {Original: "example.com:80", Host: "example.com", Port: "80"}, |
| 62 | }, |
| 63 | []string{"example.com"}, |
| 64 | []string{"example.com"}, |
| 65 | }, |
| 66 | { |
| 67 | []Address{ |
| 68 | {Original: "https://:2015/foo", Scheme: "https", Port: "2015", Path: "/foo"}, |
| 69 | }, |
| 70 | []string{}, |
| 71 | []string{}, |
| 72 | }, |
| 73 | { |
| 74 | []Address{ |
| 75 | {Original: "https://example.com:2015/foo", Scheme: "https", Host: "example.com", Port: "2015", Path: "/foo"}, |
| 76 | }, |
| 77 | []string{"example.com"}, |
| 78 | []string{"example.com:2015"}, |
| 79 | }, |
| 80 | } { |
| 81 | sb := serverBlock{parsedKeys: tc.keys} |
| 82 | |
| 83 | // test in normal mode |
| 84 | actual := sb.hostsFromKeys(false) |
| 85 | sort.Strings(actual) |
| 86 | if !reflect.DeepEqual(tc.expectNormalMode, actual) { |
| 87 | t.Errorf("Test %d (loggerMode=false): Expected: %v Actual: %v", i, tc.expectNormalMode, actual) |
| 88 | } |
| 89 | |
| 90 | // test in logger mode |
| 91 | actual = sb.hostsFromKeys(true) |
| 92 | sort.Strings(actual) |
| 93 | if !reflect.DeepEqual(tc.expectLoggerMode, actual) { |
| 94 | t.Errorf("Test %d (loggerMode=true): Expected: %v Actual: %v", i, tc.expectLoggerMode, actual) |
| 95 | } |
| 96 | } |
| 97 | } |