github.com/llimllib/devd@v0.0.0-20230426145215-4d29fc25f909/route_test.go (about) 1 package devd 2 3 import ( 4 "fmt" 5 "reflect" 6 "strings" 7 "testing" 8 9 "github.com/GeertJohan/go.rice" 10 "github.com/llimllib/devd/inject" 11 "github.com/llimllib/devd/ricetemp" 12 ) 13 14 func tFilesystemEndpoint(s string) *filesystemEndpoint { 15 e, _ := newFilesystemEndpoint(s, []string{}) 16 return e 17 } 18 19 func tForwardEndpoint(s string) *forwardEndpoint { 20 e, _ := newForwardEndpoint(s) 21 return e 22 } 23 24 func within(s string, e error) bool { 25 s = strings.ToLower(s) 26 estr := strings.ToLower(fmt.Sprint(e)) 27 return strings.Contains(estr, s) 28 } 29 30 var newSpecTests = []struct { 31 raw string 32 spec *Route 33 err string 34 }{ 35 { 36 "/one=two", 37 &Route{"", "/one", tFilesystemEndpoint("two")}, 38 "", 39 }, 40 { 41 "/one=two=three", 42 &Route{"", "/one", tFilesystemEndpoint("two=three")}, 43 "", 44 }, 45 { 46 "one", 47 &Route{"", "/", tFilesystemEndpoint("one")}, 48 "invalid spec", 49 }, 50 {"=one", nil, "invalid spec"}, 51 {"one=", nil, "invalid spec"}, 52 { 53 "one/two=three", 54 &Route{"one.devd.io", "/two", tFilesystemEndpoint("three")}, 55 "", 56 }, 57 { 58 "one=three", 59 &Route{"one.devd.io", "/", tFilesystemEndpoint("three")}, 60 "", 61 }, 62 { 63 "one=http://three", 64 &Route{"one.devd.io", "/", tForwardEndpoint("http://three")}, 65 "", 66 }, 67 { 68 "one=localhost:1234", 69 nil, 70 "Unknown scheme 'localhost': did you mean http or https?: localhost:1234", 71 }, 72 { 73 "one=localhost:1234/abc", 74 nil, 75 "Unknown scheme 'localhost': did you mean http or https?: localhost:1234/abc", 76 }, 77 { 78 "one=ws://three", 79 nil, 80 "Websocket protocol not supported: ws://three", 81 }, 82 { 83 "one=:1234", 84 &Route{"one.devd.io", "/", tForwardEndpoint("http://localhost:1234")}, 85 "", 86 }, 87 } 88 89 func TestParseSpec(t *testing.T) { 90 for i, tt := range newSpecTests { 91 s, err := newRoute(tt.raw, []string{}) 92 if tt.spec != nil { 93 if err != nil { 94 t.Errorf("Test %d, error:\n%s\n", i, err) 95 continue 96 } 97 if !reflect.DeepEqual(s, tt.spec) { 98 t.Errorf("Test %d, expecting:\n%s\nGot:\n%s\n", i, tt.spec, s) 99 continue 100 } 101 } else if tt.err != "" { 102 if err == nil { 103 t.Errorf("Test %d, expected error:\n%s\n", i, tt.err) 104 continue 105 } 106 if !within(tt.err, err) { 107 t.Errorf( 108 "Test %d, expected error:\n%s\nGot error:%s\n", 109 i, 110 tt.err, 111 err, 112 ) 113 continue 114 } 115 } 116 } 117 } 118 119 func TestForwardEndpoint(t *testing.T) { 120 f, err := newForwardEndpoint("http://foo") 121 if err != nil { 122 t.Errorf("Unexpected error: %s", err) 123 } 124 rb, err := rice.FindBox("templates") 125 if err != nil { 126 t.Error(err) 127 } 128 templates, err := ricetemp.MakeTemplates(rb) 129 if err != nil { 130 panic(err) 131 } 132 133 f.Handler("", templates, inject.CopyInject{}) 134 135 f, err = newForwardEndpoint("%") 136 if err == nil { 137 t.Errorf("Expected error, got %s", f) 138 } 139 } 140 141 func TestNewRoute(t *testing.T) { 142 r, err := newRoute("foo=http://%", []string{}) 143 if err == nil { 144 t.Errorf("Expected error, got %s", r) 145 } 146 } 147 148 func TestRouteHandler(t *testing.T) { 149 var routeHandlerTests = []struct { 150 spec string 151 }{ 152 {"/one=two"}, 153 } 154 for i, tt := range routeHandlerTests { 155 r, err := newRoute(tt.spec, []string{}) 156 if err != nil { 157 t.Errorf( 158 "Test %d, unexpected error:\n%s\n", 159 i, 160 err, 161 ) 162 } 163 164 rb, err := rice.FindBox("templates") 165 if err != nil { 166 t.Error(err) 167 } 168 templates, err := ricetemp.MakeTemplates(rb) 169 if err != nil { 170 panic(err) 171 } 172 173 r.Endpoint.Handler("", templates, inject.CopyInject{}) 174 } 175 } 176 177 func TestRouteCollection(t *testing.T) { 178 var m = make(RouteCollection) 179 _ = m.String() 180 err := m.Add("foo=bar", []string{}) 181 if err != nil { 182 t.Error(err) 183 } 184 err = m.Add("foo", []string{}) 185 if err != nil { 186 t.Error(err) 187 } 188 189 err = m.Add("xxx=bar", []string{}) 190 if err != nil { 191 t.Errorf("Set error: %s", err) 192 } 193 194 err = m.Add("xxx=bar", []string{}) 195 if err == nil { 196 t.Errorf("Expected error, got: %s", m) 197 } 198 } 199 200 func TestNotFound(t *testing.T) { 201 e, _ := newFilesystemEndpoint("/test", []string{}) 202 fmt.Println(e) 203 }