Skip to content

Commit

Permalink
feat(tests): add client test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Feb 25, 2016
1 parent b9d3efd commit 23507a7
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 44 deletions.
14 changes: 7 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *Client) URL(uri string) *Client {
// BaseURL defines the URL schema and host for client requests.
// Useful to define at client level the base URL used by client child requests.
func (c *Client) BaseURL(uri string) *Client {
c.Use(url.URL(uri))
c.Use(url.BaseURL(uri))
return c
}

Expand Down Expand Up @@ -151,17 +151,17 @@ func (c *Client) SetHeaders(fields map[string]string) *Client {
return c
}

// AddCookie sets a new cookie field bsaed on the given *http.Cookie struct
// without overwriting any existent header.
// AddCookie sets a new cookie field bsaed on the given http.Cookie struct
// without overwriting any existent cookie.
func (c *Client) AddCookie(cookie *http.Cookie) *Client {
c.Use(cookies.Add(cookie))
return c
}

// SetCookies sets a new cookie field by key and value.
// without overwriting any existent header.
func (c *Client) SetCookies(data map[string]string) *Client {
c.Use(cookies.SetMap(data))
// AddCookies sets a new cookie field based on a list of http.Cookie
// without overwriting any existent cookie.
func (c *Client) AddCookies(data []*http.Cookie) *Client {
c.Use(cookies.AddMultiple(data))
return c
}

Expand Down
191 changes: 170 additions & 21 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package gentleman

import (
"errors"
"fmt"
"github.com/nbio/st"
"gopkg.in/h2non/gentleman.v0/context"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -61,7 +63,6 @@ func TestClientRequestMiddleware(t *testing.T) {
defer ts.Close()

client := New()

client.UseRequest(func(ctx *context.Context, h context.Handler) {
ctx.Request.Header.Set("Client", "go")
h.Next(ctx)
Expand All @@ -75,18 +76,10 @@ func TestClientRequestMiddleware(t *testing.T) {
})

res, err := req.Do()
if err != nil {
t.Error(err)
}
if res.StatusCode != 200 {
t.Errorf("Invalid status code: %d", res.StatusCode)
}
if res.Header.Get("Server") != "go" {
t.Error("Invalid server header")
}
if res.Header.Get("Agent") != "gentleman" {
t.Error("Invalid agent header")
}
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
st.Expect(t, res.Header.Get("Server"), "go")
st.Expect(t, res.Header.Get("Agent"), "gentleman")
}

func TestClientRequestResponseMiddleware(t *testing.T) {
Expand All @@ -96,12 +89,10 @@ func TestClientRequestResponseMiddleware(t *testing.T) {
defer ts.Close()

client := New()

client.UseRequest(func(c *context.Context, h context.Handler) {
c.Request.Header.Set("Client", "go")
h.Next(c)
})

client.UseResponse(func(c *context.Context, h context.Handler) {
c.Response.Header.Set("Server", c.Request.Header.Get("Client"))
h.Next(c)
Expand All @@ -110,13 +101,171 @@ func TestClientRequestResponseMiddleware(t *testing.T) {
req := client.Request()
req.URL(ts.URL)
res, err := req.Do()
if err != nil {
t.Error(err)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 200)
st.Expect(t, res.Header.Get("Server"), "go")
}

func TestClientErrorMiddleware(t *testing.T) {
client := New()
client.UseRequest(func(c *context.Context, h context.Handler) {
c.Error = errors.New("foo error")
h.Next(c)
})
client.UseError(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")
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.Method, "POST")
}

func TestClientURL(t *testing.T) {
url := "http://foo.com"
cli := New()
cli.URL(url)
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.URL.String(), url)
}

func TestClientBaseURL(t *testing.T) {
url := "http://foo.com/bar/baz"
cli := New()
cli.BaseURL(url)
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.URL.String(), "http://foo.com")
}

func TestClientPath(t *testing.T) {
url := "http://foo.com/bar/baz"
path := "/foo/baz"
cli := New()
cli.URL(url)
cli.Path(path)
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.URL.String(), "http://foo.com/foo/baz")
}

func TestClientPathParam(t *testing.T) {
url := "http://foo.com/bar/baz"
path := "/:foo/bar/:baz"
cli := New()
cli.URL(url)
cli.Path(path)
cli.Param("foo", "baz")
cli.Param("baz", "foo")
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.URL.String(), "http://foo.com/baz/bar/foo")
}

func TestClientPathParams(t *testing.T) {
url := "http://foo.com/bar/baz"
path := "/:foo/bar/:baz"
cli := New()
cli.URL(url)
cli.Path(path)
cli.Params(map[string]string{"foo": "baz", "baz": "foo"})
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.URL.String(), "http://foo.com/baz/bar/foo")
}

func TestClientSetHeader(t *testing.T) {
cli := New()
cli.SetHeader("foo", "bar")
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.Header.Get("foo"), "bar")
}

func TestClientAddHeader(t *testing.T) {
cli := New()
cli.AddHeader("foo", "baz")
cli.AddHeader("foo", "bar")
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.Header.Get("foo"), "baz")
}

func TestClientSetHeaders(t *testing.T) {
cli := New()
cli.SetHeaders(map[string]string{"foo": "baz", "baz": "foo"})
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.Header.Get("foo"), "baz")
st.Expect(t, cli.Context.Request.Header.Get("baz"), "foo")
}

func TestClientAddCookie(t *testing.T) {
cli := New()
cookie := &http.Cookie{Name: "foo", Value: "bar"}
cli.AddCookie(cookie)
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.Header.Get("Cookie"), "foo=bar")
}

func TestClientAddCookies(t *testing.T) {
cli := New()
cookies := []*http.Cookie{{Name: "foo", Value: "bar"}}
cli.AddCookies(cookies)
cli.Middleware.Run("request", cli.Context)
st.Expect(t, cli.Context.Request.Header.Get("Cookie"), "foo=bar")
}

func TestClientCookieJar(t *testing.T) {
cli := New()
cli.CookieJar()
cli.Middleware.Run("request", cli.Context)
st.Reject(t, cli.Context.Client.Jar, nil)
}

func TestClientVerbMethods(t *testing.T) {
cli := New()
req := cli.Get()
cli.Middleware.Run("request", req.Context)
if req.Context.Request.Method != "GET" {
t.Errorf("Invalid request method: %s", req.Context.Request.Method)
}
if res.StatusCode != 200 {
t.Error("Invalid status code")

cli = New()
req = cli.Post()
cli.Middleware.Run("request", req.Context)
if req.Context.Request.Method != "POST" {
t.Errorf("Invalid request method: %s", req.Context.Request.Method)
}

cli = New()
req = cli.Put()
cli.Middleware.Run("request", req.Context)
if req.Context.Request.Method != "PUT" {
t.Errorf("Invalid request method: %s", req.Context.Request.Method)
}

cli = New()
req = cli.Delete()
cli.Middleware.Run("request", req.Context)
if req.Context.Request.Method != "DELETE" {
t.Errorf("Invalid request method: %s", req.Context.Request.Method)
}

cli = New()
req = cli.Patch()
cli.Middleware.Run("request", req.Context)
if req.Context.Request.Method != "PATCH" {
t.Errorf("Invalid request method: %s", req.Context.Request.Method)
}
if res.Header.Get("Server") != "go" {
t.Error("Invalid response header")

cli = New()
req = cli.Head()
cli.Middleware.Run("request", req.Context)
if req.Context.Request.Method != "HEAD" {
t.Errorf("Invalid request method: %s", req.Context.Request.Method)
}
}
10 changes: 10 additions & 0 deletions plugins/cookies/cookies.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ func SetMap(cookies map[string]string) p.Plugin {
})
}

// AddMultiple adds a list of cookies.
func AddMultiple(cookies []*http.Cookie) p.Plugin {
return p.NewRequestPlugin(func(ctx *c.Context, h c.Handler) {
for _, cookie := range cookies {
ctx.Request.AddCookie(cookie)
}
h.Next(ctx)
})
}

// Jar creates a cookie jar to store HTTP cookies when they are sent down.
func Jar() p.Plugin {
return p.NewRequestPlugin(func(ctx *c.Context, h c.Handler) {
Expand Down
9 changes: 9 additions & 0 deletions plugins/cookies/cookies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ func TestCookieSetMap(t *testing.T) {
st.Expect(t, ctx.Request.Header.Get("Cookie"), "foo=bar")
}

func TestCookieAddMultiple(t *testing.T) {
ctx := context.New()
fn := newHandler()
cookies := []*http.Cookie{{Name: "foo", Value: "bar"}}
AddMultiple(cookies).Request(ctx, fn.fn)
st.Expect(t, fn.called, true)
st.Expect(t, ctx.Request.Header.Get("Cookie"), "foo=bar")
}

func TestCookieJar(t *testing.T) {
ctx := context.New()
fn := newHandler()
Expand Down
31 changes: 15 additions & 16 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,25 @@ func (r *Request) Method(method string) *Request {
return r
}

// URL parses and defines the URL to be used in the HTTP request.
// URL parses and defines the URL to be used in the outgoing request.
func (r *Request) URL(uri string) *Request {
r.Use(url.URL(uri))
return r
}

// Path defines the request URL path to be used in the HTTP request.
// BaseURL parses the given URL and uses the URL schema and host in the outgoing request.
func (r *Request) BaseURL(uri string) *Request {
r.Use(url.BaseURL(uri))
return r
}

// Path defines the request URL path to be used in the outgoing request.
func (r *Request) Path(path string) *Request {
r.Use(url.Path(path))
return r
}

// AddPath defines the request URL path to be used in the HTTP request.
// AddPath defines the request URL path to be used in the outgoing request.
func (r *Request) AddPath(path string) *Request {
r.Use(url.AddPath(path))
return r
Expand Down Expand Up @@ -188,24 +194,17 @@ func (r *Request) SetHeaders(fields map[string]string) *Request {
return r
}

// SetCookie sets a new cookie field by key and value.
// If another cookie exists with the same key, it will be overwritten.
func (r *Request) SetCookie(key, value string) *Request {
r.Use(cookies.Set(key, value))
return r
}

// AddCookie sets a new cookie field bsaed on the given *http.Cookie struct
// without overwriting any existent header.
// AddCookie sets a new cookie field bsaed on the given http.Cookie struct
// without overwriting any existent cookie.
func (r *Request) AddCookie(cookie *http.Cookie) *Request {
r.Use(cookies.Add(cookie))
return r
}

// SetCookies sets a new cookie field by key and value.
// without overwriting any existent header.
func (r *Request) SetCookies(data map[string]string) *Request {
r.Use(cookies.SetMap(data))
// AddCookies sets a new cookie field based on a list of http.Cookie
// without overwriting any existent cookie.
func (r *Request) AddCookies(data []*http.Cookie) *Request {
r.Use(cookies.AddMultiple(data))
return r
}

Expand Down

0 comments on commit 23507a7

Please sign in to comment.