Example #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")
}
Example #2
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")
}
Example #3
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("")
}
Example #4
0
func TestExecutesAListAction(t *testing.T) {
	f := func(context interface{}) Response { return Json(`{"spice":"mustflow"}`).Response }
	req := gspec.Request().Url("/v4/sessions.json").Req
	res := httptest.NewRecorder()
	router := newRouter(Configure().Route(R("LIST", "v4", "sessions", f)))
	router.ServeHTTP(res, req)
	assertResponse(t, res, 200, `{"spice":"mustflow"}`)
}
Example #5
0
func TestExecutesAnActionRegardlessOfCasing(t *testing.T) {
	f := func(context interface{}) Response { return Json(`{"name":"duncan"}`).Response }
	req := gspec.Request().Url("/v2/GHOLAS/123g.json").Req
	res := httptest.NewRecorder()
	router := newRouter(Configure().Route(R("GET", "v2", "gholas", f)))
	router.ServeHTTP(res, req)
	assertResponse(t, res, 200, `{"name":"duncan"}`)
}
Example #6
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)
}
Example #7
0
func TestHandlesANilResponse(t *testing.T) {
	f := func(context interface{}) Response { return nil }
	c := Configure().Route(R("GET", "v1", "worms", f))
	req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").Req
	res := httptest.NewRecorder()
	newRouter(c).ServeHTTP(res, req)
	assertResponse(t, res, 500, `{"error":"internal server error","code":500}`)
}
Example #8
0
func TestHandlesBodiesLargerThanAllowed(t *testing.T) {
	f := func(context *TestContext) Response { return Json("").Response }
	c := Configure().Route(R("GET", "v1", "worms", f).BodyFactory(testBodyFactory)).ContextFactory(testContextFactory).Dispatcher(testDispatcher).BodyPool(3, 1)
	req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").BodyString(`{"hello":"World"}`).Req
	res := httptest.NewRecorder()
	newRouter(c).ServeHTTP(res, req)
	assertResponse(t, res, 413, `{"error":"body too large","code":413}`)
}
Example #9
0
func TestExecutesAPutAction(t *testing.T) {
	f := func(context interface{}) Response { return Json(`{"name":"shaihulud"}`).Response }
	req := gspec.Request().Url("/v1/worms/22w.json").Method("PUT").Req
	res := httptest.NewRecorder()
	router := newRouter(Configure().Route(R("PUT", "v1", "worms", f)))
	router.ServeHTTP(res, req)
	assertResponse(t, res, 200, `{"name":"shaihulud"}`)
}
Example #10
0
func TestNoIpPresent(t *testing.T) {
	f := func(context *TestContext) Response {
		gspec.New(t).Expect(context.Ip).ToEqual("127.0.0.1")
		return Json("").Response
	}
	c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").Req
	newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
Example #11
0
func TestLoadCountryFromIpWhenNoXGeoIpHeader(t *testing.T) {
	f := func(context *TestContext) Response {
		gspec.New(t).Expect(context.Country).ToEqual("cn")
		return Json("").Response
	}
	c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Header("x-forwarded-for", "218.108.232.190").Url("/v1/worms/22w.json").Method("GET")
	newRouter(c).ServeHTTP(httptest.NewRecorder(), req.Req)
}
Example #12
0
func TestLoadIpFromXForwardedHeader(t *testing.T) {
	f := func(context *TestContext) Response {
		gspec.New(t).Expect(context.Ip).ToEqual("12.12.12.12")
		return Json("").Response
	}
	c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Header("x-forwarded-for", "12.12.12.12,13.13.13.13").Url("/v1/worms/22w.json").Method("GET")
	newRouter(c).ServeHTTP(httptest.NewRecorder(), req.Req)
}
Example #13
0
func TestParsesQueryStirngWithEmptyPairAtTheStart(t *testing.T) {
	spec := gspec.New(t)
	f := func(context *TestContext) Response {
		spec.Expect(context.Query["app"]).ToEqual("100004a")
		return Json("").Response
	}
	c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Url("/v1/worms/22w.json?&app=100004a").Method("GET").Req
	newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
Example #14
0
func TestHandlesMultipleQuestionMarksInQueryString(t *testing.T) {
	spec := gspec.New(t)
	f := func(context *TestContext) Response {
		spec.Expect(context.Query["app"]).ToEqual("100005a")
		return Json("").Response
	}
	c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Url("/v1/worms/22w.json?app=100002a?app=100005a").Method("GET").Req
	newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
Example #15
0
func TestParsesAQueryStringWithAMissingValue2(t *testing.T) {
	spec := gspec.New(t)
	f := func(context *TestContext) Response {
		spec.Expect(context.Query["b"]).ToEqual("")
		return Json("").Response
	}
	c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Url("/v1/worms/22w.json?b=").Method("GET").Req
	newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
Example #16
0
func TestStoresRawBody(t *testing.T) {
	spec := gspec.New(t)
	f := func(context *TestContext) Response {
		spec.Expect(string(context.RawBody)).ToEqual(`{"hello":"World"}`)
		return Json("").Response
	}
	c := Configure().LoadRawBody().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").BodyString(`{"hello":"World"}`).Req
	newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
Example #17
0
func TestDefaulsTheBodyValueWhenNoBodyIsPresent(t *testing.T) {
	spec := gspec.New(t)
	f := func(context *TestContext) Response {
		input := context.Body.(*TestBody)
		spec.Expect(input.Hello).ToEqual("")
		return Json("").Response
	}
	c := Configure().Route(R("GET", "v1", "worms", f).BodyFactory(testBodyFactory)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").Req
	newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
Example #18
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()
}
Example #19
0
func TestParsesQueryString(t *testing.T) {
	spec := gspec.New(t)
	f := func(context *TestContext) Response {
		spec.Expect(context.Query["app"]).ToEqual("6003")
		spec.Expect(context.Query["t"]).ToEqual("1 2")
		return Json("").Response
	}
	c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Url("/v1/worms/22w.json?APP=6003&t=1%202&").Req
	newRouter(c).ServeHTTP(httptest.NewRecorder(), req)
}
Example #20
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()
}
Example #21
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)
}
Example #22
0
func TestCreatingAndDispatchingThroughCustomTypes(t *testing.T) {
	spec := gspec.New(t)
	f := func(context *TestContext) Response {
		spec.Expect(context.Name).ToEqual("leto")
		spec.Expect(context.Params.Version).ToEqual("v1")
		spec.Expect(context.Params.Resource).ToEqual("worms")
		spec.Expect(context.Params.Id).ToEqual("22w")
		return Json(`{"name":"bigjohn"}`).Status(203).Response
	}
	c := Configure().Route(R("GET", "v1", "worms", f)).ContextFactory(testContextFactory).Dispatcher(testDispatcher)
	req := gspec.Request().Url("/v1/worms/22w.json").Method("GET").Req
	res := httptest.NewRecorder()
	newRouter(c).ServeHTTP(res, req)
	assertResponse(t, res, 203, `{"name":"bigjohn"}`)
}
Example #23
0
func TestRoutesToANestedResource(t *testing.T) {
	spec := gspec.New(t)
	f := func(context interface{}) Response {
		spec.Expect(context.(*BaseContext).Params.ParentResource).ToEqual("gholas")
		spec.Expect(context.(*BaseContext).Params.ParentId).ToEqual("123g")
		spec.Expect(context.(*BaseContext).Params.Resource).ToEqual("history")
		spec.Expect(context.(*BaseContext).Params.Id).ToEqual("")
		return Json(`{"name":"history"}`).Response
	}
	req := gspec.Request().Url("/v2/gholas/123g/history.json").Req
	res := httptest.NewRecorder()
	router := newRouter(Configure().Route(R("LIST", "v2", "gholas/history", f)))
	router.ServeHTTP(res, req)
	assertResponse(t, res, 200, `{"name":"history"}`)
}
Example #24
0
func TestNotFoundOnShortUrls(t *testing.T) {
	req := gspec.Request().Url("/x").Req
	res := httptest.NewRecorder()
	newRouter(Configure()).ServeHTTP(res, req)
	assertResponse(t, res, 404, `{"error":"not found","code":404}`)
}
Example #25
0
func TestSetsTheRightChuckWhenLargeRange(t *testing.T) {
	spec := gspec.New(t)
	c := NewContext(gspec.Request().Header("range", "bytes=3000000-9000000").Req)
	spec.Expect(c.Chunk).ToEqual(1)
}
Example #26
0
func TestTheContextsDirectories(t *testing.T) {
	spec := gspec.New(t)
	c := NewContext(gspec.Request().Url("/test.json").Req)
	spec.Expect(c.Dir).ToEqual("/tmp/01/0196/0196f4b7a30827487e3272e9499749e9/")
	spec.Expect(c.TempDir).ToEqual("/tmp/tmp/")
}
Example #27
0
func TestTheContextsFiles(t *testing.T) {
	spec := gspec.New(t)
	c := NewContext(gspec.Request().Url("/over9000.json").Req)
	spec.Expect(c.HeaderFile).ToEqual("/tmp/d2/d21f/d21fd0eba7a9f34038292d38c8ff4837/d21fd0eba7a9f34038292d38c8ff4837_0.hdr")
	spec.Expect(c.DataFile).ToEqual("/tmp/d2/d21f/d21fd0eba7a9f34038292d38c8ff4837/d21fd0eba7a9f34038292d38c8ff4837_0.dat")
}
Example #28
0
func TestNotFoundOnUnknownControllers(t *testing.T) {
	req := gspec.Request().Url("/v4/cats.json").Req
	res := httptest.NewRecorder()
	newRouter(Configure()).ServeHTTP(res, req)
	assertResponse(t, res, 404, `{"error":"not found","code":404}`)
}
Example #29
0
func TestTheContextsKey(t *testing.T) {
	spec := gspec.New(t)
	c := NewContext(gspec.Request().Header("range", "bytes=30000000-90000000").Url("/test.json?page=1").Req)
	spec.Expect(c.Key).ToEqual("0196f4b7a30827487e3272e9499749e9_14")
}
Example #30
0
func TestDefaultsToTheFirstChunk(t *testing.T) {
	spec := gspec.New(t)
	c := NewContext(gspec.Request().Req)
	spec.Expect(c.Chunk).ToEqual(0)
}