-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
159 lines (152 loc) · 4.89 KB
/
server_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package lazypress
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
"testing"
)
func TestShouldDownloadPDFWhenNoOutputParam(t *testing.T) {
html := `<html><body>Hello World</body></html>`
req, err := http.NewRequest("POST", "/convert", strings.NewReader(html))
if err != nil {
t.Error(err)
}
req.Header.Set("Content-Type", "text/html")
req.Header.Set("Content-Length", fmt.Sprint((len(html))))
w := httptest.NewRecorder()
// locate chrome executable path
dir, dirError := os.Getwd()
if dirError != nil {
log.Fatalln(dirError)
}
chromePath := path.Join(dir, "chrome-linux", "chrome")
convertHTMLServerHandler(chromePath)(w, req)
result := w.Result()
defer result.Body.Close()
if result.Header.Get("Content-Type") != "application/pdf" {
t.Errorf("Expected Content-Type to be application/pdf, got %s", result.Header.Get("Content-Type"))
}
// TODO: add some more robust checks
}
func TestShouldReturnErrorWhenContentTypeIsNotAllowed(t *testing.T) {
html := `<html><body>Hello World</body></html>`
req, err := http.NewRequest("POST", "/convert", strings.NewReader(html))
if err != nil {
t.Error(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Length", fmt.Sprint((len(html))))
w := httptest.NewRecorder()
// locate chrome executable path
dir, dirError := os.Getwd()
if dirError != nil {
log.Fatalln(dirError)
}
chromePath := path.Join(dir, "chrome-linux", "chrome")
convertHTMLServerHandler(chromePath)(w, req)
result := w.Result()
defer result.Body.Close()
if result.StatusCode != http.StatusBadRequest {
t.Errorf("Expected status code to be 400, got %d", result.StatusCode)
}
}
func TestShouldReturnErrorWhenContentLenghtIsZero(t *testing.T) {
html := `<html><body>Hello World</body></html>`
req, err := http.NewRequest("POST", "/convert", strings.NewReader(html))
if err != nil {
t.Error(err)
}
req.Header.Set("Content-Type", "text/html")
req.Header.Set("Content-Length", "0")
w := httptest.NewRecorder()
// locate chrome executable path
dir, dirError := os.Getwd()
if dirError != nil {
log.Fatalln(dirError)
}
chromePath := path.Join(dir, "chrome-linux", "chrome")
convertHTMLServerHandler(chromePath)(w, req)
result := w.Result()
defer result.Body.Close()
if result.StatusCode != http.StatusBadRequest {
t.Errorf("Expected status code to be 400, got %d", result.StatusCode)
}
}
func TestShouldReturnErrorWhenRequestBodyIsEmpty(t *testing.T) {
req, err := http.NewRequest("POST", "/convert", strings.NewReader(""))
if err != nil {
t.Error(err)
}
req.Header.Set("Content-Type", "text/html")
// we don't want to test the content length
// validation (we did this before).
// we want to just test the actual size of
// the req's body.
// therefore we set the content-length to
// a non-zero value.
req.Header.Set("Content-Length", "42")
w := httptest.NewRecorder()
// locate chrome executable path
dir, dirError := os.Getwd()
if dirError != nil {
log.Fatalln(dirError)
}
chromePath := path.Join(dir, "chrome-linux", "chrome")
convertHTMLServerHandler(chromePath)(w, req)
result := w.Result()
defer result.Body.Close()
if result.StatusCode != http.StatusBadRequest {
t.Errorf("Expected status code to be 400, got %d", result.StatusCode)
}
}
func TestShouldReturnErrorIfSanitizationIsOnAndContentIsAllScript(t *testing.T) {
html := `<script>alert("Hello World")</script>`
req, err := http.NewRequest("POST", "/convert?sanitize=true", strings.NewReader(html))
if err != nil {
t.Error(err)
}
req.Header.Set("Content-Type", "text/html")
req.Header.Set("Content-Length", fmt.Sprint((len(html))))
w := httptest.NewRecorder()
// locate chrome executable path
dir, dirError := os.Getwd()
if dirError != nil {
log.Fatalln(dirError)
}
chromePath := path.Join(dir, "chrome-linux", "chrome")
convertHTMLServerHandler(chromePath)(w, req)
result := w.Result()
defer result.Body.Close()
if result.StatusCode != http.StatusBadRequest {
t.Errorf("Expected status code to be 400, got %d", result.StatusCode)
}
}
func TestShouldNotReturnErrorIfSanitizationIsOffAndContentIsAllScript(t *testing.T) {
html := `<script>alert("Hello World")</script>`
req, err := http.NewRequest("POST", "/convert", strings.NewReader(html))
if err != nil {
t.Error(err)
}
req.Header.Set("Content-Type", "text/html")
req.Header.Set("Content-Length", fmt.Sprint((len(html))))
w := httptest.NewRecorder()
// locate chrome executable path
dir, dirError := os.Getwd()
if dirError != nil {
log.Fatalln(dirError)
}
chromePath := path.Join(dir, "chrome-linux", "chrome")
convertHTMLServerHandler(chromePath)(w, req)
result := w.Result()
defer result.Body.Close()
if result.StatusCode != http.StatusOK {
t.Errorf("Expected status code to be 200, got %d", result.StatusCode)
}
if result.Header.Get("Content-Type") != "application/pdf" {
t.Errorf("Expected Content-Type to be application/pdf, got %s", result.Header.Get("Content-Type"))
}
}