-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver_test.go
52 lines (39 loc) · 1.18 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
// Steve Phillips / elimisteve
// 2017.11.09
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/cryptag/minishare/miniware"
"github.com/stretchr/testify/assert"
)
var router = NewRouter(miniware.NewMapper())
func TestLogin(t *testing.T) {
testURL(t, "GET", "/api/login", nil, router,
http.StatusBadRequest, `{"error":"Error: invalid miniLock ID"}`)
}
func TestRouting(t *testing.T) {
for _, url := range []string{"/dashboard", "/pursuance", "/123"} {
testURL(t, "GET", url, nil, router, http.StatusOK, "")
}
testURL(t, "GET", "/somethingelse/should404", nil, router, http.StatusNotFound, "")
}
func testURL(t *testing.T, httpMethod string, url string, headers http.Header, handler http.Handler, wantedStatusCode int, wantedResponse string) {
t.Logf("Testing '%v' request to '%v'", httpMethod, url)
req, err := http.NewRequest(httpMethod, url, nil)
if err != nil {
t.Fatal(err)
}
if headers != nil {
req.Header = headers
}
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if wantedStatusCode != 0 {
assert.Equal(t, wantedStatusCode, rec.Code)
}
if wantedResponse != "" {
assert.Equal(t, wantedResponse, rec.Body.String())
}
}