-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig_test.go
107 lines (90 loc) · 3 KB
/
config_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package client_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/tidepool-org/platform/client"
configTest "github.com/tidepool-org/platform/config/test"
testHttp "github.com/tidepool-org/platform/test/http"
)
var _ = Describe("Config", func() {
Context("NewConfig", func() {
It("returns successfully", func() {
cfg := client.NewConfig()
Expect(cfg).ToNot(BeNil())
})
It("returns default values", func() {
cfg := client.NewConfig()
Expect(cfg).ToNot(BeNil())
Expect(cfg.Address).To(BeEmpty())
Expect(cfg.UserAgent).To(BeEmpty())
})
})
Context("with new config", func() {
var address string
var userAgent string
var cfg *client.Config
BeforeEach(func() {
address = testHttp.NewAddress()
userAgent = testHttp.NewUserAgent()
cfg = client.NewConfig()
Expect(cfg).ToNot(BeNil())
})
Context("Load", func() {
var configReporter *configTest.Reporter
BeforeEach(func() {
configReporter = configTest.NewReporter()
configReporter.Config["address"] = address
configReporter.Config["user_agent"] = userAgent
})
It("returns an error if config reporter is missing", func() {
Expect(cfg.Load(nil)).To(MatchError("config reporter is missing"))
})
It("uses existing address if not set", func() {
existingAddress := testHttp.NewAddress()
cfg.Address = existingAddress
delete(configReporter.Config, "address")
Expect(cfg.Load(configReporter)).To(Succeed())
Expect(cfg.Address).To(Equal(existingAddress))
Expect(cfg.UserAgent).To(Equal(userAgent))
})
It("uses existing user agent if not set", func() {
existingUserAgent := testHttp.NewUserAgent()
cfg.UserAgent = existingUserAgent
delete(configReporter.Config, "user_agent")
Expect(cfg.Load(configReporter)).To(Succeed())
Expect(cfg.Address).To(Equal(address))
Expect(cfg.UserAgent).To(Equal(existingUserAgent))
})
It("returns successfully and uses values from config reporter", func() {
Expect(cfg.Load(configReporter)).To(Succeed())
Expect(cfg.Address).To(Equal(address))
Expect(cfg.UserAgent).To(Equal(userAgent))
})
})
Context("with valid values", func() {
BeforeEach(func() {
cfg.Address = address
cfg.UserAgent = userAgent
})
Context("Validate", func() {
It("returns an error if the address is missing", func() {
cfg.Address = ""
Expect(cfg.Validate()).To(MatchError("address is missing"))
})
It("returns an error if the address is not a parseable URL", func() {
cfg.Address = "Not%Parseable"
Expect(cfg.Validate()).To(MatchError("address is invalid: parse \"Not%Parseable\": invalid URL escape \"%Pa\""))
})
It("returns an error if the user agent is missing", func() {
cfg.UserAgent = ""
Expect(cfg.Validate()).To(MatchError("user agent is missing"))
})
It("returns success", func() {
Expect(cfg.Validate()).To(Succeed())
Expect(cfg.Address).To(Equal(address))
Expect(cfg.UserAgent).To(Equal(userAgent))
})
})
})
})
})