/
main_test.go
55 lines (40 loc) · 1.06 KB
/
main_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
package main
import (
"testing"
"os"
"net/http"
"net/http/httptest"
)
var a App
func TestMain(m *testing.M) {
a = App{}
a.initialize()
code := m.Run()
os.Exit(code)
}
func TestIllegalRequest(t *testing.T) {
req, _ := http.NewRequest("POST", "/search", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusForbidden, response.Code)
if body := response.Body.String(); body != "Allow: GET" {
t.Errorf("Expected an empty array. Got %s", body)
}
}
func TestAllowedRequest(t *testing.T){
req, _ := http.NewRequest("GET", "/search?q=hello", nil)
response := executeRequest(req)
checkResponseCode(t, http.StatusOK, response.Code)
if body := response.Body.String(); len(body) < 0 {
t.Errorf("Expected a populated array. Got %s", body)
}
}
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
a.Router.ServeHTTP(rr, req)
return rr
}
func checkResponseCode(t *testing.T, expected, actual int) {
if expected != actual {
t.Errorf("Expected response code %d. Got %d\n", expected, actual)
}
}