forked from cloudfoundry/cli-plugin-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
206 lines (165 loc) · 5.75 KB
/
main_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"fmt"
"net/http"
"os/exec"
"strconv"
"io/ioutil"
"time"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/types"
)
var _ = Describe("Integration", func() {
var (
session *gexec.Session
port string
)
Context("--force-ssl not set", func() {
BeforeEach(func() {
port = strconv.Itoa(8080 + GinkgoParallelNode())
var err error
session, err = gexec.Start(
exec.Command(buildPath, "-p", port, "-f", "fixtures/repo-index.yml"),
GinkgoWriter,
GinkgoWriter,
)
Expect(err).NotTo(HaveOccurred())
time.Sleep(time.Second)
})
AfterEach(func() {
session.Kill()
})
Describe("/", func() {
It("returns HTML we expect", func() {
client := http.DefaultClient
response, err := client.Get("http://127.0.0.1:" + port)
Expect(err).NotTo(HaveOccurred())
Expect(response).To(BeSuccessful())
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
Expect(err).NotTo(HaveOccurred())
// sanity test that at least one thing is present
Expect(contents).To(ContainSubstring("doctor scans your deployed applications"))
// and that the template finishes rendering without aborting due to an error
Expect(contents).To(ContainSubstring("</html>"))
})
})
Describe("/list", func() {
It("returns json that looks like we expect it", func() {
client := http.DefaultClient
response, err := client.Get("http://127.0.0.1:" + port + "/list")
Expect(err).NotTo(HaveOccurred())
Expect(response).To(BeSuccessful())
b, err := ioutil.ReadFile("fixtures/repo-index-response.json")
Expect(err).NotTo(HaveOccurred())
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
Expect(err).NotTo(HaveOccurred())
Expect(string(contents)).To(Equal(string(b)))
})
})
Describe("/ui", func() {
It("redirects to index", func() {
client := http.DefaultClient
response, err := client.Get("http://127.0.0.1:" + port + "/ui")
Expect(err).NotTo(HaveOccurred())
Expect(response).To(BeSuccessful())
Expect(response.Request.URL.Path).To(Equal("/"))
})
})
})
Context("--force-ssl is set", func() {
BeforeEach(func() {
port = strconv.Itoa(8080 + GinkgoParallelNode())
var err error
session, err = gexec.Start(
exec.Command(buildPath, "-p", port, "-f", "fixtures/repo-index.yml", "--force-ssl"),
GinkgoWriter,
GinkgoWriter,
)
Expect(err).NotTo(HaveOccurred())
time.Sleep(time.Second)
})
AfterEach(func() {
session.Kill()
})
Context("when 'x-forwarded-proto' is set to 'http'", func() {
Describe("/", func() {
It("redirects to the https url", func() {
transport := http.Transport{}
request, err := http.NewRequest("GET", "http://127.0.0.1:"+port, nil)
Expect(err).NotTo(HaveOccurred())
request.Header.Set("x-forwarded-proto", "http")
response, err := transport.RoundTrip(request)
Expect(err).NotTo(HaveOccurred())
Expect(response).To(BeSuccessful())
redirectLocation, err := response.Location()
Expect(err).NotTo(HaveOccurred())
Expect(redirectLocation).To(MatchRegexp("^https:"))
})
})
Describe("/list", func() {
It("redirects to the https url", func() {
transport := http.Transport{}
request, err := http.NewRequest("GET", "http://127.0.0.1:"+port+"/list", nil)
Expect(err).NotTo(HaveOccurred())
request.Header.Set("x-forwarded-proto", "http")
response, err := transport.RoundTrip(request)
Expect(err).NotTo(HaveOccurred())
Expect(response).To(BeSuccessful())
redirectLocation, err := response.Location()
Expect(err).NotTo(HaveOccurred())
Expect(redirectLocation).To(MatchRegexp("^https:"))
})
})
Describe("/ui", func() {
It("redirects to the https url", func() {
transport := http.Transport{}
request, err := http.NewRequest("GET", "http://127.0.0.1:"+port+"/ui", nil)
Expect(err).NotTo(HaveOccurred())
request.Header.Set("x-forwarded-proto", "http")
response, err := transport.RoundTrip(request)
Expect(err).NotTo(HaveOccurred())
Expect(response).To(BeSuccessful())
redirectLocation, err := response.Location()
Expect(err).NotTo(HaveOccurred())
Expect(redirectLocation).To(MatchRegexp("^https:"))
})
})
Describe("https request", func() {
It("does not do a redirect", func() {
transport := http.Transport{}
request, err := http.NewRequest("GET", "http://127.0.0.1:"+port, nil)
Expect(err).NotTo(HaveOccurred())
request.Header.Set("x-forwarded-proto", "https")
response, err := transport.RoundTrip(request)
Expect(err).NotTo(HaveOccurred())
Expect(response).To(BeSuccessful())
_, err = response.Location()
Expect(err).To(HaveOccurred())
})
})
})
})
})
func BeSuccessful() types.GomegaMatcher {
return &SuccessfulHTTPResponseMatcher{}
}
type SuccessfulHTTPResponseMatcher struct{}
func (matcher *SuccessfulHTTPResponseMatcher) Match(actual interface{}) (success bool, err error) {
response, ok := actual.(*http.Response)
if !ok {
return false, fmt.Errorf("SuccessfulHTTPResponseMatcher matcher expects an http.Response")
}
return (response.StatusCode >= 200) && (response.StatusCode < 400), nil
}
func (matcher *SuccessfulHTTPResponseMatcher) FailureMessage(actual interface{}) (message string) {
response := actual.(*http.Response)
return fmt.Sprintf("Expected Status Code\n\t%d\nto be successful (2XX or 3XX)", response.StatusCode)
}
func (matcher *SuccessfulHTTPResponseMatcher) NegatedFailureMessage(actual interface{}) (message string) {
response := actual.(*http.Response)
return fmt.Sprintf("Expected Status Code\n\t%d\nto not be successful (1XX, 4XX, 5XX)", response.StatusCode)
}