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")
	}
}
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")
	}
}
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)
}
Exemple #4
0
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)
}
Exemple #5
0
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")
	}
}
Exemple #6
0
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)
}
Exemple #7
0
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")
}
Exemple #8
0
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, "")
}
Exemple #9
0
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")
}
Exemple #10
0
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)
}
Exemple #11
0
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)
}
Exemple #12
0
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)
}
Exemple #13
0
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")
}
Exemple #14
0
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")
}
Exemple #15
0
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)
	}
}
Exemple #16
0
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)
	}
}
Exemple #17
0
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)
	}
}
Exemple #18
0
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")
	}
}
Exemple #19
0
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)
	}
}
Exemple #20
0
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")
	}
}
Exemple #21
0
func TestMiddlewarePlugin(t *testing.T) {
	calls := 0
	fn := func(ctx *context.Context, h context.Handler) {
		calls++
		h.Next(ctx)
	}
	plugin := plugin.New()
	plugin.SetHandler("request", fn)
	plugin.SetHandler("response", fn)
	plugin.SetHandler("error", fn)

	mw := New()
	mw.Use(plugin)

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

	if calls != 3 {
		t.Errorf("Invalid middleware calls: %d", calls)
	}
}
Exemple #22
0
func TestAsyncMiddlewareCallChain(t *testing.T) {
	mw := New()

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

	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)
	}
}
Exemple #23
0
// New creates a new high level client entity
// able to perform HTTP requests.
func New() *Client {
	return &Client{
		Context:    context.New(),
		Middleware: middleware.New(),
	}
}
Exemple #24
0
func TestRedirectPlugin(t *testing.T) {
	ctx := context.New()
	fn := newHandler()
	Config(Options{}).Exec("request", ctx, fn.fn)
	st.Expect(t, fn.called, true)
}