• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang context.New函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中gopkg/in/h2non/gentleman/v1/context.New函数的典型用法代码示例。如果您正苦于以下问题:Golang New函数的具体用法?Golang New怎么用?Golang New使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了New函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: TestMiddlewareNewContextPassing

func TestMiddlewareNewContextPassing(t *testing.T) {
	mw := New()

	mw.UseRequest(func(c *context.Context, h context.Handler) {
		c.Set("foo", "bar")
		h.Next(c)
	})
	mw.UseRequest(func(c *context.Context, h context.Handler) {
		ctx := c.Clone()
		h.Next(ctx)
	})

	ctx := context.New()
	newCtx := mw.Run("request", ctx)

	if ctx.Error != nil {
		t.Error("Error must be empty")
	}
	if newCtx == ctx {
		t.Error("Context should not be equal")
	}
	if newCtx.Get("foo") == "foo" {
		t.Error("foo context value is not valid")
	}
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:25,代码来源:middleware_test.go


示例2: TestMiddlewareErrorWithInheritance

func TestMiddlewareErrorWithInheritance(t *testing.T) {
	parent := New()
	mw := New()
	mw.UseParent(parent)

	fn := func(c *context.Context, h context.Handler) {
		t.Error("Should not call the handler")
		h.Next(c)
	}
	stop := func(c *context.Context, h context.Handler) {
		h.Stop(c)
	}

	mw.UseRequest(stop)
	mw.UseRequest(fn)

	ctx := context.New()
	ctx = mw.Run("request", ctx)

	if ctx.Error != nil {
		t.Error("Error must be empty")
	}
	if !ctx.Stopped {
		t.Error("Call chain should be stopped")
	}
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:26,代码来源:middleware_test.go


示例3: TestDisableCompression

func TestDisableCompression(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	Disable().Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	transport := ctx.Client.Transport.(*http.Transport)
	st.Expect(t, transport.DisableCompression, true)
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:8,代码来源:compression_test.go


示例4: TestTimeout

func TestTimeout(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	Request(1000).Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	st.Expect(t, ctx.Error, nil)
	st.Expect(t, int(ctx.Client.Timeout), 1000)
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:8,代码来源:timeout_test.go


示例5: TestNewErrorPlugin

func TestNewErrorPlugin(t *testing.T) {
	called := false
	plugin := NewErrorPlugin(func(c *context.Context, h context.Handler) { h.Next(c) })
	plugin.Exec("error", context.New(), context.NewHandler(func(c *context.Context) { called = true }))
	if !called {
		t.Errorf("Handler not called")
	}
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:8,代码来源:plugin_test.go


示例6: TestSetTransport

func TestSetTransport(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	transport := &http.Transport{}
	Set(transport).Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	newTransport := ctx.Client.Transport.(*http.Transport)
	st.Expect(t, newTransport, transport)
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:9,代码来源:transport_test.go


示例7: TestQuerySet

func TestQuerySet(t *testing.T) {
	ctx := context.New()
	ctx.Request.URL.RawQuery = "baz=foo&foo=foo"
	fn := newHandler()

	Set("foo", "bar").Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	st.Expect(t, ctx.Request.URL.RawQuery, "baz=foo&foo=bar")
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:9,代码来源:query_test.go


示例8: TestQueryDelAll

func TestQueryDelAll(t *testing.T) {
	ctx := context.New()
	ctx.Request.URL.RawQuery = "foo=baz&foo=foo"
	fn := newHandler()

	DelAll().Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	st.Expect(t, ctx.Request.URL.RawQuery, "")
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:9,代码来源:query_test.go


示例9: TestQuerySetMap

func TestQuerySetMap(t *testing.T) {
	ctx := context.New()
	ctx.Request.URL.RawQuery = "baz=foo&foo=foo"
	fn := newHandler()
	params := map[string]string{"foo": "bar"}

	SetMap(params).Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	st.Expect(t, ctx.Request.URL.RawQuery, "baz=foo&foo=bar")
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:10,代码来源:query_test.go


示例10: TestAuthBasic

func TestAuthBasic(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	config := &tls.Config{InsecureSkipVerify: true}
	Config(config).Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)

	transport := ctx.Client.Transport.(*http.Transport)
	st.Expect(t, transport.TLSClientConfig, config)
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:10,代码来源:tls_test.go


示例11: TestTimeoutAll

func TestTimeoutAll(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	All(Timeouts{Request: 1000, Dial: 1000, TLS: 1000}).Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	st.Expect(t, ctx.Error, nil)
	transport := ctx.Client.Transport.(*http.Transport)
	st.Expect(t, int(ctx.Client.Timeout), 1000)
	st.Expect(t, int(transport.TLSHandshakeTimeout), 1000)
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:10,代码来源:timeout_test.go


示例12: TestTimeoutTLS

func TestTimeoutTLS(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	TLS(1000).Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
	st.Expect(t, ctx.Error, nil)

	transport := ctx.Client.Transport.(*http.Transport)
	st.Expect(t, int(transport.TLSHandshakeTimeout), 1000)
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:10,代码来源:timeout_test.go


示例13: TestMuxSimple

func TestMuxSimple(t *testing.T) {
	mx := New()
	mx.UseRequest(func(ctx *context.Context, h context.Handler) {
		ctx.Request.Header.Set("foo", "bar")
		h.Next(ctx)
	})
	ctx := context.New()
	mx.Run("request", ctx)
	st.Expect(t, ctx.Request.Header.Get("foo"), "bar")
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:10,代码来源:mux_test.go


示例14: TestMuxComposeIfMatches

func TestMuxComposeIfMatches(t *testing.T) {
	mx := New()
	mx.Use(If(Method("GET"), Host("foo.com")).UseRequest(func(ctx *context.Context, h context.Handler) {
		ctx.Request.Header.Set("foo", "bar")
		h.Next(ctx)
	}))
	ctx := context.New()
	ctx.Request.Method = "GET"
	ctx.Request.URL.Host = "foo.com"
	mx.Run("request", ctx)
	st.Expect(t, ctx.Request.Header.Get("foo"), "bar")
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:12,代码来源:compose_test.go


示例15: TestMiddlewareContextSharing

func TestMiddlewareContextSharing(t *testing.T) {
	fn := func(ctx *context.Context, h context.Handler) {
		ctx.Set("foo", ctx.GetString("foo")+"bar")
		h.Next(ctx)
	}

	mw := New()
	mw.UseRequest(fn)
	mw.UseRequest(fn)
	mw.UseRequest(fn)

	ctx := context.New()
	ctx = mw.Run("request", ctx)
	if val := ctx.GetString("foo"); val != "barbarbar" {
		t.Errorf("Invalid context value: %s", val)
	}
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:17,代码来源:middleware_test.go


示例16: TestMiddlewareErrorPassing

func TestMiddlewareErrorPassing(t *testing.T) {
	mw := New()
	mw.UseRequest(func(c *context.Context, h context.Handler) {
		h.Error(c, errors.New("foo"))
	})
	mw.UseError(func(c *context.Context, h context.Handler) {
		h.Error(c, errors.New("Error: "+c.Error.Error()))
	})

	ctx := mw.Run("request", context.New())
	if ctx.Error.Error() != "foo" {
		t.Errorf("Invalid error value: %s", ctx.Error)
	}

	ctx = mw.Run("error", ctx)
	if ctx.Error.Error() != "Error: foo" {
		t.Errorf("Invalid error value: %s", ctx.Error)
	}
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:19,代码来源:middleware_test.go


示例17: TestMultipleMiddlewareCallChain

func TestMultipleMiddlewareCallChain(t *testing.T) {
	mw := New()

	calls := 0
	fn := func(ctx *context.Context, h context.Handler) {
		calls++
		h.Next(ctx)
	}

	mw.UseRequest(fn)
	mw.UseRequest(fn)
	mw.UseRequest(fn)
	mw.UseRequest(fn)

	ctx := context.New()
	ctx = mw.Run("request", ctx)
	if ctx.Error != nil || calls != 4 {
		t.Errorf("Invalid middleware calls: %d => %s", calls, ctx.Error)
	}
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:20,代码来源:middleware_test.go


示例18: TestMiddlewareError

func TestMiddlewareError(t *testing.T) {
	mw := New()

	fn := func(c *context.Context, h context.Handler) {
		t.Error("Should not call the handler")
		h.Next(c)
	}
	fnError := func(c *context.Context, h context.Handler) {
		h.Error(c, errors.New("Error"))
	}

	mw.UseRequest(fnError)
	mw.UseRequest(fn)

	ctx := context.New()
	ctx = mw.Run("request", ctx)

	if ctx.Error == nil {
		t.Error("Error must exists")
	}
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:21,代码来源:middleware_test.go


示例19: TestPluginLayer

func TestPluginLayer(t *testing.T) {
	phase := ""
	fn := func(c *context.Context, h context.Handler) {
		phase = c.GetString("$phase")
		h.Next(c)
	}

	ctx := context.New()
	plugin := New()
	plugin.SetHandler("request", fn)
	plugin.SetHandler("response", fn)
	plugin.SetHandler("error", fn)

	calls := 0
	createHandler := func() context.Handler {
		return context.NewHandler(func(c *context.Context) { calls++ })
	}

	ctx.Set("$phase", "request")
	plugin.Exec("request", ctx, createHandler())
	if phase != "request" {
		t.Errorf("Invalid phase: %s", phase)
	}

	ctx.Set("$phase", "response")
	plugin.Exec("response", ctx, createHandler())
	if phase != "response" {
		t.Errorf("Invalid phase: %s", phase)
	}

	ctx.Set("$phase", "error")
	plugin.Exec("error", ctx, createHandler())
	if phase != "error" {
		t.Errorf("Invalid phase: %s", phase)
	}

	if calls != 3 {
		t.Errorf("Invalid number of calls: %d", calls)
	}
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:40,代码来源:plugin_test.go


示例20: TestMiddlewareInheritance

func TestMiddlewareInheritance(t *testing.T) {
	parent := New()
	child := New()
	child.UseParent(parent)
	mw := New()
	mw.UseParent(child)

	fn := func(c *context.Context, h context.Handler) {
		c.Set("foo", c.GetString("foo")+"bar")
		h.Next(c)
	}

	child.UseRequest(fn)
	parent.UseRequest(fn)
	mw.UseRequest(fn)

	ctx := context.New()
	ctx = mw.Run("request", ctx)

	if ctx.GetString("foo") != "barbarbar" {
		t.Error("Invalid context value")
	}
}
开发者ID:tomas-fp,项目名称:gentleman,代码行数:23,代码来源:middleware_test.go



注:本文中的gopkg/in/h2non/gentleman/v1/context.New函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang sockjs.Session类代码示例发布时间:2022-05-28
下一篇:
Golang v1.Transaction类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap