Example #1
0
func TestDispatch(t *testing.T) {
	r := New().
		Use(func(g *Gas) (int, Outputter) {
			g.SetData("middleware", true)
			return g.Continue()
		}).
		Get("/test1", func(g *Gas) (int, Outputter) {
			g.Write([]byte("yes"))
			return g.Stop()
		}).
		Get("/test2", func(g *Gas) (int, Outputter) {
			g.SetData("something", 6)
			g.SetData("something else", "test")
			return g.Continue()
		}, func(g *Gas) (int, Outputter) {
			g.Write([]byte(g.Data("something else").(string)))
			return g.Stop()
		}).
		Get("/test3", func(g *Gas) (int, Outputter) {
			g.SetData("test", 10)
			return g.Continue()
		}, func(g *Gas) (int, Outputter) {
			g.Write([]byte(strconv.Itoa(g.Data("test").(int))))
			return g.Stop()
		}, func(g *Gas) (int, Outputter) {
			g.Write([]byte("nope"))
			return g.Stop()
		}).
		Get("/test4", func(g *Gas) (int, Outputter) {
			g.Write([]byte(strconv.FormatBool(g.Data("middleware").(bool))))
			return g.Stop()
		}).
		Get("/panic", func(g *Gas) (int, Outputter) {
			panic("lol")
		})

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

	testutil.TestGet(t, srv, "/test1", "yes")
	testutil.TestGet(t, srv, "/test2", "test")
	testutil.TestGet(t, srv, "/test3", "10")
	testutil.TestGet(t, srv, "/test4", "true")

	resp, err := testutil.Client.Get(srv.URL + "/panic")
	if err != nil {
		t.Fatal(err)
	}
	if resp.StatusCode != 500 {
		t.Fatalf("expected 500 code for panic, got %d", resp.StatusCode)
	}
}
Example #2
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 #3
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")
}