-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathid_handler_test.go
More file actions
58 lines (50 loc) · 1.1 KB
/
id_handler_test.go
File metadata and controls
58 lines (50 loc) · 1.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
package operator
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/balboah/wireguard-operator/proto"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func TestIDHandler(t *testing.T) {
key, err := wgtypes.GeneratePrivateKey()
if err != nil {
t.Fatal(err)
}
pubkey := keyToSlice(key.PublicKey())
id := myID{
publicKey: pubkey,
port: 1337,
}
req := httptest.NewRequest("GET", "http://operator/v1/id", nil)
w := httptest.NewRecorder()
started := time.Now()
h := IDHandler(id)
h(w, req)
if w.Code != http.StatusOK {
t.Error(http.StatusText(w.Code))
}
res := proto.IDResponse{}
t.Log(string(w.Body.Bytes()))
if err := json.NewDecoder(w.Body).Decode(&res); err != nil {
t.Fatal(err)
}
if !bytes.Equal(res.PublicKey, pubkey) {
t.Error("invalid public key")
}
if res.Port != 1337 {
t.Error("invalid port")
}
if res.Started.Before(started) {
t.Error("invalid started timestamp")
}
}
type myID struct {
publicKey []byte
port int
}
func (id myID) PublicKey() []byte { return id.publicKey }
func (id myID) Port() int { return id.port }