-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdisplayer_test.go
126 lines (104 loc) · 3.22 KB
/
displayer_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
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
// Helper that creates test responses
func makeResponse(body string, headers map[string][]string) *http.Response {
b := bytes.NewBufferString(body)
return &http.Response{
Body: ioutil.NopCloser(b),
Header: http.Header(headers),
}
}
var _ = Describe("Displayer", func() {
var (
resp *http.Response
displayer *Displayer
err error
)
JustBeforeEach(func() {
displayer, err = NewDisplayer(resp)
})
Context("with a response containing invalid JSON", func() {
var invalidJSON = "{notvalid}"
BeforeEach(func() {
resp = makeResponse(invalidJSON, nil)
})
It("NewDisplayer contains the raw value", func() {
Ω(displayer).ShouldNot(BeNil())
Ω(displayer.RawOutput).Should(Equal(invalidJSON))
Ω(err).Should(BeNil())
})
})
Context("with a response containing valid JSON", func() {
var (
validJSON = `{"foo":"bar","baz":42,"m":[{"a":1},{"a":2}]}`
)
BeforeEach(func() {
resp = makeResponse(validJSON, nil)
})
It("NewDisplayer returns a new displayer and no error", func() {
Ω(displayer).ShouldNot(BeNil())
Ω(err).Should(BeNil())
})
It("Applies json selects that extract a single value", func() {
Ω(displayer).ShouldNot(BeNil())
Ω(displayer.ApplySingleExtract(".baz")).ShouldNot(HaveOccurred())
Ω(displayer.RawOutput).Should(Equal(float64(42)))
})
It("Applies json selects that extract multiple values in JSON", func() {
Ω(displayer).ShouldNot(BeNil())
Ω(displayer.ApplyExtract(".m .a", true)).ShouldNot(HaveOccurred())
Ω(displayer.RawOutput).Should(Equal([]interface{}{float64(1), float64(2)}))
})
It("Applies json selects that extract multiple values not in JSON", func() {
Ω(displayer).ShouldNot(BeNil())
Ω(displayer.ApplyExtract(".m .a", false)).ShouldNot(HaveOccurred())
Ω(displayer.RawOutput).Should(Equal("1\n2\n"))
})
Context("with the go value corresponding to the JSON data", func() {
var (
value interface{}
)
BeforeEach(func() {
json.Unmarshal([]byte(validJSON), &value)
})
It("returns the output", func() {
Ω(displayer).ShouldNot(BeNil())
var output, _ = json.Marshal(value)
Ω(displayer.Output()).Should(Equal(string(output)))
})
It("prettifies", func() {
Ω(displayer).ShouldNot(BeNil())
displayer.Pretty()
var pretty, _ = json.MarshalIndent(value, "", " ")
Ω(displayer.Output()).Should(Equal(string(pretty) + "\n"))
})
})
})
Context("with a response containing headers", func() {
var (
headerValue = []string{"foo", "bar"}
headers = map[string][]string{"X-Test": headerValue}
)
BeforeEach(func() {
resp = makeResponse("", headers)
})
It("ApplyHeaderExtract returns the extracted header value", func() {
Ω(err).Should(BeNil())
Ω(displayer).ShouldNot(BeNil())
Ω(displayer.ApplyHeaderExtract("X-Test")).ShouldNot(HaveOccurred())
Ω(displayer.RawOutput).Should(Equal("foo"))
})
It("ApplyHeaderExtract returns an error if header is missing", func() {
Ω(err).Should(BeNil())
Ω(displayer).ShouldNot(BeNil())
Ω(displayer.ApplyHeaderExtract("X-NonExistent")).Should(HaveOccurred())
})
})
})