func TestServerErrorServer(t *testing.T) { srv := &Server{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return nil, fmt.Errorf("error") }), } _, err := srv.Get(imageserver.Params{Param: "source"}) if err == nil { t.Fatal("no error") } }
func TestServerErrorGroup(t *testing.T) { srv := newTestServer( imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return nil, fmt.Errorf("error") }), imageserver_cache.StringKeyGenerator("test"), ) _, err := srv.Get(imageserver.Params{}) if err == nil { t.Fatal("no error") } }
func TestGetterErrorContextType(t *testing.T) { var data []byte dest := groupcache.AllocatingByteSliceSink(&data) gt := &Getter{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return testdata.Medium, nil }), } err := gt.Get("invalid", "foo", dest) if err == nil { t.Fatal("no error") } }
func TestServerErrorServer(t *testing.T) { s := &Server{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return nil, fmt.Errorf("error") }), Cache: cachetest.NewMapCache(), KeyGenerator: StringKeyGenerator("test"), } _, err := s.Get(imageserver.Params{}) if err == nil { t.Fatal("no error") } }
func TestGetterErrorSink(t *testing.T) { ctx := &Context{ Params: imageserver.Params{}, } dest := groupcache.AllocatingByteSliceSink(nil) gt := &Getter{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return testdata.Medium, nil }), } err := gt.Get(ctx, "foo", dest) if err == nil { t.Fatal("no error") } }
func TestGetterErrorServer(t *testing.T) { ctx := &Context{ Params: imageserver.Params{}, } var data []byte dest := groupcache.AllocatingByteSliceSink(&data) gt := &Getter{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return nil, fmt.Errorf("error") }), } err := gt.Get(ctx, "foo", dest) if err == nil { t.Fatal("no error") } }
func TestServer(t *testing.T) { srv := newTestServer( imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return testdata.Medium, nil }), imageserver_cache.StringKeyGenerator("test"), ) im, err := srv.Get(imageserver.Params{ imageserver.SourceParam: testdata.MediumFileName, }) if err != nil { t.Fatal(err) } if !imageserver.ImageEqual(im, testdata.Medium) { t.Fatal("not equal") } }
func TestGetterErrorImageMarshal(t *testing.T) { ctx := &Context{ Params: imageserver.Params{}, } var data []byte dest := groupcache.AllocatingByteSliceSink(&data) gt := &Getter{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return &imageserver.Image{ Format: strings.Repeat("a", imageserver.ImageFormatMaxLen+1), }, nil }), } err := gt.Get(ctx, "foo", dest) if err == nil { t.Fatal("no error") } }
func benchmarkServer(b *testing.B, im *imageserver.Image, parallelism int) { srv := newTestServer( imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return im, nil }), imageserver_cache.StringKeyGenerator("test"), ) params := imageserver.Params{} b.SetParallelism(parallelism) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { _, err := srv.Get(params) if err != nil { b.Fatal(err) } } }) b.SetBytes(int64(len(im.Data))) }
func TestServer(t *testing.T) { srv := &Server{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { if !params.Has(Param) { t.Fatal("no source param") } if params.Has("foo") { t.Fatal("unexpected param") } return &imageserver.Image{}, nil }), } _, err := srv.Get(imageserver.Params{ Param: "source", "foo": "bar", }) if err != nil { t.Fatal(err) } }
func TestServerErrorCacheSet(t *testing.T) { s := &Server{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return testdata.Medium, nil }), Cache: &Func{ GetFunc: func(key string, params imageserver.Params) (*imageserver.Image, error) { return nil, nil }, SetFunc: func(key string, image *imageserver.Image, params imageserver.Params) error { return fmt.Errorf("error") }, }, KeyGenerator: StringKeyGenerator("test"), } _, err := s.Get(imageserver.Params{}) if err == nil { t.Fatal("no error") } }
func TestServer(t *testing.T) { s := &Server{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return testdata.Medium, nil }), Cache: cachetest.NewMapCache(), KeyGenerator: StringKeyGenerator("test"), } image1, err := s.Get(imageserver.Params{}) if err != nil { t.Fatal(err) } image2, err := s.Get(imageserver.Params{}) if err != nil { t.Fatal(err) } if !imageserver.ImageEqual(image1, image2) { t.Fatal("not equal") } }
func BenchmarkHandler(b *testing.B) { h := &Handler{ Parser: &nopParser{}, Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return testdata.Medium, nil }), ETagFunc: func(params imageserver.Params) string { return "foo" }, } rw := &nopResponseWriter{} req, err := http.NewRequest("GET", "http://localhost", nil) if err != nil { b.Fatal(err) } req.Header.Set("If-None-Match", "\"bar\"") b.ResetTimer() for i := 0; i < b.N; i++ { h.ServeHTTP(rw, req) } }
func TestGetter(t *testing.T) { ctx := &Context{ Params: imageserver.Params{}, } var data []byte dest := groupcache.AllocatingByteSliceSink(&data) gt := &Getter{ Server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return testdata.Medium, nil }), } err := gt.Get(ctx, "foo", dest) if err != nil { t.Fatal(err) } im := new(imageserver.Image) err = im.UnmarshalBinary(data) if err != nil { t.Fatal(err) } if !imageserver.ImageEqual(im, testdata.Medium) { t.Fatal("not equal") } }
// RandomFileName is the file name of Random. RandomFileName = "random.png" // Random is a random Image. Random = loadImage(RandomFileName, "png") // InvalidFileName is the file name of Invalid. InvalidFileName = "invalid.jpg" // Invalid is an invalid Image. Invalid = loadImage(InvalidFileName, "invalid") // Server is an Image Server that uses filename as source. Server = imageserver.Server(imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { source, err := params.GetString(imageserver.SourceParam) if err != nil { return nil, err } im, err := Get(source) if err != nil { return nil, &imageserver.ParamError{Param: imageserver.SourceParam, Message: err.Error()} } return im, nil })) ) // Get returns an Image for a name. func Get(name string) (*imageserver.Image, error) { im, ok := Images[name] if !ok { return nil, fmt.Errorf("unknown image \"%s\"", name) } return im, nil }
func TestHandler(t *testing.T) { type TC struct { hasETagFunc bool server imageserver.Server method string url string header map[string]string responseWriter http.ResponseWriter expectedStatusCode int expectedHeader map[string]string expectErrorFuncCalled bool } for _, tc := range []TC{ { hasETagFunc: true, url: "http://localhost?source=medium.jpg", expectedStatusCode: http.StatusOK, expectedHeader: map[string]string{ "Etag": fmt.Sprintf("\"%s\"", NewParamsHashETagFunc(sha256.New)(imageserver.Params{ imageserver.SourceParam: testdata.MediumFileName, })), "Content-Type": fmt.Sprintf("image/%s", testdata.Medium.Format), "Content-Length": fmt.Sprint(len(testdata.Medium.Data)), }, }, { method: "HEAD", url: "http://localhost?source=medium.jpg", expectedStatusCode: http.StatusOK, }, { url: "http://localhost?source=medium.jpg", header: map[string]string{ "If-None-Match": fmt.Sprintf("\"%s\"", NewParamsHashETagFunc(sha256.New)(imageserver.Params{ imageserver.SourceParam: testdata.MediumFileName, })), }, expectedStatusCode: http.StatusOK, }, { hasETagFunc: true, url: "http://localhost?source=medium.jpg", header: map[string]string{ "If-None-Match": fmt.Sprintf("\"%s\"", NewParamsHashETagFunc(sha256.New)(imageserver.Params{ imageserver.SourceParam: testdata.MediumFileName, })), }, expectedStatusCode: http.StatusNotModified, }, { hasETagFunc: true, url: "http://localhost?source=medium.jpg", header: map[string]string{ "If-None-Match": "foobar", }, expectedStatusCode: http.StatusOK, }, { hasETagFunc: true, url: "http://localhost?source=medium.jpg", header: map[string]string{ "If-None-Match": "\"foobar\"", }, expectedStatusCode: http.StatusOK, }, { method: "POST", url: "http://localhost?source=medium.jpg", expectedStatusCode: http.StatusMethodNotAllowed, }, { url: "http://localhost?error=foo", expectedStatusCode: http.StatusBadRequest, }, { url: "http://localhost", server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return nil, &imageserver.ParamError{ Param: imageserver.SourceParam, Message: "error", } }), expectedStatusCode: http.StatusBadRequest, }, { url: "http://localhost", server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return nil, &imageserver.ParamError{ Param: "foobar", Message: "error", } }), expectedStatusCode: http.StatusBadRequest, }, { url: "http://localhost", server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return nil, &imageserver.ImageError{ Message: "error", } }), expectedStatusCode: http.StatusBadRequest, }, { url: "http://localhost", server: imageserver.ServerFunc(func(params imageserver.Params) (*imageserver.Image, error) { return nil, fmt.Errorf("error") }), expectedStatusCode: http.StatusInternalServerError, expectErrorFuncCalled: true, }, } { func() { defer func() { if t.Failed() { t.Logf("%#v", tc) } }() errorFuncCalled := false h := &Handler{ Parser: ListParser{ &SourceParser{}, &testErrorParser{}, }, ErrorFunc: func(err error, req *http.Request) { errorFuncCalled = true }, } if tc.server != nil { h.Server = tc.server } else { h.Server = testdata.Server } if tc.hasETagFunc { h.ETagFunc = NewParamsHashETagFunc(sha256.New) } rw := tc.responseWriter if rw == nil { rw = httptest.NewRecorder() } met := tc.method if met == "" { met = "GET" } req, err := http.NewRequest(met, tc.url, nil) if err != nil { t.Fatal(err) } if tc.header != nil { for hd, val := range tc.header { req.Header.Set(hd, val) } } h.ServeHTTP(rw, req) if rw, ok := rw.(*httptest.ResponseRecorder); ok { rw.Flush() if tc.expectedStatusCode != 0 && rw.Code != tc.expectedStatusCode { t.Fatalf("unexpected statud code: got %d, want %d", rw.Code, tc.expectedStatusCode) } } if tc.expectedHeader != nil { for hd, want := range tc.expectedHeader { got := rw.Header().Get(hd) if got != want { t.Fatalf("unexpected value for header \"%s\": got \"%s\", want \"%s\"", hd, got, want) } } } if tc.expectErrorFuncCalled && !errorFuncCalled { t.Fatal("ErrorFunc not called") } }() } }