-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mango.Redirect to make redirects a bit easier
- Loading branch information
1 parent
3c286b2
commit 07c74e6
Showing
3 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ GOFILES=\ | |
static.go\ | ||
jsonp.go\ | ||
mime.go\ | ||
redirect.go\ | ||
|
||
include $(GOROOT)/src/Make.pkg | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |