Skip to content

Commit

Permalink
add Rule.IsRewrite()
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 30, 2017
1 parent fa26621 commit 16a0635
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type Rule struct {
Params Params
}

// IsRewrite returns true if the rule represents a rewrite (status 200).
func (r *Rule) IsRewrite() bool {
return r.Status == 200
}

// IsProxy returns true if it's a proxy rule (aka contains a hostname).
func (r *Rule) IsProxy() bool {
u, err := url.Parse(r.To)
Expand Down
31 changes: 31 additions & 0 deletions redirects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,34 @@ func TestRule_IsProxy(t *testing.T) {
assert.True(t, r.IsProxy())
})
}

func TestRule_IsRewrite(t *testing.T) {
t.Run("with 3xx", func(t *testing.T) {
r := redirects.Rule{
From: "/blog",
To: "/blog/engineering",
Status: 302,
}

assert.False(t, r.IsRewrite())
})

t.Run("with 200", func(t *testing.T) {
r := redirects.Rule{
From: "/blog",
To: "/blog/engineering",
Status: 200,
}

assert.True(t, r.IsRewrite())
})

t.Run("with 0", func(t *testing.T) {
r := redirects.Rule{
From: "/blog",
To: "/blog/engineering",
}

assert.False(t, r.IsRewrite())
})
}

0 comments on commit 16a0635

Please sign in to comment.