-
Notifications
You must be signed in to change notification settings - Fork 1
/
cfcurl_test.go
189 lines (156 loc) · 5.8 KB
/
cfcurl_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
package cfcurl_test
import (
"bufio"
"errors"
"os"
. "github.com/krujos/cfcurl"
"github.com/cloudfoundry/cli/plugin/fakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Cfcurl", func() {
var fakeCliConnection *fakes.FakeCliConnection
Describe("an api that is not depricated", func() {
var v2apps []string
BeforeEach(func() {
fakeCliConnection = &fakes.FakeCliConnection{}
file, err := os.Open("apps.json")
defer file.Close()
if err != nil {
Fail("Could not open apps.json")
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
v2apps = append(v2apps, scanner.Text())
}
if scanner.Err() != nil {
Fail("Failed to read lines from file")
}
if 0 == len(v2apps) {
Fail("you didn't read anything in")
}
})
AfterEach(func() {
v2apps = nil
})
Describe("cf cli results validation", func() {
It("returns an error when there is no output", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(nil, nil)
appsJSON, err := Curl(fakeCliConnection, "/v2/apps")
Expect(err).ToNot(BeNil())
Expect(appsJSON).To(BeNil())
})
It("returns an error with zero length output", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns([]string{""}, nil)
appsJSON, err := Curl(fakeCliConnection, "/v2/apps")
Expect(err).ToNot(BeNil())
Expect(appsJSON).To(BeNil())
})
It("should call the path specified", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(v2apps, nil)
Curl(fakeCliConnection, "/v2/an_unpredictable_path")
args := fakeCliConnection.CliCommandWithoutTerminalOutputArgsForCall(0)
Expect("curl").To(Equal(args[0]))
Expect("/v2/an_unpredictable_path").To(Equal(args[1]))
})
It("returns an error when the cli fails", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(nil, errors.New("Something bad"))
appsJSON, err := Curl(fakeCliConnection, "/v2/an_unpredictable_path")
Expect(appsJSON).To(BeNil())
Expect(err).NotTo(BeNil())
})
})
Describe("we get legit json for apps", func() {
It("should return the output for apps", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(v2apps, nil)
appsJSON, err := Curl(fakeCliConnection, "/v2/apps")
Expect(err).To(BeNil())
Expect(appsJSON).ToNot(BeNil())
})
It("has a 2 results", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(v2apps, nil)
appsJSON, _ := Curl(fakeCliConnection, "/v2/apps")
Expect(appsJSON["total_results"]).To(Equal(float64(2)))
})
It("has a results array with two elements", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(v2apps, nil)
appsJSON, _ := Curl(fakeCliConnection, "/v2/apps")
Expect(len(appsJSON["resources"].([]interface{}))).To(Equal(2))
})
It("has a results array with two elements and uses the deprecated call", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(v2apps, nil)
appsJSON, _ := CurlDepricated(fakeCliConnection, "/v2/apps")
Expect(len(appsJSON["resources"].([]interface{}))).To(Equal(2))
})
})
})
Describe("A depricated API", func() {
var v2domains []string
BeforeEach(func() {
fakeCliConnection = &fakes.FakeCliConnection{}
file, err := os.Open("domains.json")
defer file.Close()
if err != nil {
Fail("Could not open apps.json")
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
v2domains = append(v2domains, scanner.Text())
}
if scanner.Err() != nil {
Fail("Failed to read lines from file")
}
if 0 == len(v2domains) {
Fail("you didn't read anything in")
}
})
AfterEach(func() {
v2domains = nil
})
Describe("cf cli results validation", func() {
It("returns an error when there is no output", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(nil, nil)
domainsJSON, err := CurlDepricated(fakeCliConnection, "/v2/domains")
Expect(err).ToNot(BeNil())
Expect(domainsJSON).To(BeNil())
})
It("returns an error with zero length output", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns([]string{""}, nil)
domainsJSON, err := CurlDepricated(fakeCliConnection, "/v2/domains")
Expect(err).ToNot(BeNil())
Expect(domainsJSON).To(BeNil())
})
It("should call the path specified", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(v2domains, nil)
CurlDepricated(fakeCliConnection, "/v2/an_unpredictable_path")
args := fakeCliConnection.CliCommandWithoutTerminalOutputArgsForCall(0)
Expect("curl").To(Equal(args[0]))
Expect("/v2/an_unpredictable_path").To(Equal(args[1]))
})
It("returns an error when the cli fails", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(nil, errors.New("Something bad"))
domainsJSON, err := CurlDepricated(fakeCliConnection, "/v2/an_unpredictable_path")
Expect(domainsJSON).To(BeNil())
Expect(err).NotTo(BeNil())
})
})
Describe("legit json for domains", func() {
It("should return the output for domains", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(v2domains, nil)
domainsJSON, err := CurlDepricated(fakeCliConnection, "/v2/domains")
Expect(err).To(BeNil())
Expect(domainsJSON).ToNot(BeNil())
})
It("has a 1 result", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(v2domains, nil)
domainsJSON, _ := CurlDepricated(fakeCliConnection, "/v2/domains")
Expect(domainsJSON["total_results"]).To(Equal(float64(1)))
})
It("has a results array with one element", func() {
fakeCliConnection.CliCommandWithoutTerminalOutputReturns(v2domains, nil)
domainsJSON, _ := CurlDepricated(fakeCliConnection, "/v2/domains")
Expect(len(domainsJSON["resources"].([]interface{}))).To(Equal(1))
})
})
})
})