Skip to content
Permalink
Browse files

Add support for 307, 308 status codes

See #12
  • Loading branch information
holic committed Apr 2, 2016
1 parent 89bbd7f commit f8702c6e2df4dfb80d69cfad4406b0f07ce1554b
Showing with 61 additions and 1 deletion.
  1. +4 −1 parse.go
  2. +35 −0 parse_test.go
  3. +4 −0 translate.go
  4. +18 −0 translate_test.go
@@ -11,7 +11,7 @@ type Config struct {
var configRE = regexp.MustCompile(`Redirects?(\s+.*)`)
var fromRE = regexp.MustCompile(`\s+from\s+(/\S*)`)
var toRE = regexp.MustCompile(`\s+to\s+(https?\://\S+|/\S*)`)
var stateRE = regexp.MustCompile(`\s+(permanently|temporarily)`)
var stateRE = regexp.MustCompile(`\s+(permanently|temporarily)|\s+with\s+(301|302|307|308)`)

func Parse(record string) *Config {
configMatches := configRE.FindStringSubmatch(record)
@@ -32,6 +32,9 @@ func Parse(record string) *Config {
}
if len(stateMatches) > 0 {
config.RedirectState = stateMatches[1]
if config.RedirectState == "" {
config.RedirectState = stateMatches[2]
}
}

return config
@@ -40,4 +40,39 @@ func TestParse(t *testing.T) {
assertEqual(t, config.From, "")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "temporarily")

// Test status codes

config = Parse("Redirect to http://github.com/holic with 301")
assertEqual(t, config.From, "")
assertEqual(t, config.To, "http://github.com/holic")
assertEqual(t, config.RedirectState, "301")

config = Parse("Redirect with 302 from / to http://github.com/holic")
assertEqual(t, config.From, "/")
assertEqual(t, config.To, "http://github.com/holic")
assertEqual(t, config.RedirectState, "302")

config = Parse("Redirects to /new from /old with 307")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "307")

config = Parse("Redirects with 308 from /old to /new")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "308")

// Test that we get the first parsed value when multiple values
// of the same type are specified (e.g. `permanently` and `with 308`)

config = Parse("Redirects permanently from /old to /new with 302")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "permanently")

config = Parse("Redirects with 307 from /old to /new permanently")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "307")
}
@@ -29,6 +29,10 @@ func Translate(uri string, config *Config) *Redirect {
redirect.Status = 301
case "302", "temporarily":
redirect.Status = 302
case "307":
redirect.Status = 307
case "308":
redirect.Status = 308
default:
redirect.Status = 302
}
@@ -26,6 +26,24 @@ func TestTranslate(t *testing.T) {
redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "permanently"})
assertEqual(t, redirect.Location, "https://example.com/")
assertEqual(t, redirect.Status, 301)

redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "temporarily"})
assertEqual(t, redirect.Location, "https://example.com/")
assertEqual(t, redirect.Status, 302)

// Test status codes

redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "301"})
assertEqual(t, redirect.Status, 301)

redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "302"})
assertEqual(t, redirect.Status, 302)

redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "307"})
assertEqual(t, redirect.Status, 307)

redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "308"})
assertEqual(t, redirect.Status, 308)
}

func TestTranslateWildcard(t *testing.T) {

0 comments on commit f8702c6

Please sign in to comment.
You can’t perform that action at this time.