func TestRemotePrecedence(t *testing.T) {
	initJSON()

	remote := bytes.NewReader(remoteExample)
	assert.Equal(t, "0001", Get("id"))
	marshalReader(remote, v.kvstore)
	assert.Equal(t, "0001", Get("id"))
	assert.NotEqual(t, "cronut", Get("type"))
	assert.Equal(t, "remote", Get("newkey"))
	Set("newkey", "newvalue")
	assert.NotEqual(t, "remote", Get("newkey"))
	assert.Equal(t, "newvalue", Get("newkey"))
	Set("newkey", "remote")
}
func TestLogin(t *testing.T) {
	r := createRouter()

	// It should return 400 if required params are missing
	req := testRequest(r, "POST", "/account/login", nil)
	assert.Equal(t, 400, req.send().Code)

	// It should return 200 and the correct token for an existing UUID
	req.PostForm.Set("username", "test")
	req.PostForm.Set("password", "pass")
	req.PostForm.Set("uuid", "real_unique")
	req.PostForm.Set("clientname", "Castcloud")
	res := req.send()
	assert.Equal(t, 200, res.Code)
	resb := token{}
	json.Unmarshal(res.Body.Bytes(), &resb)
	assert.Equal(t, "token", resb.Token)

	// It should return 200 and a new token if the UUID is new
	req.PostForm.Set("uuid", "nope")
	res = req.send()
	assert.Equal(t, 200, res.Code)
	json.Unmarshal(res.Body.Bytes(), &resb)
	assert.True(t, len(resb.Token) > 0)
	assert.NotEqual(t, "token", resb.Token)

	// It should return 401 if the user doesnt exist
	req.PostForm.Set("username", "bob")
	assert.Equal(t, 401, req.send().Code)

	// It should return 401 if the password is wrong
	req.PostForm.Set("username", "test")
	req.PostForm.Set("password", "not_correct")
	assert.Equal(t, 401, req.send().Code)
}
Esempio n. 3
0
func TestLevels(t *testing.T) {
	SetStdoutThreshold(LevelError)
	assert.Equal(t, StdoutThreshold(), LevelError)
	SetLogThreshold(LevelCritical)
	assert.Equal(t, LogThreshold(), LevelCritical)
	assert.NotEqual(t, StdoutThreshold(), LevelCritical)
	SetStdoutThreshold(LevelWarn)
	assert.Equal(t, StdoutThreshold(), LevelWarn)
}
func TestResponse(t *testing.T) {
	w := httptest.NewRecorder()
	r := NewResponse(w)

	// SetWriter
	r.SetWriter(w)

	// Writer
	assert.Equal(t, w, r.Writer())

	// Header
	assert.NotNil(t, r.Header())

	// WriteHeader
	r.WriteHeader(http.StatusOK)
	assert.Equal(t, http.StatusOK, r.status)

	// Committed
	assert.True(t, r.committed)

	// Already committed
	r.WriteHeader(http.StatusTeapot)
	assert.NotEqual(t, http.StatusTeapot, r.Status())

	// Status
	r.status = http.StatusOK
	assert.Equal(t, http.StatusOK, r.Status())

	// Write
	s := "echo"
	_, err := r.Write([]byte(s))
	assert.NoError(t, err)

	// Flush
	r.Flush()

	// Size
	assert.EqualValues(t, len(s), r.Size())

	// Hijack
	assert.Panics(t, func() {
		r.Hijack()
	})

	// CloseNotify
	assert.Panics(t, func() {
		r.CloseNotify()
	})

	// reset
	r.reset(httptest.NewRecorder())
}
func TestDefaultPost(t *testing.T) {
	assert.NotEqual(t, "NYC", Get("state"))
	SetDefault("state", "NYC")
	assert.Equal(t, "NYC", Get("state"))
}