forked from benalexau/ibconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_handler_test.go
86 lines (71 loc) · 2.53 KB
/
account_handler_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
83
84
85
86
package server
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/ant0ine/go-json-rest/rest/test"
"github.com/benalexau/ibconnect/core"
"github.com/benalexau/ibconnect/gateway"
)
func TestAccountHandlerGetAll(t *testing.T) {
ctx, handler := NewTestHandler(t)
defer ctx.Close()
c := core.NewTestConfig(t)
var ff gateway.FeedFactory = &gateway.AccountFeedFactory{AccountRefresh: c.AccountRefresh}
WaitForFeed(t, ctx, &ff, 15*time.Second)
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", "http://1.2.3.4/v1/accounts", nil))
recorded.CodeIs(http.StatusOK)
recorded.ContentTypeIsJson()
recorded.HeaderIs("Cache-Control", "private, max-age=60")
}
func TestAccountHandlerGetLatest(t *testing.T) {
ctx, handler := NewTestHandler(t)
defer ctx.Close()
c := core.NewTestConfig(t)
var ff gateway.FeedFactory = &gateway.AccountFeedFactory{AccountRefresh: c.AccountRefresh}
WaitForFeed(t, ctx, &ff, 15*time.Second)
accountCode := ""
row := ctx.DB.QueryRow("SELECT account_code FROM account LIMIT 1")
if err := row.Scan(&accountCode); err != nil {
t.Fatal(err)
}
url := fmt.Sprintf("http://1.2.3.4/v1/accounts/%s", accountCode)
recorded := test.RunRequest(t, handler, test.MakeSimpleRequest("GET", url, nil))
recorded.CodeIs(http.StatusSeeOther)
target := recorded.Recorder.Header().Get("Location")
recorded = test.RunRequest(t, handler, test.MakeSimpleRequest("GET", target, nil))
recorded.CodeIs(http.StatusOK)
recorded.ContentTypeIsJson()
recorded.HeaderIs("Cache-Control", "private, max-age=31556926")
}
func TestAccountHandlerGetAllRefresh(t *testing.T) {
ctx, handler := NewTestHandler(t)
defer ctx.Close()
c := core.NewTestConfig(t)
var ff gateway.FeedFactory = &gateway.AccountFeedFactory{AccountRefresh: c.AccountRefresh}
WaitForFeed(t, ctx, &ff, 15*time.Second)
nc := make(chan *core.Notification)
ctx.N.Subscribe(nc)
defer ctx.N.Unsubscribe(nc)
go func() {
req := test.MakeSimpleRequest("GET", "http://1.2.3.4/v1/accounts", nil)
req.Header.Add("Cache-Control", "private; max-age=0")
test.RunRequest(t, handler, req)
// NB: This request will not return data, as there's no feed running.
// Long timeouts avoided as the ctx.Close() closes the Notifier,
// which in turn closes the RefreshIfNeeded listener.
}()
outer:
for {
select {
case msg := <-nc:
if msg.Type == core.NtAccountRefresh {
// controller requested an account refresh, like it should have
break outer
}
case <-time.After(15 * time.Second):
t.Fatal("controller didn't request update before timeout")
}
}
}