// New is the constructor for Application struct. func New(config *viper.Viper) (*Application, error) { dsn := config.Get("dsn").(string) db, err := sqlx.Connect("mysql", dsn) if err != nil { return nil, err } cookieStoreSecret := config.Get("cookie_secret").(string) app := &Application{} app.config = config app.router = httprouter.New() app.dsn = dsn app.db = db app.cookieStore = sessions.NewCookieStore([]byte(cookieStoreSecret)) return app, nil }
func TestHandle(t *testing.T) { router := httprouter.New() group := NewApiGroup("test", nil) var resource = NewApiResource("resource_a", "scope_a") group.AddResource(resource) group.Routing("api", router) w := new(mockResponseWriter) req2, _ := http.NewRequest("GET", "/api/test//action_b", nil) router.ServeHTTP(w, req2) if w.status != http.StatusBadRequest { t.Error("Must reply error on resource not found") } req3, _ := http.NewRequest("GET", "/api/test/resource_a/action_00", nil) router.ServeHTTP(w, req3) if w.status != http.StatusBadRequest { t.Error("Must reply error on action not found") } resource.AddAction("POST", "action_a", "test", func(c *Context) { c.RespondOK("") }) req4, _ := http.NewRequest("POST", "/api/test/resource_a/action_a", nil) router.ServeHTTP(w, req4) if w.status != http.StatusOK { t.Error("Routing Failed") } req5, _ := http.NewRequest("GET", "/api/test/resource_a/action_a", nil) router.ServeHTTP(w, req5) if w.status != http.StatusMethodNotAllowed { t.Error("Must reply error on method not found") } }