Permalink
Browse files

Refacter the handler for easier testing, add testing of server.go

  • Loading branch information...
elementalvoid committed Apr 2, 2016
1 parent e130099 commit ce91a613ccda6d30d0c76cf5bfba669cdd6af19e
Showing with 78 additions and 18 deletions.
  1. +26 −18 server.go
  2. +52 −0 server_test.go
View
@@ -1,6 +1,7 @@
package main
import (
+ "errors"
"fmt"
"log"
"net"
@@ -19,17 +20,7 @@ func fallback(w http.ResponseWriter, r *http.Request, reason string) {
http.Redirect(w, r, location, 302)
}
-func handler(w http.ResponseWriter, r *http.Request) {
- parts := strings.Split(r.Host, ":")
- host := parts[0]
-
- hostname := fmt.Sprintf("_redirect.%s", host)
- txt, err := net.LookupTXT(hostname)
- if err != nil {
- fallback(w, r, fmt.Sprintf("Could not resolve hostname (%v)", err))
- return
- }
-
+func getRedirect(txt []string, url string) (*Redirect, error) {
catch_alls := make(map[string]*Config)
for _, record := range txt {
@@ -38,23 +29,40 @@ func handler(w http.ResponseWriter, r *http.Request) {
catch_alls[record] = config
continue
}
- redirect := Translate(r.URL.String(), config)
+ redirect := Translate(url, config)
if redirect != nil {
- http.Redirect(w, r, redirect.Location, redirect.Status)
- return
+ return redirect, nil
}
}
var config *Config
for _, config = range catch_alls {
- redirect := Translate(r.URL.String(), config)
+ redirect := Translate(url, config)
if redirect != nil {
- http.Redirect(w, r, redirect.Location, redirect.Status)
- return
+ return redirect, nil
}
}
- fallback(w, r, "No paths matched")
+ return nil, errors.New("No paths matched")
+}
+
+func handler(w http.ResponseWriter, r *http.Request) {
+ parts := strings.Split(r.Host, ":")
+ host := parts[0]
+
+ hostname := fmt.Sprintf("_redirect.%s", host)
+ txt, err := net.LookupTXT(hostname)
+ if err != nil {
+ fallback(w, r, fmt.Sprintf("Could not resolve hostname (%v)", err))
+ return
+ }
+
+ redirect, err := getRedirect(txt, r.URL.String())
+ if err != nil {
+ fallback(w, r, err.Error())
+ } else {
+ http.Redirect(w, r, redirect.Location, redirect.Status)
+ }
}
func main() {
View
@@ -0,0 +1,52 @@
+package main
+
+import "testing"
+
+func TestGetRedirectSimple(t *testing.T) {
+ var redirect *Redirect
+ var err error
+
+ dnsTXT := []string{
+ "Redirects from /test/* to https://github.com/holic/*",
+ }
+
+ redirect, err = getRedirect(dnsTXT, "/test/")
+ assertEqual(t, err, nil)
+ assertEqual(t, redirect.Location, "https://github.com/holic/")
+
+ redirect, err = getRedirect(dnsTXT, "/test/success")
+ assertEqual(t, err, nil)
+ assertEqual(t, redirect.Location, "https://github.com/holic/success")
+
+ redirect, err = getRedirect(dnsTXT, "/should/fail")
+ assertEqual(t, err.Error(), "No paths matched")
+}
+
+func TestGetRedirectComplex(t *testing.T) {
+ // Tests that catchalls (even interspersed in the TXT records) apply
+ // only after more specific matches
+ var redirect *Redirect
+ var err error
+
+ dnsTXT := []string{
+ "Redirects from /test/* to https://github.com/holic/*",
+ "Redirects to https://github.com/holic",
+ "Redirects from /noglob/ to https://github.com/holic/noglob",
+ }
+
+ redirect, err = getRedirect(dnsTXT, "/")
+ assertEqual(t, err, nil)
+ assertEqual(t, redirect.Location, "https://github.com/holic")
+
+ redirect, err = getRedirect(dnsTXT, "/test/somepath")
+ assertEqual(t, err, nil)
+ assertEqual(t, redirect.Location, "https://github.com/holic/somepath")
+
+ redirect, err = getRedirect(dnsTXT, "/noglob/")
+ assertEqual(t, err, nil)
+ assertEqual(t, redirect.Location, "https://github.com/holic/noglob")
+
+ redirect, err = getRedirect(dnsTXT, "/catch/all")
+ assertEqual(t, err, nil)
+ assertEqual(t, redirect.Location, "https://github.com/holic")
+}

0 comments on commit ce91a61

Please sign in to comment.