本文整理汇总了Golang中github.com/pierrre/imageserver.ServerFunc函数的典型用法代码示例。如果您正苦于以下问题:Golang ServerFunc函数的具体用法?Golang ServerFunc怎么用?Golang ServerFunc使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ServerFunc函数的16个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestServerErrorServer
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")
}
}
开发者ID:pierrre,项目名称:imageserver,代码行数:11,代码来源:source_test.go
示例2: TestServerErrorGroup
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")
}
}
开发者ID:cautio,项目名称:imageserver,代码行数:12,代码来源:groupcache_test.go
示例3: TestGetterErrorContextType
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")
}
}
开发者ID:pierrre,项目名称:imageserver,代码行数:13,代码来源:groupcache_test.go
示例4: TestServerErrorServer
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")
}
}
开发者ID:cautio,项目名称:imageserver,代码行数:13,代码来源:server_test.go
示例5: TestGetterErrorSink
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")
}
}
开发者ID:pierrre,项目名称:imageserver,代码行数:15,代码来源:groupcache_test.go
示例6: TestGetterErrorServer
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")
}
}
开发者ID:pierrre,项目名称:imageserver,代码行数:16,代码来源:groupcache_test.go
示例7: TestServer
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")
}
}
开发者ID:cautio,项目名称:imageserver,代码行数:17,代码来源:groupcache_test.go
示例8: TestGetterErrorImageMarshal
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")
}
}
开发者ID:pierrre,项目名称:imageserver,代码行数:18,代码来源:groupcache_test.go
示例9: benchmarkServer
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)))
}
开发者ID:cautio,项目名称:imageserver,代码行数:20,代码来源:groupcache_benchmark_test.go
示例10: TestServer
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)
}
}
开发者ID:pierrre,项目名称:imageserver,代码行数:20,代码来源:source_test.go
示例11: TestServerErrorCacheSet
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")
}
}
开发者ID:cautio,项目名称:imageserver,代码行数:20,代码来源:server_test.go
示例12: TestServer
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")
}
}
开发者ID:cautio,项目名称:imageserver,代码行数:20,代码来源:server_test.go
示例13: BenchmarkHandler
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)
}
}
开发者ID:cautio,项目名称:imageserver,代码行数:21,代码来源:handler_benchmark_test.go
示例14: TestGetter
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")
}
}
开发者ID:pierrre,项目名称:imageserver,代码行数:24,代码来源:groupcache_test.go
示例15: Get
// 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
}
开发者ID:cautio,项目名称:imageserver,代码行数:31,代码来源:testdata.go
示例16: TestHandler
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",
}
//.........这里部分代码省略.........
开发者ID:cautio,项目名称:imageserver,代码行数:101,代码来源:handler_test.go
注:本文中的github.com/pierrre/imageserver.ServerFunc函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论