Beispiel #1
0
func TestRouterAdd(t *testing.T) {
	r := NewRouter()
	_ = r.Add(controller.GetCtrlFunc(&Sample{}))

	req, err := http.NewRequest("GET", "/sample/bang", nil)
	if err != nil {
		t.Error(err)
	}
	w := httptest.NewRecorder()
	r.ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Errorf("expected %d got %d", http.StatusOK, w.Code)
	}
	if w.Body.String() != msg {
		t.Errorf("expected %s got %s", msg, w.Body.String())
	}
}
Beispiel #2
0
func TestMiddlewareContext(t *testing.T) {
	expect := "3"
	r := NewRouter()
	_ = r.Add(controller.GetCtrlFunc(&Sample{}), contextMiddleware(0), contextMiddleware(1), contextMiddleware(2))

	req, err := http.NewRequest("GET", "/sample/increment", nil)
	if err != nil {
		t.Error(err)
	}
	w := httptest.NewRecorder()
	r.ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Errorf("expected %d got %d", http.StatusOK, w.Code)
	}
	if w.Body.String() != expect {
		t.Errorf("expected %s got %s", expect, w.Body.String())
	}
}
Beispiel #3
0
func TestMVC(t *testing.T) {
	app, err := NewMVC("fixtures/mvc")
	if err != nil {
		t.Skip(err)
	}
	app.AddController(controller.GetCtrlFunc(&SimpleMVC{}))

	req, _ := http.NewRequest("GET", "/simplemvc/hello", nil)
	w := httptest.NewRecorder()
	app.ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Errorf("expcted %d got %d", http.StatusOK, w.Code)
	}

	if !strings.Contains(w.Body.String(), "gernest") {
		t.Errorf("expected %s to contain gernest", w.Body.String())
	}

}
Beispiel #4
0
func TestRouteField(t *testing.T) {
	r := NewRouter()
	routes := []string{
		"get,post;/hello/world;Hello",
	}
	s := &Sample{}
	s.Routes = routes
	err := r.Add(controller.GetCtrlFunc(s))
	if err != nil {
		t.Error(err)
	}
	req, err := http.NewRequest("GET", "/hello/world", nil)
	if err != nil {
		t.Error(err)
	}
	w := httptest.NewRecorder()
	r.ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Errorf("expected %d got %d", http.StatusOK, w.Code)
	}
	if w.Body.String() != msg {
		t.Errorf("expected %s got %s", msg, w.Body.String())
	}

	req, err = http.NewRequest("GET", "/sample/bang", nil)
	if err != nil {
		t.Error(err)
	}
	w = httptest.NewRecorder()
	r.ServeHTTP(w, req)

	if w.Code != http.StatusOK {
		t.Errorf("expected %d got %d", http.StatusOK, w.Code)
	}
	if w.Body.String() != msg {
		t.Errorf("expected %s got %s", msg, w.Body.String())
	}
}
Beispiel #5
0
func TestRoutesFile(t *testing.T) {

	routeFiles := []string{
		"../fixtures/config/routes.json",
		"../fixtures/config/routes.yml",
		"../fixtures/config/routes.toml",
		"../fixtures/config/routes.hcl",
	}

	for _, file := range routeFiles {
		r := NewRouter()

		err := r.LoadRoutesFile(file)
		if err != nil {
			t.Error(err)
		}
		if len(r.routes) != 2 {
			t.Errorf("expcted 2 got %d", len(r.routes))
		}
		_ = r.Add(controller.GetCtrlFunc(NewSample()))

		req, err := http.NewRequest("GET", "/hello", nil)
		if err != nil {
			t.Error(err)
		}
		w := httptest.NewRecorder()
		r.ServeHTTP(w, req)

		if w.Code != http.StatusOK {
			t.Errorf("expected %d got %d", http.StatusOK, w.Code)
		}
		if w.Body.String() != msg {
			t.Errorf("expected %s got %s", msg, w.Body.String())
		}
	}

}