-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
129 lines (113 loc) · 3.1 KB
/
server_test.go
File metadata and controls
129 lines (113 loc) · 3.1 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
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
package peasant
import (
"encoding/json"
"net/http"
"testing"
"time"
"github.com/candango/gopeasant/dummy"
"github.com/candango/httpok/testrunner"
"github.com/stretchr/testify/assert"
)
type NoncedHandler struct {
http.Handler
s NonceService
}
func (h *NoncedHandler) GetNonce(w http.ResponseWriter, r *http.Request) {
s, ok := r.Context().Value("nonce-service").(NonceService)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
return
}
method := r.Method
if method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
nonce, err := s.GetNonce(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("nonce", nonce)
}
func (h *NoncedHandler) DoNoncedFunc(w http.ResponseWriter, r *http.Request) {
method := r.Method
if method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
nonce := r.Header.Get("nonce")
w.Write([]byte("Func done with nonce " + nonce))
}
func NewNoncedHandler(s NonceService) *NoncedHandler {
return &NoncedHandler{}
}
func NewNoncedFuncServeMux(t *testing.T) http.Handler {
s := dummy.NewDummyInMemoryNonceService()
nonced := NewNoncedHandler(s)
h := http.NewServeMux()
h.HandleFunc("/directory", GetDirectory)
h.HandleFunc("/new-nonce", NoncedHandlerFunc(s, nonced.GetNonce))
h.HandleFunc("/do-nonced-something",
NoncedHandlerFunc(s, nonced.DoNoncedFunc))
return NonceServed(s, "")(h)
}
func GetDirectory(w http.ResponseWriter, r *http.Request) {
directory := map[string]any{
"new-nonce": "/new-nonce",
"do-something": "/do-nonced-something",
}
w.Header().Set("Content-Type", "application/json")
out, err := json.Marshal(directory)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(out)
}
func TestNoncedFuncServer(t *testing.T) {
handler := NewNoncedFuncServeMux(t)
runner := testrunner.NewHttpTestRunner(t).WithHandler(handler)
t.Run("Retrieve a new nonce", func(t *testing.T) {
t.Run("Request OK", func(t *testing.T) {
res, err := runner.WithPath("/new-nonce").Head()
if err != nil {
t.Error(err)
}
assert.Equal(t, "200 OK", res.Status)
assert.Equal(t, http.NoBody, res.Body)
assert.Equal(t, 32, len(res.Header.Get("nonce")))
})
})
t.Run("Run a nonced function", func(t *testing.T) {
t.Run("Request OK", func(t *testing.T) {
res, err := runner.WithPath("/new-nonce").Head()
if err != nil {
t.Error(err)
}
nonce := res.Header.Get("nonce")
res, err = runner.WithPath(
"/do-nonced-something").WithHeader("nonce", nonce).Get()
if err != nil {
t.Error(err)
}
assert.Equal(t, "200 OK", res.Status)
assert.Equal(t, "Func done with nonce "+nonce,
testrunner.BodyAsString(t, res))
})
t.Run("Expired nonce", func(t *testing.T) {
res, err := runner.WithPath("/new-nonce").Head()
if err != nil {
t.Error(err)
}
nonce := res.Header.Get("nonce")
time.Sleep(250 * time.Millisecond)
res, err = runner.WithPath(
"/do-nonced-something").WithHeader("nonce", nonce).Get()
if err != nil {
t.Error(err)
}
assert.Equal(t, "403 Forbidden", res.Status)
})
})
}