Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 10, 22:30 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 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 caddy |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func TestGetModules(t *testing.T) { |
| 23 | modulesMu.Lock() |
| 24 | modules = map[string]ModuleInfo{ |
| 25 | "a": {ID: "a"}, |
| 26 | "a.b": {ID: "a.b"}, |
| 27 | "a.b.c": {ID: "a.b.c"}, |
| 28 | "a.b.cd": {ID: "a.b.cd"}, |
| 29 | "a.c": {ID: "a.c"}, |
| 30 | "a.d": {ID: "a.d"}, |
| 31 | "b": {ID: "b"}, |
| 32 | "b.a": {ID: "b.a"}, |
| 33 | "b.b": {ID: "b.b"}, |
| 34 | "b.a.c": {ID: "b.a.c"}, |
| 35 | "c": {ID: "c"}, |
| 36 | } |
| 37 | modulesMu.Unlock() |
| 38 | |
| 39 | for i, tc := range []struct { |
| 40 | input string |
| 41 | expect []ModuleInfo |
| 42 | }{ |
| 43 | { |
| 44 | input: "", |
| 45 | expect: []ModuleInfo{ |
| 46 | {ID: "a"}, |
| 47 | {ID: "b"}, |
| 48 | {ID: "c"}, |
| 49 | }, |
| 50 | }, |
| 51 | { |
| 52 | input: "a", |
| 53 | expect: []ModuleInfo{ |
| 54 | {ID: "a.b"}, |
| 55 | {ID: "a.c"}, |
| 56 | {ID: "a.d"}, |
| 57 | }, |
| 58 | }, |
| 59 | { |
| 60 | input: "a.b", |
| 61 | expect: []ModuleInfo{ |
| 62 | {ID: "a.b.c"}, |
| 63 | {ID: "a.b.cd"}, |
| 64 | }, |
| 65 | }, |
| 66 | { |
| 67 | input: "a.b.c", |
| 68 | }, |
| 69 | { |
| 70 | input: "b", |
| 71 | expect: []ModuleInfo{ |
| 72 | {ID: "b.a"}, |
| 73 | {ID: "b.b"}, |
| 74 | }, |
| 75 | }, |
| 76 | { |
| 77 | input: "asdf", |
| 78 | }, |
| 79 | } { |
| 80 | actual := GetModules(tc.input) |
| 81 | if !reflect.DeepEqual(actual, tc.expect) { |
| 82 | t.Errorf("Test %d: Expected %v but got %v", i, tc.expect, actual) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func TestModuleID(t *testing.T) { |
| 88 | for i, tc := range []struct { |
| 89 | input ModuleID |
| 90 | expectNamespace string |
| 91 | expectName string |
| 92 | }{ |
| 93 | { |
| 94 | input: "foo", |
| 95 | expectNamespace: "", |
| 96 | expectName: "foo", |
| 97 | }, |
| 98 | { |
| 99 | input: "foo.bar", |
| 100 | expectNamespace: "foo", |
| 101 | expectName: "bar", |
| 102 | }, |
| 103 | { |
| 104 | input: "a.b.c", |
| 105 | expectNamespace: "a.b", |
| 106 | expectName: "c", |
| 107 | }, |
| 108 | } { |
| 109 | actualNamespace := tc.input.Namespace() |
| 110 | if actualNamespace != tc.expectNamespace { |
| 111 | t.Errorf("Test %d: Expected namespace '%s' but got '%s'", i, tc.expectNamespace, actualNamespace) |
| 112 | } |
| 113 | actualName := tc.input.Name() |
| 114 | if actualName != tc.expectName { |
| 115 | t.Errorf("Test %d: Expected name '%s' but got '%s'", i, tc.expectName, actualName) |
| 116 | } |
| 117 | } |
| 118 | } |