func TestSetsTheBodyPoolSize(t *testing.T) { spec := gspec.New(t) c := Configure().BodyPool(10, 16) //allocate 1 extra byte so we know if the body is too large (or just right) spec.Expect(c.maxBodySize).ToEqual(int64(17)) spec.Expect(c.bodyPoolSize).ToEqual(10) }
func TestInvalidWhenStringLengthIsOutsideLen(t *testing.T) { spec := gspec.New(t) rule := Len(4, 6) for _, str := range []string{"123", "12", "1234567", "12345678"} { spec.Expect(rule.Verify(str)).ToEqual(false) } }
func TestSetsTheNotFoundResponse(t *testing.T) { spec := gspec.New(t) expected := Json("the res").Status(244).Response actual := Configure().NotFoundResponse(expected).notFound spec.Expect(actual.GetStatus()).ToEqual(244) spec.Expect(string(actual.GetBody())).ToEqual("the res") }
func TestMultipleRanges(t *testing.T) { spec := gspec.New(t) ranges := ParseRange("bytes=7-8,17-100") spec.Expect(len(ranges)).ToEqual(2) spec.Expect(ranges[0]).ToEqual(*&Range{7, 8}) spec.Expect(ranges[1]).ToEqual(*&Range{17, 100}) }
func TestInvalidWhenStringIsTooShort(t *testing.T) { spec := gspec.New(t) rule := MinLen(4) for _, str := range []string{"1", "12", "123"} { spec.Expect(rule.Verify(str)).ToEqual(false) } }
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") }
func TestGetReturnsTheItemWithEmptySecondaryKey(t *testing.T) { spec := gspec.New(t) c := New(Configure()) item := NewItem("SAMPLE BODY FOR TESTING") c.Set("the-p", "", item) spec.Expect(c.Get("the-p", "")).ToEqual(item) }
func TestValidWhenStringIsShorterThanMaxLen(t *testing.T) { spec := gspec.New(t) rule := MaxLen(4) for _, str := range []string{"1", "12", "123", "1234"} { spec.Expect(rule.Verify(str)).ToEqual(true) } }
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("") }
func TestLoadsAConfiguration(t *testing.T) { spec := gspec.New(t) loadConfig([]byte(`{"listen":"1.123.58.13:9001", "upstream":"its.over.net"}`)) config := GetConfig() spec.Expect(config.Listen).ToEqual("1.123.58.13:9001") spec.Expect(config.Upstream).ToEqual("its.over.net") }
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") }
func TestValidWhenStringIsLongerThanMinLen(t *testing.T) { spec := gspec.New(t) rule := MinLen(4) for _, str := range []string{"1234", "12345", "123456"} { spec.Expect(rule.Verify(str)).ToEqual(true) } }
func TestInvalidWhenStringIsTooLong(t *testing.T) { spec := gspec.New(t) rule := MaxLen(4) for _, str := range []string{"12345", "123456"} { spec.Expect(rule.Verify(str)).ToEqual(false) } }
func TestValidWhenStringLengthIsWithinLen(t *testing.T) { spec := gspec.New(t) rule := Len(4, 6) for _, str := range []string{"1234", "12345", "123456"} { spec.Expect(rule.Verify(str)).ToEqual(true) } }
func TestAddingMultipleSimpleRoutes(t *testing.T) { spec := gspec.New(t) c := Configure(). Route(R("GET", "v1", "gholas", "getgholas")). Route(R("LIST", "v1", "gholas", "listgholas")) spec.Expect(c.routes["v1"]["gholas"]["GET"].Action.(string)).ToEqual("getgholas") spec.Expect(c.routes["v1"]["gholas"]["LIST"].Action.(string)).ToEqual("listgholas") }
func TestComplexRange(t *testing.T) { spec := gspec.New(t) ranges := ParseRange("bytes=1-10,25-100,-10") spec.Expect(len(ranges)).ToEqual(3) spec.Expect(ranges[0]).ToEqual(*&Range{1, 10}) spec.Expect(ranges[1]).ToEqual(*&Range{25, 100}) spec.Expect(ranges[2]).ToEqual(*&Range{0, -10}) }
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) }
func TestPrunesTheList1(t *testing.T) { spec := gspec.New(t) l, one, two, three := sampleList() items := l.Prune(1) assertList(t, l, one, two) spec.Expect(items[0]).ToEqual(three) spec.Expect(len(items)).ToEqual(1) }
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) }
func TestSetsANewBucketItem(t *testing.T) { spec := gspec.New(t) bucket := testBucket() item, new := bucket.set("spice", TestValue("flow"), time.Minute) assertValue(t, item, "flow") item = bucket.get("spice") assertValue(t, item, "flow") spec.Expect(new).ToEqual(true) }
func TestSetsAnExistingItem(t *testing.T) { spec := gspec.New(t) bucket := testBucket() item, new := bucket.set("power", TestValue("9002"), time.Minute) assertValue(t, item, "9002") item = bucket.get("power") assertValue(t, item, "9002") spec.Expect(new).ToEqual(false) }
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) }
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) }
func TestCreatesAJsonResponse(t *testing.T) { spec := gspec.New(t) r := Json("this is the body").Status(9001).Response spec.Expect(string(r.GetBody())).ToEqual("this is the body") spec.Expect(r.GetStatus()).ToEqual(9001) spec.Expect(len(r.GetHeader())).ToEqual(2) spec.Expect(r.GetHeader().Get("Content-Type")).ToEqual("application/json; charset=utf-8") spec.Expect(r.GetHeader().Get("Content-Length")).ToEqual("16") }
func TestHandlesRemovalOfAnInvalidPrimaryKey(t *testing.T) { spec := gspec.New(t) c := New(Configure().Size(550)) item1 := NewItem("SAMPLE BODY FOR TESTING") c.Set("a", "1", item1) c.Remove("b") spec.Expect(c.Get("a", "1")).ToEqual(item1) spec.Expect(len(c.groups)).ToEqual(1) // b has to be removed since there'2 only 1 item, and we know, from the above, that it's a }
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) }
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) }
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) }
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) }
func TestGetPromotesTheItemToFrontOfTheCache(t *testing.T) { spec := gspec.New(t) c := New(Configure()) item1 := NewItem("SAMPLE BODY FOR TESTING") item2 := NewItem("SAMPLE BODY FOR TESTING") c.Set("a", "1", item1) c.Set("b", "1", item2) c.Get("a", "1") spec.Expect(c.list.head.item).ToEqual(item1) }