/
redirects_test.go
125 lines (99 loc) · 2.74 KB
/
redirects_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"testing"
)
var redirectXMLResponses = map[Redirect]string{
Redirect{Cab: "789"}: `
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Number>789</Number>
</Dial>
</Response>
`,
Redirect{Cab: "789", CabExtension: "100"}: `
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Dial>
<Number sendDigits="100">789</Number>
</Dial>
</Response>
`,
}
var responseJSON = []byte(`
{
"phone": "789"
}
`)
func TestLoadRedirectsFromYAML(t *testing.T) {
var (
redirects []Redirect
redirect Redirect
)
redirects = LoadRedirectsFromYAML("fixtures/redirects.yaml")
if len(redirects) != 2 {
t.Error("Should return a slice of 2 Redirects")
}
redirect = redirects[0]
if redirect.Id != "123" {
t.Error("Id should be set")
}
if redirect.Twilio != "456" {
t.Error("Twilio should be set")
}
if redirect.Cab != "789" {
t.Error("Cab should be set")
}
if redirect.CabExtension != "" {
t.Error("CabExtension should not be set")
}
redirectWithExtension := redirects[1]
if redirectWithExtension.CabExtension != "100" {
t.Error("CabExtension should be set")
}
}
func TestFindRedirectForTwilio(t *testing.T) {
redirects := LoadRedirectsFromYAML("fixtures/redirects.yaml")
if _, err := FindRedirectForTwilio(redirects, "456"); err != nil {
t.Error("Should not return an error for valid Twilio number")
}
if redirect, _ := FindRedirectForTwilio(redirects, "456"); redirect != redirects[0] {
t.Error("Should return Redirect for valid Twilio number")
}
if _, err := FindRedirectForTwilio(redirects, "999"); err == nil {
t.Error("Should return an error for invalid Twilio number")
}
}
func TestFindTwilioForID(t *testing.T) {
redirects := LoadRedirectsFromYAML("fixtures/redirects.yaml")
if _, err := FindTwilioForID(redirects, "123"); err != nil {
t.Error("Should not return an error for valid id")
}
if redirect, _ := FindTwilioForID(redirects, "123"); redirect != "456" {
t.Error("Should return Twilio number for valid id")
}
if _, err := FindTwilioForID(redirects, "000"); err == nil {
t.Error("Should return an error for invalid id")
}
}
func TestGenerateResponseXMLFor(t *testing.T) {
for redirect, response := range redirectXMLResponses {
expected := []byte(strings.TrimSpace(response))
result := GenerateResponseXMLFor(redirect)
if !bytes.Equal(expected, result) {
t.Error(fmt.Sprintf("Expected:\n%s\n\nGot:\n%s\n", expected, result))
}
}
}
func TestGenerateResponseJSONFor(t *testing.T) {
expected := new(bytes.Buffer)
_ = json.Compact(expected, responseJSON)
result := GenerateResponseJSONFor("789")
if !bytes.Equal(expected.Bytes(), result) {
t.Error(fmt.Sprintf("Expected:\n%s\n\nGot:\n%s\n", expected, result))
}
}