-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqrserve_test.go
59 lines (51 loc) · 1.54 KB
/
qrserve_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
package main
import (
"image/png"
"net/http"
"net/http/httptest"
"testing"
)
func testGet(uri string) (rr *httptest.ResponseRecorder) {
req := httptest.NewRequest("GET", uri, nil)
rr = httptest.NewRecorder()
handler := http.HandlerFunc(qrHandler)
handler.ServeHTTP(rr, req)
return
}
// Tests that qrHandler returns Bad Request if data param is missing
func TestMissingData(t *testing.T) {
rr := testGet("/")
if rr.Code != http.StatusBadRequest {
t.Error("handler didn't return Bad Request on missing data")
}
}
// Tests that qrHandler returns Bad Request if size is not an integer value
func TestInvalidSize(t *testing.T) {
rr := testGet("/?data=test&size=large")
if rr.Code != http.StatusBadRequest {
t.Error("handler didn't return Bad Request on invalid size")
}
}
// Tests that qrHandler returns Bad Request if size is too large
func TestTooLarge(t *testing.T) {
rr := testGet("/?data=test&size=8192")
if rr.Code != http.StatusBadRequest {
t.Error("handler didn't return Bad Request on image size too large")
}
}
// Tests that qrHandler generates and returns the QR code image as PNG when
// all the params are set
func TestGeneratesImage(t *testing.T) {
rr := testGet("/?data=test&size=100")
if rr.Code != http.StatusOK {
t.Error("handler didn't return status code 200 OK")
}
img, err := png.Decode(rr.Body)
if err != nil {
t.Errorf("handler didn't return a PNG image: %s", err.Error())
}
size := img.Bounds().Max
if size.X != 100 || size.Y != 100 {
t.Errorf("expected PNG of size (100,100), got size %s instead", size)
}
}