github.com/cortesi/devd@v0.0.0-20200427000907-c1a3bfba27d8/server_test.go (about) 1 package devd 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/GeertJohan/go.rice" 8 "github.com/cortesi/devd/inject" 9 "github.com/cortesi/devd/ricetemp" 10 "github.com/cortesi/termlog" 11 ) 12 13 var formatURLTests = []struct { 14 tls bool 15 addr string 16 port int 17 output string 18 }{ 19 {true, "127.0.0.1", 8000, "https://devd.io:8000"}, 20 {false, "127.0.0.1", 8000, "http://devd.io:8000"}, 21 {false, "127.0.0.1", 80, "http://devd.io"}, 22 {true, "127.0.0.1", 443, "https://devd.io"}, 23 {false, "127.0.0.1", 443, "http://devd.io:443"}, 24 } 25 26 func TestFormatURL(t *testing.T) { 27 for i, tt := range formatURLTests { 28 url := formatURL(tt.tls, tt.addr, tt.port) 29 if url != tt.output { 30 t.Errorf("Test %d, expected \"%s\" got \"%s\"", i, tt.output, url) 31 } 32 } 33 } 34 35 func TestPickPort(t *testing.T) { 36 _, err := pickPort("127.0.0.1", 8000, 10000, true) 37 if err != nil { 38 t.Errorf("Could not bind to any port: %s", err) 39 } 40 _, err = pickPort("127.0.0.1", 8000, 8000, true) 41 if err == nil { 42 t.Errorf("Expected not to be able to bind to any port") 43 } 44 45 } 46 47 func fsEndpoint(s string) *filesystemEndpoint { 48 e, _ := newFilesystemEndpoint(s, []string{}) 49 return e 50 } 51 52 func TestDevdRouteHandler(t *testing.T) { 53 logger := termlog.NewLog() 54 logger.Quiet() 55 r := Route{"", "/", fsEndpoint("./testdata")} 56 templates := ricetemp.MustMakeTemplates(rice.MustFindBox("templates")) 57 ci := inject.CopyInject{} 58 59 devd := Devd{LivereloadRoutes: true} 60 h := devd.WrapHandler(logger, r.Endpoint.Handler("", templates, ci)) 61 ht := handlerTester{t, h} 62 63 AssertCode(t, ht.Request("GET", "/", nil), 200) 64 } 65 66 func TestDevdHandler(t *testing.T) { 67 logger := termlog.NewLog() 68 logger.Quiet() 69 templates := ricetemp.MustMakeTemplates(rice.MustFindBox("templates")) 70 71 devd := Devd{LivereloadRoutes: true, WatchPaths: []string{"./"}} 72 err := devd.AddRoutes([]string{"./"}, []string{}) 73 if err != nil { 74 t.Error(err) 75 } 76 h, err := devd.Router(logger, templates) 77 if err != nil { 78 t.Error(err) 79 } 80 ht := handlerTester{t, h} 81 82 AssertCode(t, ht.Request("GET", "/", nil), 200) 83 AssertCode(t, ht.Request("GET", "/nonexistent", nil), 404) 84 } 85 86 func TestGetTLSConfig(t *testing.T) { 87 _, err := getTLSConfig("nonexistent") 88 if err == nil { 89 t.Error("Expected failure, found success.") 90 } 91 _, err = getTLSConfig("./testdata/certbundle.pem") 92 if err != nil { 93 t.Errorf("Could not get TLS config: %s", err) 94 } 95 } 96 97 var credentialsTests = []struct { 98 spec string 99 creds *Credentials 100 }{ 101 {"foo:bar", &Credentials{"foo", "bar"}}, 102 {"foo:", nil}, 103 {":bar", nil}, 104 {"foo:bar:voing", &Credentials{"foo", "bar:voing"}}, 105 {"foo", nil}, 106 } 107 108 func TestCredentials(t *testing.T) { 109 for i, data := range credentialsTests { 110 got, _ := CredentialsFromSpec(data.spec) 111 if !reflect.DeepEqual(data.creds, got) { 112 t.Errorf("%d: got %v, expected %v", i, got, data.creds) 113 } 114 } 115 }