Skip to content

Commit

Permalink
feat(url): append base path, if present, via BaseURL. Add AddPath method
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Feb 17, 2020
1 parent c4d45b5 commit a168d94
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: go

go:
- "1.13"
- "1.12"
- "1.11"
- "1.10"
- "1.9"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# gentleman [![Build Status](https://travis-ci.org/h2non/gentleman.svg)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/badge/version-2.0.2-orange.svg?style=flat)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/gopkg.in/h2non/gentleman.v2?status.svg)](https://godoc.org/gopkg.in/h2non/gentleman.v2) [![Coverage Status](https://coveralls.io/repos/github/h2non/gentleman/badge.svg?branch=master)](https://coveralls.io/github/h2non/gentleman?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) [![Go Version](https://img.shields.io/badge/go-v2.0+-green.svg?style=flat)](https://github.com/h2non/gentleman)
# gentleman [![Build Status](https://travis-ci.org/h2non/gentleman.svg)](https://travis-ci.org/h2non/gentleman) [![GitHub release](https://img.shields.io/badge/version-2.0.4-orange.svg?style=flat)](https://github.com/h2non/gentleman/releases) [![GoDoc](https://godoc.org/gopkg.in/h2non/gentleman.v2?status.svg)](https://godoc.org/gopkg.in/h2non/gentleman.v2) [![Coverage Status](https://coveralls.io/repos/github/h2non/gentleman/badge.svg?branch=master)](https://coveralls.io/github/h2non/gentleman?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/h2non/gentleman)](https://goreportcard.com/report/github.com/h2non/gentleman) [![Go Version](https://img.shields.io/badge/go-v2.0+-green.svg?style=flat)](https://github.com/h2non/gentleman)

Full-featured, plugin-driven, middleware-oriented toolkit to easily create rich, versatile and composable HTTP clients in [Go](http://golang.org).

Expand Down
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ func (c *Client) Path(path string) *Client {
return c
}

// AddPath concatenates a path slice to the existent path in at client level.
func (c *Client) AddPath(path string) *Client {
c.Use(url.AddPath(path))
return c
}

// Param replaces a path param based on the given param name and value.
func (c *Client) Param(name, value string) *Client {
c.Use(url.Param(name, value))
Expand Down
10 changes: 10 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ func TestClientPath(t *testing.T) {
st.Expect(t, cli.Context.Request.URL.String(), "http://foo.com/foo/baz")
}

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

func TestClientPathParam(t *testing.T) {
url := "http://foo.com/bar/baz"
path := "/:foo/bar/:baz"
Expand Down
10 changes: 8 additions & 2 deletions plugins/url/url.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package url

import (
c "gopkg.in/h2non/gentleman.v2/context"
p "gopkg.in/h2non/gentleman.v2/plugin"
"net/url"
"regexp"
"strings"

c "gopkg.in/h2non/gentleman.v2/context"
p "gopkg.in/h2non/gentleman.v2/plugin"
)

// URL parses and defines a new URL in the outgoing request
Expand Down Expand Up @@ -33,6 +34,11 @@ func BaseURL(uri string) p.Plugin {

ctx.Request.URL.Scheme = u.Scheme
ctx.Request.URL.Host = u.Host

if u.Path != "" && u.Path != "/" {
ctx.Request.URL.Path = normalizePath(u.Path)
}

h.Next(ctx)
})
}
Expand Down
10 changes: 5 additions & 5 deletions plugins/url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func TestURL(t *testing.T) {

func TestBaseURL(t *testing.T) {
cases := []test{
{"http://foo", &url.URL{Host: "foo", Scheme: "http"}},
{"http://foo:123", &url.URL{Host: "foo:123", Scheme: "http"}},
{"https://127.0.0.1", &url.URL{Host: "127.0.0.1", Scheme: "https"}},
{"https://foo/bar", &url.URL{Host: "foo", Scheme: "https"}},
{"foo/bar", &url.URL{Host: "foo", Scheme: "http"}},
{"http://foo", &url.URL{Host: "foo", Scheme: "http", Path: ""}},
{"http://foo:123", &url.URL{Host: "foo:123", Scheme: "http", Path: ""}},
{"https://127.0.0.1", &url.URL{Host: "127.0.0.1", Scheme: "https", Path: ""}},
{"https://foo/bar", &url.URL{Host: "foo", Scheme: "https", Path: "/bar"}},
{"foo/bar", &url.URL{Host: "foo", Scheme: "http", Path: "/bar"}},
}

ctx := context.New()
Expand Down
2 changes: 1 addition & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (r *Request) Path(path string) *Request {
return r
}

// AddPath defines the request URL path to be used in the outgoing request.
// AddPath concatenates a path slice to the existent path in at request level.
func (r *Request) AddPath(path string) *Request {
r.Use(url.AddPath(path))
return r
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gentleman

// Version defines the package semantic version
const Version = "2.0.3"
const Version = "2.0.4"

0 comments on commit a168d94

Please sign in to comment.