-
Notifications
You must be signed in to change notification settings - Fork 20
/
twiml_test.go
78 lines (68 loc) · 1.54 KB
/
twiml_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
package twiml
import (
"encoding/xml"
"strings"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestTwiml(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "TwiML Suite")
}
func x(s string, n int) string {
return strings.Join([]string{strings.Repeat(" ", n), s, "\n"}, "")
}
func buildResponse(s ...string) string {
t := []string{xml.Header, "<Response>\n"}
for _, verb := range s {
t = append(t, verb)
}
t = append(t, "</Response>")
return strings.Join(t, "")
}
var _ = Describe("TwiML responses", func() {
It("to error on an empty response", func() {
r := NewResponse()
_, err := r.String()
Expect(err).To(HaveOccurred())
})
It("can encode a basic verb in XML", func() {
r := NewResponse()
d := Dial{
Action: "https://testurl.com",
Number: "415-999-9999",
}
exp := buildResponse(
x("<Dial action=\"https://testurl.com\">415-999-9999</Dial>", 2),
)
r.Add(&d)
resp, err := r.String()
Expect(err).ToNot(HaveOccurred())
Expect(resp).To(Equal(exp))
})
It("can encode nested verbs and nouns", func() {
r := NewResponse()
d := Dial{
Number: "415-999-9999",
}
c := Client{
Name: "test",
}
exp := buildResponse(
x("<Dial>415-999-9999", 2),
x("<Client>test</Client>", 4),
x("</Dial>", 2),
)
d.Add(&c)
r.Add(&d)
s, err := r.String()
Expect(err).ToNot(HaveOccurred())
Expect(s).To(Equal(exp))
})
It("will validate markup and prevent encoding on errors", func() {
d := Dial{Method: "TEST"}
err := d.Validate()
Expect(err).To(HaveOccurred())
})
})