This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
daemon_test.go
74 lines (59 loc) · 1.84 KB
/
daemon_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDaemonStatus(t *testing.T) {
// Start secretary daemon
handler := statusEndpointHandler()
daemon := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/v1/status":
handler(w, r)
default:
http.Error(w, fmt.Sprintf("Bad URL %s", r.URL.Path), http.StatusNotFound)
}
}))
defer daemon.Close()
response, err := httpGet(daemon.URL + "/v1/status")
assert.Nil(t, err)
var parsedResponse DaemonStatusResponse
err = json.Unmarshal(response, &parsedResponse)
assert.Nil(t, err)
assert.Equal(t, "OK", parsedResponse.Status)
}
func TestTLSDaemonStatus(t *testing.T) {
// Start secretary daemon
handler := statusEndpointHandler()
daemon := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/v1/status":
handler(w, r)
default:
http.Error(w, fmt.Sprintf("Bad URL %s", r.URL.Path), http.StatusNotFound)
}
}))
cert, err := tls.LoadX509KeyPair("./resources/test/keys/tlscertfile.pem", "./resources/test/keys/tlskeyfile.pem")
daemon.TLS = &tls.Config{Certificates: []tls.Certificate{cert}}
daemon.StartTLS()
defer daemon.Close()
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableCompression: true,
}
client := &http.Client{Transport: tr}
response, err := client.Get(daemon.URL + "/v1/status")
assert.Nil(t, err)
var parsedResponse DaemonStatusResponse
respBody, err := httpReadBody(response)
err = json.Unmarshal(respBody, &parsedResponse)
assert.Nil(t, err)
assert.Equal(t, "OK", parsedResponse.Status)
}