From e23732926f15d93e9d0c18f88d6c48dec083484d Mon Sep 17 00:00:00 2001 From: Tomas Aparicio Date: Thu, 25 Feb 2016 14:41:42 +0000 Subject: [PATCH] feat(tests) --- client_test.go | 18 ++++++++++ request_test.go | 95 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 94 insertions(+), 19 deletions(-) diff --git a/client_test.go b/client_test.go index 781af2d..d02224a 100644 --- a/client_test.go +++ b/client_test.go @@ -118,6 +118,24 @@ func TestClientErrorMiddleware(t *testing.T) { st.Expect(t, res.StatusCode, 0) } +func TestClientCustomPhaseMiddleware(t *testing.T) { + client := New() + client.UseRequest(func(c *context.Context, h context.Handler) { + c.Error = errors.New("foo error") + h.Next(c) + }) + client.UsePhase("error", func(c *context.Context, h context.Handler) { + c.Error = errors.New("error: " + c.Error.Error()) + h.Next(c) + }) + + req := client.Request() + res, err := req.Do() + st.Expect(t, err.Error(), "error: foo error") + st.Expect(t, res.Ok, false) + st.Expect(t, res.StatusCode, 0) +} + func TestClientMethod(t *testing.T) { cli := New() cli.Method("POST") diff --git a/request_test.go b/request_test.go index ccf9a6b..f6ba31c 100644 --- a/request_test.go +++ b/request_test.go @@ -36,15 +36,35 @@ func TestRequest(t *testing.T) { }) res, err := req.Send() - if err != nil { - t.Errorf("Request error: %s", err) - } - if res.RawRequest.URL == nil { - t.Error("Invalid context") - } - if res.StatusCode != 200 { - t.Errorf("Invalid response status: %d", res.StatusCode) - } + st.Expect(t, err, nil) + st.Reject(t, res.RawRequest.URL, nil) + st.Expect(t, res.StatusCode, 200) +} + +func TestRequestAlreadyDispatched(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "Hello, world") + })) + defer ts.Close() + + req := NewRequest() + req.UseRequest(func(ctx *context.Context, h context.Handler) { + h.Next(ctx) + }) + + req.UseRequest(func(ctx *context.Context, h context.Handler) { + u, _ := url.Parse(ts.URL) + ctx.Request.URL = u + h.Next(ctx) + }) + + res, err := req.Send() + st.Expect(t, err, nil) + st.Reject(t, res.RawRequest.URL, nil) + st.Expect(t, res.StatusCode, 200) + + res, err = req.Send() + st.Reject(t, err, nil) } func TestMiddlewareErrorInjectionAndInterception(t *testing.T) { @@ -70,15 +90,9 @@ func TestMiddlewareErrorInjectionAndInterception(t *testing.T) { }) res, err := req.Send() - if err != nil { - t.Errorf("Request error: %s", err) - } - if res.RawRequest.URL == nil { - t.Error("Invalid context") - } - if res.StatusCode != 200 { - t.Errorf("Invalid response status: %d", res.StatusCode) - } + st.Expect(t, err, nil) + st.Reject(t, res.RawRequest.URL, nil) + st.Expect(t, res.StatusCode, 200) } func TestRequestResponseMiddleware(t *testing.T) { @@ -104,6 +118,29 @@ func TestRequestResponseMiddleware(t *testing.T) { st.Expect(t, res.Header.Get("Server"), "go") } +func TestRequestCustomPhaseMiddleware(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "Hello, world") + })) + defer ts.Close() + + req := NewRequest() + req.URL(ts.URL) + req.UsePhase("request", func(c *context.Context, h context.Handler) { + c.Request.Header.Set("Client", "go") + h.Next(c) + }) + req.UsePhase("response", func(c *context.Context, h context.Handler) { + c.Response.Header.Set("Server", c.Request.Header.Get("Client")) + h.Next(c) + }) + + res, err := req.Do() + st.Expect(t, err, nil) + st.Expect(t, res.StatusCode, 200) + st.Expect(t, res.Header.Get("Server"), "go") +} + func TestRequestOverwriteTargetURL(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, world") @@ -193,7 +230,7 @@ func TestRequestTimeout(t *testing.T) { req := NewRequest().URL(ts.URL) req.UseRequest(func(ctx *context.Context, h context.Handler) { - ctx.Client.Timeout = 100 * time.Millisecond + ctx.Client.Timeout = 50 * time.Millisecond h.Next(ctx) }) @@ -260,6 +297,16 @@ func TestRequestPath(t *testing.T) { st.Expect(t, req.Context.Request.URL.String(), "http://foo.com/foo/baz") } +func TestRequestAddPath(t *testing.T) { + url := "http://foo.com/bar/baz" + path := "/foo/baz" + req := NewRequest() + req.URL(url) + req.AddPath(path) + req.Middleware.Run("request", req.Context) + st.Expect(t, req.Context.Request.URL.String(), "http://foo.com/bar/baz/foo/baz") +} + func TestRequestPathParam(t *testing.T) { url := "http://foo.com/bar/baz" path := "/:foo/bar/:baz" @@ -446,3 +493,13 @@ func TestRequestFiles(t *testing.T) { st.Expect(t, strings.Contains(string(body), "content1"), true) st.Expect(t, strings.Contains(string(body), "content2"), true) } + +func TestRequestClone(t *testing.T) { + req1 := NewRequest() + req1.UseRequest(func(c *context.Context, h context.Handler) {}) + req1.Context.Set("foo", "bar") + req2 := req1.Clone() + st.Expect(t, req1 != req2, true) + st.Expect(t, req2.Context.GetString("foo"), req1.Context.GetString("foo")) + st.Expect(t, len(req2.Middleware.GetStack()), 1) +}