@@ -0,0 +1,194 @@
1+package options
2+
3+import (
4+ "time"
5+
6+ . "github.com/onsi/ginkgo"
7+ . "github.com/onsi/ginkgo/extensions/table"
8+ . "github.com/onsi/gomega"
9+)
10+
11+var _ = Describe("Legacy Options", func() {
12+ Context("ToOptions", func() {
13+ It("converts the options as expected", func() {
14+ opts := NewOptions()
15+
16+ legacyOpts := NewLegacyOptions()
17+
18+ // Set upstreams and related options to test their conversion
19+ flushInterval := 5 * time.Second
20+ legacyOpts.LegacyFlushInterval = flushInterval
21+ legacyOpts.LegacyPassHostHeader = true
22+ legacyOpts.LegacyProxyWebSockets = true
23+ legacyOpts.LegacySSLUpstreamInsecureSkipVerify = true
24+ legacyOpts.LegacyUpstreams = []string{"http://foo.bar/baz", "file://var/lib/website#/bar"}
25+
26+ opts.UpstreamServers = Upstreams{
27+ {
28+ ID: "/baz",
29+ Path: "/baz",
30+ URI: "http://foo.bar/baz",
31+ FlushInterval: &flushInterval,
32+ InsecureSkipTLSVerify: true,
33+ PassHostHeader: true,
34+ ProxyWebSockets: true,
35+ },
36+ {
37+ ID: "/bar",
38+ Path: "/bar",
39+ URI: "file://var/lib/website#/bar",
40+ FlushInterval: &flushInterval,
41+ InsecureSkipTLSVerify: true,
42+ PassHostHeader: true,
43+ ProxyWebSockets: true,
44+ },
45+ }
46+
47+ converted, err := legacyOpts.ToOptions()
48+ Expect(err).ToNot(HaveOccurred())
49+ Expect(converted).To(Equal(opts))
50+ })
51+ })
52+
53+ Context("Legacy Upstreams", func() {
54+ type convertUpstreamsTableInput struct {
55+ upstreamStrings []string
56+ expectedUpstreams Upstreams
57+ errMsg string
58+ }
59+
60+ defaultFlushInterval := 1 * time.Second
61+
62+ // Non defaults for these options
63+ skipVerify := true
64+ passHostHeader := false
65+ proxyWebSockets := true
66+ flushInterval := 5 * time.Second
67+
68+ // Test cases and expected outcomes
69+ validHTTP := "http://foo.bar/baz"
70+ validHTTPUpstream := Upstream{
71+ ID: "/baz",
72+ Path: "/baz",
73+ URI: validHTTP,
74+ InsecureSkipTLSVerify: skipVerify,
75+ PassHostHeader: passHostHeader,
76+ ProxyWebSockets: proxyWebSockets,
77+ FlushInterval: &flushInterval,
78+ }
79+
80+ // Test cases and expected outcomes
81+ emptyPathHTTP := "http://foo.bar"
82+ emptyPathHTTPUpstream := Upstream{
83+ ID: "/",
84+ Path: "/",
85+ URI: emptyPathHTTP,
86+ InsecureSkipTLSVerify: skipVerify,
87+ PassHostHeader: passHostHeader,
88+ ProxyWebSockets: proxyWebSockets,
89+ FlushInterval: &flushInterval,
90+ }
91+
92+ validFileWithFragment := "file://var/lib/website#/bar"
93+ validFileWithFragmentUpstream := Upstream{
94+ ID: "/bar",
95+ Path: "/bar",
96+ URI: validFileWithFragment,
97+ InsecureSkipTLSVerify: skipVerify,
98+ PassHostHeader: passHostHeader,
99+ ProxyWebSockets: proxyWebSockets,
100+ FlushInterval: &flushInterval,
101+ }
102+
103+ validStatic := "static://204"
104+ validStaticCode := 204
105+ validStaticUpstream := Upstream{
106+ ID: validStatic,
107+ Path: validStatic,
108+ URI: "",
109+ Static: true,
110+ StaticCode: &validStaticCode,
111+ InsecureSkipTLSVerify: false,
112+ PassHostHeader: true,
113+ ProxyWebSockets: false,
114+ FlushInterval: &defaultFlushInterval,
115+ }
116+
117+ invalidStatic := "static://abc"
118+ invalidStaticCode := 200
119+ invalidStaticUpstream := Upstream{
120+ ID: invalidStatic,
121+ Path: invalidStatic,
122+ URI: "",
123+ Static: true,
124+ StaticCode: &invalidStaticCode,
125+ InsecureSkipTLSVerify: false,
126+ PassHostHeader: true,
127+ ProxyWebSockets: false,
128+ FlushInterval: &defaultFlushInterval,
129+ }
130+
131+ invalidHTTP := ":foo"
132+ invalidHTTPErrMsg := "could not parse upstream \":foo\": parse \":foo\": missing protocol scheme"
133+
134+ DescribeTable("convertLegacyUpstreams",
135+ func(o *convertUpstreamsTableInput) {
136+ upstreams, err := convertLegacyUpstreams(o.upstreamStrings, skipVerify, passHostHeader, proxyWebSockets, flushInterval)
137+
138+ if o.errMsg != "" {
139+ Expect(err).To(HaveOccurred())
140+ Expect(err.Error()).To(Equal(o.errMsg))
141+ } else {
142+ Expect(err).ToNot(HaveOccurred())
143+ }
144+
145+ Expect(upstreams).To(ConsistOf(o.expectedUpstreams))
146+ },
147+ Entry("with no upstreams", &convertUpstreamsTableInput{
148+ upstreamStrings: []string{},
149+ expectedUpstreams: Upstreams{},
150+ errMsg: "",
151+ }),
152+ Entry("with a valid HTTP upstream", &convertUpstreamsTableInput{
153+ upstreamStrings: []string{validHTTP},
154+ expectedUpstreams: Upstreams{validHTTPUpstream},
155+ errMsg: "",
156+ }),
157+ Entry("with a HTTP upstream with an empty path", &convertUpstreamsTableInput{
158+ upstreamStrings: []string{emptyPathHTTP},
159+ expectedUpstreams: Upstreams{emptyPathHTTPUpstream},
160+ errMsg: "",
161+ }),
162+ Entry("with a valid File upstream with a fragment", &convertUpstreamsTableInput{
163+ upstreamStrings: []string{validFileWithFragment},
164+ expectedUpstreams: Upstreams{validFileWithFragmentUpstream},
165+ errMsg: "",
166+ }),
167+ Entry("with a valid static upstream", &convertUpstreamsTableInput{
168+ upstreamStrings: []string{validStatic},
169+ expectedUpstreams: Upstreams{validStaticUpstream},
170+ errMsg: "",
171+ }),
172+ Entry("with an invalid static upstream, code is 200", &convertUpstreamsTableInput{
173+ upstreamStrings: []string{invalidStatic},
174+ expectedUpstreams: Upstreams{invalidStaticUpstream},
175+ errMsg: "",
176+ }),
177+ Entry("with an invalid HTTP upstream", &convertUpstreamsTableInput{
178+ upstreamStrings: []string{invalidHTTP},
179+ expectedUpstreams: Upstreams{},
180+ errMsg: invalidHTTPErrMsg,
181+ }),
182+ Entry("with an invalid HTTP upstream and other upstreams", &convertUpstreamsTableInput{
183+ upstreamStrings: []string{validHTTP, invalidHTTP},
184+ expectedUpstreams: Upstreams{},
185+ errMsg: invalidHTTPErrMsg,
186+ }),
187+ Entry("with multiple valid upstreams", &convertUpstreamsTableInput{
188+ upstreamStrings: []string{validHTTP, validFileWithFragment, validStatic},
189+ expectedUpstreams: Upstreams{validHTTPUpstream, validFileWithFragmentUpstream, validStaticUpstream},
190+ errMsg: "",
191+ }),
192+ )
193+ })
194+})