Skip to content
Browse files

Merge branch 'develop'

  • Loading branch information...
2 parents 2763a69 + 9a24a38 commit 119548b466aaad8ad2bdc59a96968eaa859a9308 @paulbellamy committed Sep 9, 2011
Showing with 99 additions and 22 deletions.
  1. +1 −0 Makefile
  2. +0 −17 TODO
  3. +1 −1 mango.go
  4. +5 −0 redirect.go
  5. +57 −0 redirect_test.go
  6. +4 −3 show_errors.go
  7. +1 −1 show_errors_test.go
  8. BIN static/binary_file.png
  9. +30 −0 static_test.go
View
1 Makefile
@@ -12,6 +12,7 @@ GOFILES=\
static.go\
jsonp.go\
mime.go\
+ redirect.go\
include $(GOROOT)/src/Make.pkg
View
17 TODO
@@ -1,17 +0,0 @@
-* Better cookie handler
- * Domain/Expiry
-* Clean up/Flesh out tests more
- * Better benchmarking
- * End-to-End
-
-POTENTIAL MIDDLEWARE
-====================
-
-* Basic-Auth
-* Cross-Origin Resource Sharing
-* Test module (to allow easy testing of apps)
-* Lint for validation (Enforce http spec)
-* Caching support for drop-in HTTP Caching
-* Edge-Side Include support
-* Multi-part file upload handling
- * provided via golang http package?
View
2 mango.go
@@ -133,7 +133,7 @@ func (this *Stack) HandlerFunc(app App) http.HandlerFunc {
}
}
w.WriteHeader(int(status))
- fmt.Fprintf(w, string(body))
+ w.Write([]byte(body))
}
}
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()
+}
View
7 show_errors.go
@@ -2,6 +2,7 @@ package mango
import (
"bytes"
+ "fmt"
"template"
)
@@ -11,20 +12,20 @@ func ShowErrors(templateString string) Middleware {
<html>
<body>
<p>
- {Error|html}
+ {{.Error|html}}
</p>
</body>
</html>
`
}
- errorTemplate := template.MustParse(templateString, nil)
+ errorTemplate := template.Must(template.New("error").Parse(templateString))
return func(env Env, app App) (status Status, headers Headers, body Body) {
defer func() {
if err := recover(); err != nil {
buffer := bytes.NewBufferString("")
- errorTemplate.Execute(buffer, struct{ Error string }{err.(string)})
+ errorTemplate.Execute(buffer, struct{ Error string }{fmt.Sprintf("%s", err)})
status = 500
headers = Headers{}
body = Body(buffer.String())
View
2 show_errors_test.go
@@ -18,7 +18,7 @@ func init() {
func TestShowErrors(t *testing.T) {
// Compile the stack
showErrorsStack := new(Stack)
- showErrorsStack.Middleware(ShowErrors("<html><body>{Error|html}</body></html>"))
+ showErrorsStack.Middleware(ShowErrors("<html><body>{{.Error|html}}</body></html>"))
showErrorsApp := showErrorsStack.Compile(showErrorsTestServer)
// Request against it
View
BIN static/binary_file.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
30 static_test.go
@@ -1,7 +1,9 @@
package mango
import (
+ "bytes"
"http"
+ "io/ioutil"
"testing"
"runtime"
)
@@ -68,6 +70,34 @@ func TestStaticFail(t *testing.T) {
}
}
+func TestStaticBinaryFile(t *testing.T) {
+ // Compile the stack
+ staticStack := new(Stack)
+ staticStack.Middleware(Static("./static"))
+ staticApp := staticStack.Compile(staticTestServer)
+
+ // Request against it
+ request, err := http.NewRequest("GET", "http://localhost:3000/binary_file.png", nil)
+ status, _, body := staticApp(Env{"mango.request": &Request{request}})
+
+ if err != nil {
+ t.Error(err)
+ }
+
+ if status != 200 {
+ t.Error("Expected status to equal 200, got:", status)
+ }
+
+ expected, err := ioutil.ReadFile("./static/binary_file.png")
+ if err != nil {
+ t.Error(err)
+ }
+
+ if bytes.Compare([]byte(body), []byte(expected)) != 0 {
+ t.Error("Expected body to equal ./static/binary_file.png")
+ }
+}
+
func BenchmarkStatic(b *testing.B) {
b.StopTimer()

0 comments on commit 119548b

Please sign in to comment.
Something went wrong with that request. Please try again.