Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

86 lines (67 sloc) 2.026 kB
package mango
import (
"http"
"testing"
"runtime"
)
func staticTestServer(env Env) (Status, Headers, Body) {
return 200, Headers{"Content-Type": []string{"text/html"}}, Body("<h1>Hello World!</h1>")
}
func init() {
runtime.GOMAXPROCS(4)
}
func TestStaticSuccess(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/static.html", nil)
status, headers, 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 := "<h1>I'm a static test file</h1>\n"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
expected = "text/html"
got := headers.Get("Content-Type")
if got != expected {
t.Error("Expected Content-Type:", got, "to equal:", expected)
}
}
func TestStaticFail(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/not_a_file.html", 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 := "<h1>Hello World!</h1>"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
}
func BenchmarkStatic(b *testing.B) {
b.StopTimer()
staticStack := new(Stack)
staticStack.Middleware(Static("./static"))
staticApp := staticStack.Compile(staticTestServer)
request, _ := http.NewRequest("GET", "http://localhost:3000/static.html", nil)
b.StartTimer()
for i := 0; i < b.N; i++ {
staticApp(Env{"mango.request": &Request{request}})
}
b.StopTimer()
}
Jump to Line
Something went wrong with that request. Please try again.