-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_test.go
82 lines (65 loc) · 1.9 KB
/
http_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
75
76
77
78
79
80
81
82
package main
import (
"encoding/json"
"io/ioutil"
"net/http/httptest"
"testing"
"time"
"github.com/Nitro/sidecar/catalog"
"github.com/Nitro/sidecar/receiver"
"github.com/Nitro/sidecar/service"
. "github.com/smartystreets/goconvey/convey"
)
func Test_stateHandler(t *testing.T) {
Convey("stateHandler()", t, func() {
rcvr := &receiver.Receiver{
ReloadChan: make(chan time.Time, 10),
}
rcvr.OnUpdate = func(state *catalog.ServicesState) { rcvr.EnqueueUpdate() }
hostname := "chaucer"
state := catalog.NewServicesState()
state.Servers[hostname] = catalog.NewServer(hostname)
baseTime := time.Now().UTC()
svcId := "deadbeef123"
svcId2 := "deadbeef456"
svc := service.Service{
ID: svcId,
Name: "bocaccio",
Image: "101deadbeef",
Created: baseTime,
Hostname: hostname,
Updated: baseTime,
Status: service.ALIVE,
}
svc2 := service.Service{
ID: svcId2,
Name: "shakespeare",
Image: "202deadbeef",
Created: baseTime,
Hostname: hostname,
Updated: baseTime,
Status: service.ALIVE,
}
state.AddServiceEntry(svc)
state.AddServiceEntry(svc2)
req := httptest.NewRequest("GET", "/state", nil)
recorder := httptest.NewRecorder()
// Assign to the :( global
rcvr.CurrentState = state
Convey("sends the complete state, properly encoded", func() {
stateHandler(recorder, req, rcvr)
resp := recorder.Result()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
var received *catalog.ServicesState
json.Unmarshal(bodyBytes, &received)
So(received.HasServer("chaucer"), ShouldBeTrue)
So(received.Servers["chaucer"].HasService(svcId), ShouldBeTrue)
So(received.Servers["chaucer"].HasService(svcId2), ShouldBeTrue)
})
Convey("sends the right content-type", func() {
stateHandler(recorder, req, rcvr)
resp := recorder.Result()
So(resp.Header.Get("Content-Type"), ShouldEqual, "application/json")
})
})
}