当前位置: 首页>>代码示例>>Golang>>正文


Golang context.New函数代码示例

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


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


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