-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathloader_test.go
119 lines (104 loc) · 2.8 KB
/
loader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package gohaml
import (
"bytes"
"net/http"
"net/url"
"os"
"testing"
)
const test_dir = "./test"
const simple_haml = "simple.haml"
const simple_html = "simple.html"
func TestLoadFile(t *testing.T) {
var fsl Loader
var err error
if fsl, err = NewFileSystemLoader(test_dir + "/"); err != nil {
t.Errorf("couldn't create fileSystemLoader: %s", err)
}
if _, err = fsl.Load(1); err == nil {
t.Errorf("rats! expected error")
}
if _, err = fsl.Load(simple_haml); err != nil {
t.Errorf("couldn't load: test.haml: %s", err)
}
if fsl, err = NewFileSystemLoader(test_dir); err != nil {
t.Errorf("couldn't create fileSystemLoader: %s", err)
}
if _, err = fsl.Load(1); err == nil {
t.Errorf("rats! expected error")
}
if _, err = fsl.Load(simple_haml); err != nil {
t.Errorf("couldn't load: test.haml: %s", err)
}
if fsl, err = NewFileSystemLoader("blsadfasdf"); err == nil {
t.Errorf("rats! expected error for non existing dir ... ")
}
}
func readFile(t *testing.T, fn string) ([]byte, error) {
file, err := os.Open(fn)
if err != nil {
return nil, err
}
defer file.Close()
data := make([]byte, 1024)
if count, err2 := file.Read(data); err2 != nil {
return nil, err2
} else {
return data[:count], nil
}
// dead code
return nil, nil
}
func TestHttp(t *testing.T) {
if httpHandler, err := NewHamlHandler(test_dir); err != nil {
t.Errorf("couldn't create HamlHandler: %s", err)
} else {
writer := &TestResponseWriter{bytes.NewBufferString(""), nil, 0}
request := http.Request{}
request.URL, _ = url.Parse("http://localhost/simple.html")
if expected, err2 := readFile(t, test_dir+"/"+simple_html); err2 != nil {
t.Errorf("couldn't load result: %s", err2)
} else {
httpHandler.ServeHTTP(writer, &request)
if !bytes.Equal(writer.b.Bytes(), expected) {
t.Errorf("unexpected result. <%s> >%s<", writer.b.Bytes(), expected)
}
}
writer.h = nil
request.URL, _ = url.Parse("http://localhost/index.html")
httpHandler.ServeHTTP(writer, &request)
if 301 != writer.s {
t.Errorf("incorrect status: %d", writer.s)
}
if "./" != writer.h["Location"][0] {
t.Errorf("incorrect redirect: %s", writer.h["Location"][0])
}
// shouldn't redirect
writer.h = nil
request.URL, _ = url.Parse("http://localhost/")
httpHandler.ServeHTTP(writer, &request)
if 404 != writer.s {
t.Errorf("incorrect status: %d", writer.s)
}
if nil != writer.h["Location"] {
t.Errorf("redirect but shouldn't: %s", writer.h["Location"][0])
}
}
}
type TestResponseWriter struct {
b *bytes.Buffer
h http.Header
s int
}
func (t *TestResponseWriter) Header() http.Header {
if t.h == nil {
t.h = make(map[string][]string)
}
return t.h
}
func (t *TestResponseWriter) Write(bytes []byte) (int, error) {
return t.b.Write(bytes)
}
func (t *TestResponseWriter) WriteHeader(i int) {
t.s = i
}