Пример #1
0
func TestDefaultLogging(t *testing.T) {
	outputBuf := new(bytes.Buffer)
	logBuf := new(bytes.Buffer)
	LogHandle = logBuf
	OutHandle = outputBuf

	SetLogThreshold(LevelWarn)
	SetStdoutThreshold(LevelError)

	FATAL.Println("fatal err")
	CRITICAL.Println("critical err")
	ERROR.Println("an error")
	WARN.Println("a warning")
	INFO.Println("information")
	DEBUG.Println("debugging info")
	TRACE.Println("trace")

	assert.Contains(t, logBuf.String(), "fatal err")
	assert.Contains(t, logBuf.String(), "critical err")
	assert.Contains(t, logBuf.String(), "an error")
	assert.Contains(t, logBuf.String(), "a warning")
	assert.NotContains(t, logBuf.String(), "information")
	assert.NotContains(t, logBuf.String(), "debugging info")
	assert.NotContains(t, logBuf.String(), "trace")

	assert.Contains(t, outputBuf.String(), "fatal err")
	assert.Contains(t, outputBuf.String(), "critical err")
	assert.Contains(t, outputBuf.String(), "an error")
	assert.NotContains(t, outputBuf.String(), "a warning")
	assert.NotContains(t, outputBuf.String(), "information")
	assert.NotContains(t, outputBuf.String(), "debugging info")
	assert.NotContains(t, outputBuf.String(), "trace")
}
Пример #2
0
func TestRecover(t *testing.T) {
	e := echo.New()
	e.SetDebug(true)
	req, _ := http.NewRequest(echo.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), e)
	h := func(c *echo.Context) error {
		panic("test")
	}
	Recover()(h)(c)
	assert.Equal(t, http.StatusInternalServerError, rec.Code)
	assert.Contains(t, rec.Body.String(), "panic recover")
}
Пример #3
0
func TestSetSettings(t *testing.T) {
	r := createRouter()

	// It should return 400 when there is no json in the body
	req := testRequest(r, "POST", "/account/settings", nil)
	req.Header.Set("Authorization", "token")
	assert.Equal(t, 400, req.send().Code)

	// It should return 400 if the json is invalid
	req.PostForm = url.Values{}
	req.PostForm.Set("json", "not_json")
	assert.Equal(t, 400, req.send().Code)

	// It should return 200 when adding a new setting
	req.PostForm.Set("json", testJSON([]Setting{
		Setting{
			Name:  "test",
			Value: "a",
		},
	}))
	assert.Equal(t, 200, req.send().Code)

	// It should have added the setting
	settings := checkSettings(t, req)
	assert.Contains(t, settings, Setting{
		ID:    1,
		Name:  "test",
		Value: "a",
	})

	// It should return 200 when updating a setting
	req.Method = "POST"
	req.PostForm.Set("json", testJSON([]Setting{
		Setting{
			Name:  "test",
			Value: "b",
		},
	}))
	assert.Equal(t, 200, req.send().Code)

	// It should have updated the setting
	settings = checkSettings(t, req)
	assert.Contains(t, settings, Setting{
		ID:    1,
		Name:  "test",
		Value: "b",
	})

	// It should return 200 when doing a clientspecific setting update
	req.Method = "POST"
	req.PostForm.Set("json", testJSON([]Setting{
		Setting{
			Name:           "test",
			Value:          "c",
			ClientSpecific: true,
		},
	}))
	assert.Equal(t, 200, req.send().Code)

	// It should have updated the setting and should not return the
	// old non-specific version of it
	settings = checkSettings(t, req)
	assert.Contains(t, settings, Setting{
		ID:             2,
		Name:           "test",
		Value:          "c",
		ClientSpecific: true,
	})
	assert.NotContains(t, settings, Setting{
		ID:    1,
		Name:  "test",
		Value: "b",
	})

	// It should return the old non-specific setting to other clients
	req.Header.Set("Authorization", "evtest1")
	settings = checkSettings(t, req)
	assert.Contains(t, settings, Setting{
		ID:    1,
		Name:  "test",
		Value: "b",
	})
	assert.NotContains(t, settings, Setting{
		ID:             2,
		Name:           "test",
		Value:          "c",
		ClientSpecific: true,
	})
}