Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 111 lines (87 sloc) 2.708 kb
3ffa7d4 @paulbellamy Adding tests for static middleware
authored
1 package mango
2
3 import (
ea791a1 @paulbellamy Fixing binary file transmission. Resolves #15
authored
4 "bytes"
5 "io/ioutil"
d968516 @paulbellamy Running go fmt
authored
6 "net/http"
3ffa7d4 @paulbellamy Adding tests for static middleware
authored
7 "testing"
8 )
9
10 func staticTestServer(env Env) (Status, Headers, Body) {
11 return 200, Headers{"Content-Type": []string{"text/html"}}, Body("<h1>Hello World!</h1>")
12 }
13
14 func TestStaticSuccess(t *testing.T) {
15 // Compile the stack
16 staticStack := new(Stack)
17 staticStack.Middleware(Static("./static"))
18 staticApp := staticStack.Compile(staticTestServer)
19
20 // Request against it
21 request, err := http.NewRequest("GET", "http://localhost:3000/static.html", nil)
3dd9abe @paulbellamy Adding mime-type handling to mango.Static, and mango.MimeType(extension,...
authored
22 status, headers, body := staticApp(Env{"mango.request": &Request{request}})
3ffa7d4 @paulbellamy Adding tests for static middleware
authored
23
24 if err != nil {
25 t.Error(err)
26 }
27
28 if status != 200 {
29 t.Error("Expected status to equal 200, got:", status)
30 }
31
32 expected := "<h1>I'm a static test file</h1>\n"
33 if string(body) != expected {
34 t.Error("Expected body:", string(body), "to equal:", expected)
3dd9abe @paulbellamy Adding mime-type handling to mango.Static, and mango.MimeType(extension,...
authored
35 }
36
37 expected = "text/html"
38 got := headers.Get("Content-Type")
39 if got != expected {
40 t.Error("Expected Content-Type:", got, "to equal:", expected)
3ffa7d4 @paulbellamy Adding tests for static middleware
authored
41 }
42 }
43
44 func TestStaticFail(t *testing.T) {
45 // Compile the stack
46 staticStack := new(Stack)
47 staticStack.Middleware(Static("./static"))
48 staticApp := staticStack.Compile(staticTestServer)
49
50 // Request against it
51 request, err := http.NewRequest("GET", "http://localhost:3000/not_a_file.html", nil)
52 status, _, body := staticApp(Env{"mango.request": &Request{request}})
53
54 if err != nil {
55 t.Error(err)
56 }
57
58 if status != 200 {
59 t.Error("Expected status to equal 200, got:", status)
60 }
61
62 expected := "<h1>Hello World!</h1>"
63 if string(body) != expected {
64 t.Error("Expected body:", string(body), "to equal:", expected)
65 }
66 }
009def6 @paulbellamy Adding in benchmarks using http/httptest
authored
67
ea791a1 @paulbellamy Fixing binary file transmission. Resolves #15
authored
68 func TestStaticBinaryFile(t *testing.T) {
69 // Compile the stack
70 staticStack := new(Stack)
71 staticStack.Middleware(Static("./static"))
72 staticApp := staticStack.Compile(staticTestServer)
73
74 // Request against it
75 request, err := http.NewRequest("GET", "http://localhost:3000/binary_file.png", nil)
76 status, _, body := staticApp(Env{"mango.request": &Request{request}})
77
78 if err != nil {
79 t.Error(err)
80 }
81
82 if status != 200 {
83 t.Error("Expected status to equal 200, got:", status)
84 }
85
86 expected, err := ioutil.ReadFile("./static/binary_file.png")
87 if err != nil {
88 t.Error(err)
89 }
90
91 if bytes.Compare([]byte(body), []byte(expected)) != 0 {
92 t.Error("Expected body to equal ./static/binary_file.png")
93 }
94 }
95
009def6 @paulbellamy Adding in benchmarks using http/httptest
authored
96 func BenchmarkStatic(b *testing.B) {
97 b.StopTimer()
98
ebf2610 @paulbellamy Removing http/httptest from module tests because it is *SLOW*
authored
99 staticStack := new(Stack)
100 staticStack.Middleware(Static("./static"))
101 staticApp := staticStack.Compile(staticTestServer)
102
103 request, _ := http.NewRequest("GET", "http://localhost:3000/static.html", nil)
009def6 @paulbellamy Adding in benchmarks using http/httptest
authored
104
105 b.StartTimer()
106 for i := 0; i < b.N; i++ {
ebf2610 @paulbellamy Removing http/httptest from module tests because it is *SLOW*
authored
107 staticApp(Env{"mango.request": &Request{request}})
009def6 @paulbellamy Adding in benchmarks using http/httptest
authored
108 }
109 b.StopTimer()
110 }
Something went wrong with that request. Please try again.