Skip to content
Newer
Older
100644 86 lines (67 sloc) 1.98 KB
3ffa7d4 @paulbellamy Adding tests for static middleware
authored Jun 6, 2011
1 package mango
2
3 import (
4 "http"
5 "testing"
6 "runtime"
7 )
8
9 func staticTestServer(env Env) (Status, Headers, Body) {
10 return 200, Headers{"Content-Type": []string{"text/html"}}, Body("<h1>Hello World!</h1>")
11 }
12
13 func init() {
14 runtime.GOMAXPROCS(4)
15 }
16
17 func TestStaticSuccess(t *testing.T) {
18 // Compile the stack
19 staticStack := new(Stack)
20 staticStack.Middleware(Static("./static"))
21 staticApp := staticStack.Compile(staticTestServer)
22
23 // Request against it
24 request, err := http.NewRequest("GET", "http://localhost:3000/static.html", nil)
3dd9abe @paulbellamy Adding mime-type handling to mango.Static, and mango.MimeType(extensi…
authored Jun 25, 2011
25 status, headers, body := staticApp(Env{"mango.request": &Request{request}})
3ffa7d4 @paulbellamy Adding tests for static middleware
authored Jun 6, 2011
26
27 if err != nil {
28 t.Error(err)
29 }
30
31 if status != 200 {
32 t.Error("Expected status to equal 200, got:", status)
33 }
34
35 expected := "<h1>I'm a static test file</h1>\n"
36 if string(body) != expected {
37 t.Error("Expected body:", string(body), "to equal:", expected)
3dd9abe @paulbellamy Adding mime-type handling to mango.Static, and mango.MimeType(extensi…
authored Jun 25, 2011
38 }
39
40 expected = "text/html"
41 got := headers.Get("Content-Type")
42 if got != expected {
43 t.Error("Expected Content-Type:", got, "to equal:", expected)
3ffa7d4 @paulbellamy Adding tests for static middleware
authored Jun 6, 2011
44 }
45 }
46
47 func TestStaticFail(t *testing.T) {
48 // Compile the stack
49 staticStack := new(Stack)
50 staticStack.Middleware(Static("./static"))
51 staticApp := staticStack.Compile(staticTestServer)
52
53 // Request against it
54 request, err := http.NewRequest("GET", "http://localhost:3000/not_a_file.html", nil)
55 status, _, body := staticApp(Env{"mango.request": &Request{request}})
56
57 if err != nil {
58 t.Error(err)
59 }
60
61 if status != 200 {
62 t.Error("Expected status to equal 200, got:", status)
63 }
64
65 expected := "<h1>Hello World!</h1>"
66 if string(body) != expected {
67 t.Error("Expected body:", string(body), "to equal:", expected)
68 }
69 }
009def6 @paulbellamy Adding in benchmarks using http/httptest
authored Jun 12, 2011
70
71 func BenchmarkStatic(b *testing.B) {
72 b.StopTimer()
73
ebf2610 @paulbellamy Removing http/httptest from module tests because it is *SLOW*
authored Jun 12, 2011
74 staticStack := new(Stack)
75 staticStack.Middleware(Static("./static"))
76 staticApp := staticStack.Compile(staticTestServer)
77
78 request, _ := http.NewRequest("GET", "http://localhost:3000/static.html", nil)
009def6 @paulbellamy Adding in benchmarks using http/httptest
authored Jun 12, 2011
79
80 b.StartTimer()
81 for i := 0; i < b.N; i++ {
ebf2610 @paulbellamy Removing http/httptest from module tests because it is *SLOW*
authored Jun 12, 2011
82 staticApp(Env{"mango.request": &Request{request}})
009def6 @paulbellamy Adding in benchmarks using http/httptest
authored Jun 12, 2011
83 }
84 b.StopTimer()
85 }
Something went wrong with that request. Please try again.