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)
}
func TestStoreGetCasts(t *testing.T) {
	casts := store.GetCasts()
	assert.NotNil(t, casts)
	assert.True(t, len(casts) > 0)
	assert.Equal(t, uint64(1), casts[0].ID)
	assert.Equal(t, "test.go", casts[0].URL)
}
func checkEvents(t *testing.T, req testReq) events {
	now := time.Now().Unix()
	res := req.send()
	assert.Equal(t, 200, res.Code)
	data := events{}
	err := json.Unmarshal(res.Body.Bytes(), &data)
	assert.Nil(t, err)
	assert.True(t, data.Timestamp >= now)
	return data
}
func TestMarshalling(t *testing.T) {
	SetConfigType("yaml")
	r := bytes.NewReader(yamlExample)

	marshalReader(r, v.config)
	assert.True(t, InConfig("name"))
	assert.False(t, InConfig("state"))
	assert.Equal(t, "steve", Get("name"))
	assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, Get("hobbies"))
	assert.Equal(t, map[interface{}]interface{}{"jacket": "leather", "trousers": "denim"}, Get("clothing"))
	assert.Equal(t, 35, Get("age"))
}
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())
}
Esempio n. 6
0
func TestCORS(t *testing.T) {
	mw := cors()
	req := testRequest(nil, "GET", "/", nil)
	res := httptest.NewRecorder()
	c := echo.NewContext(req.Request, echo.NewResponse(res), echo.New())
	called := false

	next := func(c *echo.Context) error {
		called = true
		return nil
	}

	// It calls the next middleware when no origin is set
	h := mw(next)
	h(c)
	assert.True(t, called)
	assert.Empty(t, res.Header().Get("Access-Control-Allow-Origin"))

	// It sets CORS headers and calls the next middleware when the origin is set
	req.Header.Set("Origin", "china")
	called = false
	h(c)
	assert.True(t, called)
	assert.NotEmpty(t, res.Header().Get("Access-Control-Allow-Origin"))

	// It sets CORS headers, ends the middleware chain and
	// returns 200 when receiving a preflight request
	req.Method = "OPTIONS"
	res = httptest.NewRecorder()
	c = echo.NewContext(req.Request, echo.NewResponse(res), echo.New())
	res.Code = 0
	called = false
	h(c)
	assert.False(t, called)
	assert.Equal(t, 200, res.Code)
	assert.NotEmpty(t, res.Header().Get("Access-Control-Allow-Origin"))
	assert.NotEmpty(t, res.Header().Get("Access-Control-Allow-Methods"))
	assert.NotEmpty(t, res.Header().Get("Access-Control-Allow-Headers"))
}
func TestReadBufConfig(t *testing.T) {
	v := New()
	v.SetConfigType("yaml")
	v.ReadConfig(bytes.NewBuffer(yamlExample))
	t.Log(v.AllKeys())

	assert.True(t, v.InConfig("name"))
	assert.False(t, v.InConfig("state"))
	assert.Equal(t, "steve", v.Get("name"))
	assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, v.Get("hobbies"))
	assert.Equal(t, map[interface{}]interface{}{"jacket": "leather", "trousers": "denim"}, v.Get("clothing"))
	assert.Equal(t, 35, v.Get("age"))
}
Esempio n. 8
0
func TestEcho(t *testing.T) {
	e := New()
	req, _ := http.NewRequest(GET, "/", nil)
	rec := httptest.NewRecorder()
	c := NewContext(req, NewResponse(rec), e)

	// Router
	assert.NotNil(t, e.Router())

	// Debug
	e.SetDebug(true)
	assert.True(t, e.Debug())

	// DefaultHTTPErrorHandler
	e.DefaultHTTPErrorHandler(errors.New("error"), c)
	assert.Equal(t, http.StatusInternalServerError, rec.Code)
}
func TestGetNewEpisodes(t *testing.T) {
	r := createRouter()

	req := testRequest(r, "GET", "/library/newepisodes?since=32503679999", nil)
	req.Header.Set("Authorization", "token")
	res := checkNewEpisodes(t, req)
	assert.Len(t, res.Episodes, 2)
	assert.Equal(t, Episode{
		ID:     2,
		CastID: 69,
	}, res.Episodes[0])
	assert.Equal(t, Episode{
		ID:     3,
		CastID: 69,
	}, res.Episodes[1])

	req.URL, _ = url.Parse("/library/newepisodes?since=32503680000")
	res = checkNewEpisodes(t, req)
	assert.Len(t, res.Episodes, 1)
	assert.Equal(t, Episode{
		ID:     3,
		CastID: 69,
	}, res.Episodes[0])

	req.URL, _ = url.Parse("/library/newepisodes?since=32503680001")
	res = checkNewEpisodes(t, req)
	assert.Len(t, res.Episodes, 0)

	req.URL, _ = url.Parse("/library/newepisodes")
	res = checkNewEpisodes(t, req)
	assert.True(t, len(res.Episodes) > 2)

	req.URL, _ = url.Parse("/library/newepisodes?since=what")
	resp := req.send()
	assert.Equal(t, 400, resp.Code)
}