本文整理汇总了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")
}
}
示例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")
}
}
示例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)
}
示例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)
}
示例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")
}
}
示例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)
}
示例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")
}
示例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, "")
}
示例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")
}
示例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)
}
示例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)
}
示例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)
}
示例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")
}
示例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")
}
示例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)
}
}