Skip to content

Commit

Permalink
Added mango.Redirect to make redirects a bit easier
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbellamy committed Aug 20, 2011
1 parent 3c286b2 commit 07c74e6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ GOFILES=\
static.go\
jsonp.go\
mime.go\
redirect.go\

include $(GOROOT)/src/Make.pkg

Expand Down
5 changes: 5 additions & 0 deletions redirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mango

func Redirect(status Status, location string) (Status, Headers, Body) {
return status, Headers{"Location": []string{location}}, Body("")
}
57 changes: 57 additions & 0 deletions redirect_test.go
Original file line number Diff line number Diff line change
@@ -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.