-
Notifications
You must be signed in to change notification settings - Fork 0
/
router_test.go
144 lines (125 loc) · 3.86 KB
/
router_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package seenit
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)
func TestServeLanding(t *testing.T) {
request, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
record := httptest.NewRecorder()
handler := http.HandlerFunc(ServeLanding)
handler.ServeHTTP(record, request)
if status := record.Code; status != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, status)
}
outbuf := new(bytes.Buffer)
err = RenderLanding(outbuf)
if err != nil {
t.Fatal(err)
}
if outbuf.String() != record.Body.String() {
t.Errorf("Mismatch in expected body content - got: %s\n\nexpected: %s", outbuf.String(), record.Body.String())
}
}
func TestServeUpload(t *testing.T) {
query := url.Values{}
query.Add("community", "Matrix")
request, err := http.NewRequest("POST", "/upload", strings.NewReader(query.Encode()))
if err != nil {
t.Fatal(err)
}
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
request.Header.Add("Content-Length", strconv.Itoa(len(query.Encode())))
record := httptest.NewRecorder()
handler := http.HandlerFunc(ServeUpload)
handler.ServeHTTP(record, request)
if status := record.Code; status != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, status)
}
outbuf := new(bytes.Buffer)
err = RenderUpload("Matrix", outbuf)
if err != nil {
t.Fatal(err)
}
if outbuf.String() != record.Body.String() {
t.Errorf("Mismatch in expected body content - got: %s\n\nexpected: %s", outbuf.String(), record.Body.String())
}
}
func buildPostBody(t *testing.T, filename string, community string) (*bytes.Buffer, string) {
file, err := os.Open(filename)
if err != nil {
t.Fatal(err)
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("image", filepath.Base(file.Name()))
if err != nil {
t.Fatal(err)
}
io.Copy(part, file)
cWriter, err := writer.CreateFormField("community")
if err != nil {
t.Fatal(err)
}
io.WriteString(cWriter, community)
writer.Close()
return body, writer.Boundary()
}
func TestServeResult(t *testing.T) {
body, boundary := buildPostBody(t, "test_image.png", "Matrix")
request, err := http.NewRequest("POST", "/result", body)
if err != nil {
t.Fatal(err)
}
request.Header.Add("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%s", boundary))
request.Header.Add("Content-Length", strconv.Itoa(body.Len()))
db := MockDatabase{Buckets: make(map[string]MockBucket, 0)}
record := httptest.NewRecorder()
handler := http.HandlerFunc(BindHandler(ServeResult, &db))
handler.ServeHTTP(record, request)
if status := record.Code; status != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, status)
}
outbuf := new(bytes.Buffer)
err = RenderUnseen(outbuf)
if err != nil {
t.Fatal(err)
}
if outbuf.String() != record.Body.String() {
t.Errorf("Mismatch in expected body content - got: %s\n\nexpected: %s", outbuf.String(), record.Body.String())
}
body, boundary = buildPostBody(t, "test_image.png", "Matrix")
request, err = http.NewRequest("POST", "/result", body)
if err != nil {
t.Fatal(err)
}
request.Header.Add("Content-Type", fmt.Sprintf("multipart/form-data; boundary=%s", boundary))
request.Header.Add("Content-Length", strconv.Itoa(body.Len()))
record = httptest.NewRecorder()
handler = http.HandlerFunc(BindHandler(ServeResult, &db))
handler.ServeHTTP(record, request)
if status := record.Code; status != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, status)
}
outbuf = new(bytes.Buffer)
err = RenderSeen(outbuf)
if err != nil {
t.Fatal(err)
}
if outbuf.String() != record.Body.String() {
t.Errorf("Mismatch in expected body content - got: %s\n\nexpected: %s", outbuf.String(), record.Body.String())
}
}