Example #1
0
func TestReroute(t *testing.T) {
	r := gas.New().Get("/reroute1", func(g *gas.Gas) (int, gas.Outputter) {
		return 303, Reroute("/reroute2", map[string]string{"test": "ok"})
	}).Get("/reroute2", CheckReroute, func(g *gas.Gas) (int, gas.Outputter) {
		var m map[string]string
		if err := Recover(g, &m); err != nil {
			t.Fatal(err)
			fmt.Fprint(g, "no")
		}
		fmt.Fprint(g, m["test"])
		return -1, nil
	})

	srv := httptest.NewServer(r)
	defer srv.Close()
	testutil.TestGet(t, srv, "/reroute1", "ok")
}
Example #2
0
func TestOutputter(t *testing.T) {
	parseTemplates(templateDir)
	r := gas.New().Get("/htmltest", func(g *gas.Gas) (int, gas.Outputter) {
		return 200, HTML("a/index", "world")
	}).
		Get("/jsontest", func(g *gas.Gas) (int, gas.Outputter) {
			return 200, JSON(&struct {
				A int
				B string
				C bool
			}{-203881, "asdf", true})
		}).
		Get("/htmltest2", func(g *gas.Gas) (int, gas.Outputter) {
			return 200, HTML("m/a/g/i/c", "# hi\n")
		}).
		Get("/htmltest3", func(g *gas.Gas) (int, gas.Outputter) {
			return 200, HTML("something", 123)
		}).
		Get("/htmltest4", func(g *gas.Gas) (int, gas.Outputter) {
			return 200, HTML("something", "abc", "layouts/parens", "layouts/brackets", "layouts/quotes")
		}).
		Get("/htmltest5", func(g *gas.Gas) (int, gas.Outputter) {
			return 200, HTML("something", "xyz", "layouts/parens")
		}).
		Get("/htmltest6", func(g *gas.Gas) (int, gas.Outputter) {
			return 200, HTML("donottrythisathome", 3, "layouts/wat")
		}).
		Get("/htmltest7", func(g *gas.Gas) (int, gas.Outputter) {
			return 200, HTML("something", "asdfasdf", "layouts/nonexistent")
		})

	srv := httptest.NewServer(r)
	defer srv.Close()

	testutil.TestGet(t, srv, "/htmltest", "Hello, world! testing!")
	testutil.TestGet(t, srv, "/jsontest", `{"A":-203881,"B":"asdf","C":true}`+"\n")
	testutil.TestGet(t, srv, "/htmltest2", "<h1>hi</h1>\n")
	testutil.TestGet(t, srv, "/htmltest3", "123123123")
	testutil.TestGet(t, srv, "/htmltest4", `(["abcabcabc"])`)
	testutil.TestGet(t, srv, "/htmltest5", "(xyzxyzxyz)")
	testutil.TestGet(t, srv, "/htmltest6", "3(3)3")
	testutil.TestGet(t, srv, "/htmltest7", "no such layout nonexistent in path layouts")
}
Example #3
0
func ExampleRouter() {
	// A simple "static" route.
	loginForm := func(g *gas.Gas) (int, gas.Outputter) {
		return 200, out.HTML("example/login-form", nil)
	}

	// JSON REST? Sure.
	login := func(g *gas.Gas) (int, gas.Outputter) {
		u := new(myUser).byUsername(g.FormValue("user"))
		if err := auth.SignIn(g, u, g.FormValue("pass")); err != nil {
			return 403, out.JSON(M{"error": err.Error()})
		} else {
			return 204, nil
		}
	}

	// Reroute users (+ a cookie with the path data) if not logged in
	checkLogin := func(path string) func(g *gas.Gas) (int, gas.Outputter) {
		return func(g *gas.Gas) (int, gas.Outputter) {
			if sess, err := auth.GetSession(g); sess == nil || err != nil {
				return 303, out.Reroute(path, map[string]string{"path": g.URL.Path})
			} else {
				g.SetData("user", new(myUser).byUsername(sess.Username))
			}
			return 0, nil
		}
	}

	// A page behind the login wall
	profile := func(g *gas.Gas) (int, gas.Outputter) {
		user := g.Data("user").(*myUser)
		return 200, out.HTML("example", user)
	}

	// The router
	gas.New().
		Get("/profile", checkLogin("/login"), profile).
		Get("/login", loginForm).
		Post("/login", login)
}
Example #4
0
func TestAuth(t *testing.T) {
	/*
		runtime.GOMAXPROCS(runtime.NumCPU())
		go func() {
			//fmt.Println(http.ListenAndServe(":6006", nil))
		}()
	*/
	testPass := "******"
	hash, salt := auth.NewHash([]byte(testPass))

	tx, err := db.DB.Begin()
	if err != nil {
		t.Fatal(err)
	}
	tx.Exec(`
	CREATE TEMP TABLE gas_test_users (
		id serial PRIMARY KEY,
		name text NOT NULL,
		pass bytea NOT NULL,
		salt bytea NOT NULL
	)`)
	tx.Exec(`INSERT INTO gas_test_users VALUES ( DEFAULT, 'moshee', $1, $2 )`, hash, salt)
	if err := tx.Commit(); err != nil {
		t.Fatal(err)
	}

	r := gas.New().Get("/", func(g *gas.Gas) (int, gas.Outputter) {
		if sess, err := auth.GetSession(g); sess == nil || err != nil {
			fmt.Fprint(g, "no")
		} else {
			if u, err := new(MyUser).byUsername(sess.Username); err != nil {
				fmt.Fprint(g, "no")
			} else {
				fmt.Fprintf(g, "%d", u.Id)
			}
		}
		return -1, nil
	}).Get("/hmac", func(g *gas.Gas) (int, gas.Outputter) {
		_, err := auth.GetSession(g)
		if err != nil {
			fmt.Fprint(g, "no")
			if err != auth.ErrBadMac {
				t.Fatalf("Expected hmac error, got %v", err)
			}
		} else {
			fmt.Fprint(g, "yes")
		}
		return -1, nil
	}).Post("/login", func(g *gas.Gas) (int, gas.Outputter) {
		u, err := new(MyUser).byUsername(g.FormValue("username"))
		if err != nil {
			fmt.Fprint(g, "no")
			return -1, nil
		}
		if err = auth.SignIn(g, u, g.FormValue("pass")); err != nil {
			fmt.Fprint(g, "no")
		} else {
			fmt.Fprint(g, "yes")
		}
		return -1, nil
	}).Get("/logout", func(g *gas.Gas) (int, gas.Outputter) {
		if err := auth.SignOut(g); err != nil {
			fmt.Fprint(g, "no")
		} else {
			fmt.Fprint(g, "yes")
		}
		return -1, nil
	})

	t.Log("Testing DB session store")
	dbs, err := db.NewStore("gas_sessions")
	if err != nil {
		t.Fatal(err)
	}
	auth.UseSessionStore(dbs)
	testAuth(t, testPass, r)

	t.Log("Testing FS session store")
	fss, err := auth.NewFileStore()
	if err != nil {
		t.Fatal(err)
	}
	defer fss.Destroy()
	auth.UseSessionStore(fss)
	testAuth(t, testPass, r)
}