示例#1
0
func TestCreatesARequestWithTheCorrectHostAndUrl(t *testing.T) {
	spec := gspec.New(t)
	context := core.NewContext(gspec.Request().Url("/test.json").Req)
	req := newRequest(context, &core.Config{Upstream: "s3.viki.com"})
	spec.Expect(req.Host).ToEqual("s3.viki.com")
	spec.Expect(req.URL.Path).ToEqual("/test.json")
}
示例#2
0
func TestIgnoresTheRangeForNonRangeTypes(t *testing.T) {
	spec := gspec.New(t)
	context := core.NewContext(gspec.Request().Url("somefile.mp4").Req)
	context.Chunk = 3
	req := newRequest(context, new(core.Config))
	spec.Expect(req.Header.Get("range")).ToEqual("")
}
示例#3
0
func TestCreatesARequestWithTheCorrectRange(t *testing.T) {
	spec := gspec.New(t)
	context := core.NewContext(gspec.Request().Url("somefile.mp4").Req)
	context.Chunk = 3
	req := newRequest(context, &core.Config{RangedExtensions: map[string]bool{".mp4": true}})
	spec.Expect(req.Header.Get("range")).ToEqual("bytes=6291456-8388607")
}
示例#4
0
func TestLogsStatisticsToRedis(t *testing.T) {
	conn := pool.Get()
	defer cleanup(conn)
	spec := gspec.New(t)
	Run(core.NewContext(gspec.Request().Url("/something/funny.txt").Req), nil, core.NoopMiddleware)
	time.Sleep(time.Second * 1)
	spec.Expect(redis.Int(conn.Do("zscore", "hits", "/something/funny.txt"))).ToEqual(1)
}
示例#5
0
func TestDeletesTheFilesDirectory(t *testing.T) {
	spec := gspec.New(t)
	dir := setupTempFolder(spec)
	defer cleanupTempFolder()

	res := httptest.NewRecorder()
	Run(core.NewContext(gspec.Request().Method("purge").Req), res, nil)
	spec.Expect(res.Code).ToEqual(200)
	_, err := os.Stat(dir)
	spec.Expect(err).ToNotBeNil()
}
示例#6
0
func TestOnlyAllowsWhitelistedIpsToPurge(t *testing.T) {
	spec := gspec.New(t)
	dir := setupTempFolder(spec)
	defer cleanupTempFolder()

	res := httptest.NewRecorder()
	Run(core.NewContext(gspec.Request().Method("purge").RemoteAddr("23.33.24.55:4343").Req), res, nil)
	spec.Expect(res.Code).ToEqual(401)
	spec.Expect(res.Body.Len()).ToEqual(0)
	_, err := os.Stat(dir)
	spec.Expect(err).ToBeNil()
}
示例#7
0
func TestCallsTheNextMiddleware(t *testing.T) {
	spec := gspec.New(t)
	context := core.NewContext(gspec.Request().Req)
	res := httptest.NewRecorder()
	var called bool
	next := func(c *core.Context, r http.ResponseWriter) {
		spec.Expect(c).ToEqual(context)
		spec.Expect(r).ToEqual(res)
		called = true
	}
	Run(context, res, next)
	spec.Expect(called).ToEqual(true)
}
示例#8
0
func (h Handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
	core.Stats.Request()
	context := core.NewContext(req)
	head.Yield(context, res)
}