github.com/louisevanderlith/droxolite@v1.20.2/sample/appclient_test.go (about) 1 package sample 2 3 import ( 4 "github.com/gorilla/mux" 5 "github.com/louisevanderlith/droxolite/drx" 6 "github.com/louisevanderlith/droxolite/mix" 7 "net/http" 8 "net/http/httptest" 9 "strings" 10 "testing" 11 12 "github.com/louisevanderlith/droxolite/sample/clients" 13 ) 14 15 func TestAPP_DistAsset_OK(t *testing.T) { 16 req, err := http.NewRequest("GET", "/dist/site.css", nil) 17 18 if err != nil { 19 t.Fatal(err) 20 } 21 22 handle := appRoutes() 23 24 rr := httptest.NewRecorder() 25 handle.ServeHTTP(rr, req) 26 27 if rr.Code != http.StatusOK { 28 t.Errorf("Not OK: %v", rr.Code) 29 } 30 31 expected := "h1{margin: auto;}" 32 if rr.Body.String() != expected { 33 t.Errorf("unexpected body: got %v want %v", 34 rr.Body.String(), expected) 35 } 36 } 37 38 func TestAPP_Home_OK(t *testing.T) { 39 req, err := http.NewRequest("GET", "/", nil) 40 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 handle := appRoutes() 46 47 rr := httptest.NewRecorder() 48 handle.ServeHTTP(rr, req) 49 50 if rr.Code != http.StatusOK { 51 t.Errorf("Not OK: %v", rr.Code) 52 } 53 54 expected := "<h1>MasterPage</h1><p>This is the Home Page</p><p>Welcome</p><span>Footer</span>" 55 if rr.Body.String() != expected { 56 t.Errorf("unexpected body: got %v want %v", 57 rr.Body.String(), expected) 58 } 59 } 60 61 func BenchmarkAPP_SubDefault_OK(b *testing.B) { 62 req, err := http.NewRequest("GET", "/stock/parts", nil) 63 64 if err != nil { 65 b.Fatal(err) 66 } 67 68 handle := appRoutes() 69 70 rr := httptest.NewRecorder() 71 handle.ServeHTTP(rr, req) 72 73 if rr.Code != http.StatusOK { 74 b.Errorf("Not OK: %v", rr.Code) 75 } 76 77 expected := "<h1>MasterPage</h1><h2>Layout X</h2><div><p>This is the parts page</p></div><span>Footer</span>" 78 act := rr.Body.String() 79 if act != expected { 80 b.Errorf("unexpected body: got %v want %v", act, expected) 81 } 82 } 83 84 func TestAPP_SubDefault_OK(t *testing.T) { 85 req, err := http.NewRequest("GET", "/stock/parts", nil) 86 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 handle := appRoutes() 92 93 rr := httptest.NewRecorder() 94 handle.ServeHTTP(rr, req) 95 96 if rr.Code != http.StatusOK { 97 t.Errorf("Not OK: %v", rr.Code) 98 } 99 100 expected := "<h1>MasterPage</h1><h2>Layout X</h2><div><p>This is the parts page</p></div><span>Footer</span>" 101 if rr.Body.String() != expected { 102 t.Errorf("unexpected body: got %v want %v", 103 rr.Body.String(), expected) 104 } 105 } 106 107 func TestAPP_Error_OK(t *testing.T) { 108 req, err := http.NewRequest("GET", "/broken", nil) 109 110 if err != nil { 111 t.Fatal(err) 112 } 113 114 handle := appRoutes() 115 116 rr := httptest.NewRecorder() 117 handle.ServeHTTP(rr, req) 118 119 if rr.Code != http.StatusInternalServerError { 120 t.Errorf("Not OK: %v", rr.Code) 121 } 122 123 expected := "<h1>MasterPage</h1><h1>Something unexpected Happened:</h1><p>this path must break</p> <span>Footer</span>" 124 125 if rr.Body.Len() != len(expected) { 126 t.Errorf("unexpected length: got %v want %v", 127 rr.Body.Len(), len(expected)) 128 } 129 130 if strings.Compare(rr.Body.String(), expected) != 0 { 131 t.Errorf("unexpected body: got %v want %v", 132 rr.Body.String(), expected) 133 } 134 } 135 136 func appRoutes() http.Handler { 137 tmpl, err := drx.LoadTemplate("./views") 138 139 if err != nil { 140 panic(err) 141 } 142 143 r := mux.NewRouter() 144 distPath := http.FileSystem(http.Dir("dist/")) 145 fs := http.FileServer(distPath) 146 r.PathPrefix("/dist/").Handler(http.StripPrefix("/dist/", fs)) 147 148 fact := mix.NewPageFactory(tmpl) 149 r.HandleFunc("/", clients.InterfaceGet(fact)).Methods(http.MethodGet) 150 r.HandleFunc("/{pagesize:[A-Z][0-9]+}/{hash:[a-zA-Z0-9]+={0,2}}", clients.InterfaceSearch(fact)).Methods(http.MethodGet) 151 r.HandleFunc("/{pagesize:[A-Z][0-9]+}", clients.InterfaceSearch(fact)).Methods(http.MethodGet) 152 r.HandleFunc("/{key:[0-9]+\x60[0-9]+}", clients.InterfaceView(fact)).Methods(http.MethodGet) 153 r.HandleFunc("/create", clients.InterfaceCreate(fact)).Methods(http.MethodPost) 154 155 stck := r.PathPrefix("/stock").Subrouter() 156 stck.HandleFunc("/parts", clients.PartsGet(fact)).Methods(http.MethodGet) 157 stck.HandleFunc("/parts/{pagesize:[A-Z][0-9]+}/{hash:[a-zA-Z0-9]+={0,2}}", clients.PartsSearch(fact)).Methods(http.MethodGet) 158 stck.HandleFunc("/parts/{pagesize:[A-Z][0-9]+}", clients.PartsSearch(fact)).Methods(http.MethodGet) 159 stck.HandleFunc("/parts/{key:[0-9]+\x60[0-9]+}", clients.PartsView(fact)).Methods(http.MethodGet) 160 stck.HandleFunc("/parts/create", clients.PartsCreate(fact)).Methods(http.MethodPost) 161 stck.HandleFunc("/services", clients.ServicesGet(fact)).Methods(http.MethodGet) 162 stck.HandleFunc("/services/{pagesize:[A-Z][0-9]+}/{hash:[a-zA-Z0-9]+={0,2}}", clients.ServicesSearch(fact)).Methods(http.MethodGet) 163 stck.HandleFunc("/services/{pagesize:[A-Z][0-9]+}", clients.ServicesSearch(fact)).Methods(http.MethodGet) 164 stck.HandleFunc("/services/{key:[0-9]+\x60[0-9]+}", clients.ServicesView(fact)).Methods(http.MethodGet) 165 stck.HandleFunc("/services/create", clients.ServicesCreate(fact)).Methods(http.MethodPost) 166 167 return r 168 }