Skip to content

Commit

Permalink
add Rule.IsProxy()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 30, 2017
1 parent fccdb43 commit fa26621
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 131 deletions.
11 changes: 11 additions & 0 deletions redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package redirects
import (
"bufio"
"io"
"net/url"
"strconv"
"strings"

Expand Down Expand Up @@ -41,6 +42,16 @@ type Rule struct {
Params Params
}

// IsProxy returns true if it's a proxy rule (aka contains a hostname).
func (r *Rule) IsProxy() bool {
u, err := url.Parse(r.To)
if err != nil {
return false
}

return u.Host != ""
}

// Must parse utility.
func Must(v []Rule, err error) []Rule {
if err != nil {
Expand Down
146 changes: 146 additions & 0 deletions redirects_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package redirects_test

import (
"encoding/json"
"os"

"github.com/tj/go-redirects"
)

func Example() {
h := redirects.Must(redirects.ParseString(`
# Implicit 301 redirects
/home /
/blog/my-post.php /blog/my-post
/news /blog
/google https://www.google.com
# Redirect with a 301
/home / 301
# Redirect with a 302
/my-redirect / 302
# Rewrite a path
/pass-through /index.html 200
# Show a custom 404 for this path
/ecommerce /store-closed 404
# Single page app rewrite
/* /index.html 200
# Proxying
/api/* https://api.example.com/:splat 200
# Forcing
/app/* /app/index.html 200!
# Params
/ /something 302 foo=bar
/ /something 302 foo=bar bar=baz
`))

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(h)
// Output:
// [
// {
// "From": "/home",
// "To": "/",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/blog/my-post.php",
// "To": "/blog/my-post",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/news",
// "To": "/blog",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/google",
// "To": "https://www.google.com",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/home",
// "To": "/",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/my-redirect",
// "To": "/",
// "Status": 302,
// "Force": false,
// "Params": null
// },
// {
// "From": "/pass-through",
// "To": "/index.html",
// "Status": 200,
// "Force": false,
// "Params": null
// },
// {
// "From": "/ecommerce",
// "To": "/store-closed",
// "Status": 404,
// "Force": false,
// "Params": null
// },
// {
// "From": "/*",
// "To": "/index.html",
// "Status": 200,
// "Force": false,
// "Params": null
// },
// {
// "From": "/api/*",
// "To": "https://api.example.com/:splat",
// "Status": 200,
// "Force": false,
// "Params": null
// },
// {
// "From": "/app/*",
// "To": "/app/index.html",
// "Status": 200,
// "Force": true,
// "Params": null
// },
// {
// "From": "/",
// "To": "/something",
// "Status": 302,
// "Force": false,
// "Params": {
// "foo": "bar"
// }
// },
// {
// "From": "/",
// "To": "/something",
// "Status": 302,
// "Force": false,
// "Params": {
// "bar": "baz",
// "foo": "bar"
// }
// }
// ]
}
166 changes: 35 additions & 131 deletions redirects_test.go
Original file line number Diff line number Diff line change
@@ -1,146 +1,50 @@
package redirects_test

import (
"encoding/json"
"os"
"testing"

"github.com/tj/assert"
"github.com/tj/go-redirects"
)

func Example() {
h := redirects.Must(redirects.ParseString(`
# Implicit 301 redirects
/home /
/blog/my-post.php /blog/my-post
/news /blog
/google https://www.google.com
func TestParams_Has(t *testing.T) {
p := redirects.Params{
"foo": true,
"bar": "baz",
}

# Redirect with a 301
/home / 301
# Redirect with a 302
/my-redirect / 302
# Rewrite a path
/pass-through /index.html 200
assert.True(t, p.Has("foo"))
assert.True(t, p.Has("bar"))
assert.False(t, p.Has("baz"))
}

# Show a custom 404 for this path
/ecommerce /store-closed 404
func TestParams_Get(t *testing.T) {
p := redirects.Params{
"foo": true,
"bar": "baz",
}

# Single page app rewrite
/* /index.html 200
assert.Equal(t, true, p.Get("foo"))
assert.Equal(t, "baz", p.Get("bar"))
assert.Equal(t, nil, p.Get("baz"))
}

# Proxying
/api/* https://api.example.com/:splat 200
func TestRule_IsProxy(t *testing.T) {
t.Run("without host", func(t *testing.T) {
r := redirects.Rule{
From: "/blog",
To: "/blog/engineering",
}

# Forcing
/app/* /app/index.html 200!
assert.False(t, r.IsProxy())
})

# Params
/ /something 302 foo=bar
/ /something 302 foo=bar bar=baz
`))
t.Run("with host", func(t *testing.T) {
r := redirects.Rule{
From: "/blog",
To: "https://blog.apex.sh",
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(h)
// Output:
// [
// {
// "From": "/home",
// "To": "/",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/blog/my-post.php",
// "To": "/blog/my-post",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/news",
// "To": "/blog",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/google",
// "To": "https://www.google.com",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/home",
// "To": "/",
// "Status": 301,
// "Force": false,
// "Params": null
// },
// {
// "From": "/my-redirect",
// "To": "/",
// "Status": 302,
// "Force": false,
// "Params": null
// },
// {
// "From": "/pass-through",
// "To": "/index.html",
// "Status": 200,
// "Force": false,
// "Params": null
// },
// {
// "From": "/ecommerce",
// "To": "/store-closed",
// "Status": 404,
// "Force": false,
// "Params": null
// },
// {
// "From": "/*",
// "To": "/index.html",
// "Status": 200,
// "Force": false,
// "Params": null
// },
// {
// "From": "/api/*",
// "To": "https://api.example.com/:splat",
// "Status": 200,
// "Force": false,
// "Params": null
// },
// {
// "From": "/app/*",
// "To": "/app/index.html",
// "Status": 200,
// "Force": true,
// "Params": null
// },
// {
// "From": "/",
// "To": "/something",
// "Status": 302,
// "Force": false,
// "Params": {
// "foo": "bar"
// }
// },
// {
// "From": "/",
// "To": "/something",
// "Status": 302,
// "Force": false,
// "Params": {
// "bar": "baz",
// "foo": "bar"
// }
// }
// ]
assert.True(t, r.IsProxy())
})
}

0 comments on commit fa26621

Please sign in to comment.