Permalink
Browse files

Added mango.Redirect to make redirects a bit easier

  • Loading branch information...
1 parent 3c286b2 commit 07c74e6f25468c7cf362c0d3e43cb38ce9897f43 @paulbellamy committed Aug 20, 2011
Showing with 63 additions and 0 deletions.
  1. +1 −0 Makefile
  2. +5 −0 redirect.go
  3. +57 −0 redirect_test.go
View
1 Makefile
@@ -12,6 +12,7 @@ GOFILES=\
static.go\
jsonp.go\
mime.go\
+ redirect.go\
include $(GOROOT)/src/Make.pkg
View
5 redirect.go
@@ -0,0 +1,5 @@
+package mango
+
+func Redirect(status Status, location string) (Status, Headers, Body) {
+ return status, Headers{"Location": []string{location}}, Body("")
+}
View
57 redirect_test.go
@@ -0,0 +1,57 @@
+package mango
+
+import (
+ "http"
+ "testing"
+ "runtime"
+)
+
+func redirectTestServer(env Env) (Status, Headers, Body) {
+ return Redirect(302, "/somewhere")
+}
+
+func init() {
+ runtime.GOMAXPROCS(4)
+}
+
+func TestRedirect(t *testing.T) {
+ // Compile the stack
+ redirectApp := new(Stack).Compile(redirectTestServer)
+
+ // Request against it
+ request, err := http.NewRequest("GET", "http://localhost:3000/", nil)
+ status, headers, body := redirectApp(Env{"mango.request": &Request{request}})
+
+ if err != nil {
+ t.Error(err)
+ }
+
+ if status != 302 {
+ t.Error("Expected status to equal 302, got:", status)
+ }
+
+ expected := "/somewhere"
+ if headers["Location"][0] != expected {
+ t.Error("Expected Location header to be: \"", expected, "\" got: \"", headers["Location"][0], "\"")
+ }
+
+ expectedBody := Body("")
+ if body != expectedBody {
+ t.Error("Expected body to be: \"", expected, "\" got: \"", body, "\"")
+ }
+}
+
+func BenchmarkRedirect(b *testing.B) {
+ b.StopTimer()
+
+ redirectApp := new(Stack).Compile(redirectTestServer)
+
+ // Request against it
+ request, _ := http.NewRequest("GET", "http://localhost:3000/", nil)
+
+ b.StartTimer()
+ for i := 0; i < b.N; i++ {
+ redirectApp(Env{"mango.request": &Request{request}})
+ }
+ b.StopTimer()
+}

0 comments on commit 07c74e6

Please sign in to comment.