-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
48 lines (40 loc) · 1.17 KB
/
server_test.go
File metadata and controls
48 lines (40 loc) · 1.17 KB
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
package main
import (
"github.com/mikihau/rest-job-worker/handler"
"github.com/mikihau/rest-job-worker/model"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func getStuff(w http.ResponseWriter, r *http.Request, l *log.Logger) {
w.WriteHeader(http.StatusOK)
}
func TestAuth(t *testing.T) {
cases := []struct {
headerName, headerValue string
code int
}{
{"", "", http.StatusUnauthorized},
{"Authorization", "wow", http.StatusUnauthorized},
{"Authorization", "reader", http.StatusForbidden},
{"Authorization", "writer", http.StatusOK},
}
req, err := http.NewRequest("GET", "/stuff", nil)
if err != nil {
t.Fatal(err)
}
// this test handler requires Role ReadWrite
authorizedFunc := handler.VerifyAuth(getStuff, []model.Role{model.ReadWrite}, log.New(os.Stdout, "", log.LstdFlags))
handler := http.HandlerFunc(authorizedFunc)
for _, c := range cases {
rr := httptest.NewRecorder()
req.Header.Set(c.headerName, c.headerValue)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != c.code {
t.Errorf("Wrong status code: with header %v:%v, expecting %v, but got %v",
c.headerName, c.headerValue, c.code, status)
}
}
}