Esempio n. 1
0
func TestAuthenticateFailure(t *testing.T) {
	method := "GET"
	path := root + "/authenticate/failure"
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{method: method, path: path}
	want := &wanted{code: http.StatusUnauthorized, success: false}
	makeRequest(t, app, params, want)
}
Esempio n. 2
0
func TestSessionGetSuccessCreateEmpty(t *testing.T) {
	method := "GET"
	path := root + "/session-get"
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{method: method, path: path}
	want := &wanted{code: http.StatusOK, success: true}
	makeRequest(t, app, params, want)
}
Esempio n. 3
0
func TestMethodNotAllowed(t *testing.T) {
	method := "OPTIONS"
	path := root
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{method: method, path: path}
	want := &wanted{code: http.StatusMethodNotAllowed, success: false}
	makeRequest(t, app, params, want)
}
Esempio n. 4
0
func TestServerError(t *testing.T) {
	method := "GET"
	path := root + "/server-error"
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{method: method, path: path}
	want := &wanted{code: http.StatusInternalServerError, success: false}
	makeRequest(t, app, params, want)
}
Esempio n. 5
0
func TestNotFound(t *testing.T) {
	method := "GET"
	path := root + "/not-found"
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{method: method, path: path}
	want := &wanted{code: http.StatusNotFound, success: false}
	makeRequest(t, app, params, want)
}
Esempio n. 6
0
func TestCSRFFailureBodyNil(t *testing.T) {
	method := "GET"
	path := root + "/csrf"
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{method: method, path: path}
	want := &wanted{code: http.StatusBadRequest, success: false}
	makeRequest(t, app, params, want)
}
Esempio n. 7
0
func TestAuthenticateSuccess(t *testing.T) {
	method := "GET"
	path := root + "/authenticate/success"
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{method: method, path: path}
	want := &wanted{code: http.StatusOK, success: true}
	makeRequest(t, app, params, want)
}
Esempio n. 8
0
func TestSessionSetUpdateError(t *testing.T) {
	method := "GET"
	path := root + "/session-set"
	auth := sessionIDWithUpdateError
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{auth: auth, method: method, path: path}
	want := &wanted{code: http.StatusInternalServerError, success: false}
	makeRequest(t, app, params, want)
}
Esempio n. 9
0
func TestSessionSetSuccess(t *testing.T) {
	method := "GET"
	path := root + "/session-set"
	auth := sessionIDExistent
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{auth: auth, method: method, path: path}
	want := &wanted{code: http.StatusOK, success: true}
	makeRequest(t, app, params, want)
}
Esempio n. 10
0
func TestSessionGetSuccessCreateErrorWithDeleteError(t *testing.T) {
	method := "GET"
	path := root + "/session-get/create-error"
	auth := sessionIDWithDeleteError
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{auth: auth, method: method, path: path}
	want := &wanted{code: http.StatusOK, success: true}
	makeRequest(t, app, params, want)
}
Esempio n. 11
0
func TestCSRFSuccess(t *testing.T) {
	method := "GET"
	path := root + "/csrf"
	body := []byte(fmt.Sprintf("{\"sessionid\": \"%s\"}", sessionIDExistent))
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{body: body, method: method, path: path}
	want := &wanted{code: http.StatusOK, success: true}
	makeRequest(t, app, params, want)
}
Esempio n. 12
0
func TestCSRFFailureWrongSessionID(t *testing.T) {
	method := "GET"
	path := root + "/csrf"
	body := []byte(fmt.Sprintf("{\"sessionid\": \"WRONG-SESSION-ID\"}"))
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{body: body, method: method, path: path}
	want := &wanted{code: http.StatusBadRequest, success: false}
	makeRequest(t, app, params, want)
}
Esempio n. 13
0
func TestBodyParserSuccess(t *testing.T) {
	method := "POST"
	path := root + "/body-parser/success"
	body := []byte(arbitraryJSON)
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{body: body, method: method, path: path}
	want := &wanted{code: http.StatusOK, success: true}
	makeRequest(t, app, params, want)
}
Esempio n. 14
0
func TestBodyParserFailureNoInit(t *testing.T) {
	method := "POST"
	path := root + "/body-parser/failure/no-init"
	body := []byte(arbitraryJSON)
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{body: body, method: method, path: path}
	want := &wanted{code: http.StatusInternalServerError, success: false}
	makeRequest(t, app, params, want)
}
Esempio n. 15
0
func TestBodyParserFailureBadInput(t *testing.T) {
	method := "POST"
	path := root + "/body-parser/success"
	body := []byte("{BAD JSON}")
	app := forest.New("")
	app.RegisterRoute(root, newRouter(app))
	params := &requested{body: body, method: method, path: path}
	want := &wanted{code: http.StatusBadRequest, success: false}
	makeRequest(t, app, params, want)
}
Esempio n. 16
0
func TestSafeErrorFilter(t *testing.T) {
	method := "GET"
	app := forest.New("")
	app.SafeErrorFilter = func(err error) error {
		if err.Error() == customSafeErrorMessage {
			return err
		} else {
			return nil
		}
	}
	app.RegisterRoute(root, newRouter(app))
	// test safe errors via custom filter
	path := root + "/safe-error/success"
	params := &requested{method: method, path: path}
	want := &wanted{code: http.StatusInternalServerError, success: false}
	_, forestResponse := makeRequest(t, app, params, want)
	if forestResponse.Message != customSafeErrorMessage {
		t.Errorf("%s %s should return message: %s",
			method, path, customSafeErrorMessage)
	}
	// test unsafe errors not passing through custom filter
	path = root + "/safe-error/failure"
	params = &requested{method: method, path: path}
	want = &wanted{code: http.StatusInternalServerError, success: false}
	_, forestResponse = makeRequest(t, app, params, want)
	if forestResponse.Message == customUnsafeErrorMessage {
		t.Errorf("%s %s should NOT return unsafe message: %s",
			method, path, customUnsafeErrorMessage)
	}
	// test unsafe errors passing through if app.Config.Debug is true
	app.Config.Debug = true
	path = root + "/safe-error/failure"
	params = &requested{method: method, path: path}
	want = &wanted{code: http.StatusInternalServerError, success: false}
	_, forestResponse = makeRequest(t, app, params, want)
	if forestResponse.Message != customUnsafeErrorMessage {
		t.Errorf("%s %s should return unsafe message if %s is true: %s",
			method, path, "app.Config.Debug", customUnsafeErrorMessage)
	}
}