Skip to content

Commit

Permalink
feat(tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Feb 25, 2016
1 parent c7f8cf7 commit e237329
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 19 deletions.
18 changes: 18 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
95 changes: 76 additions & 19 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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")
Expand Down Expand Up @@ -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)
})

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

0 comments on commit e237329

Please sign in to comment.