File size: 5,158 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package twilio

import (
	"context"
	"io"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/target/goalert/config"
)

func TestTwiMLResponse(t *testing.T) {
	t.Run("hangup", func(t *testing.T) {
		var mockConfig config.Config
		ctx := mockConfig.Context(context.Background())
		rec := httptest.NewRecorder()

		r := newTwiMLResponse(ctx, rec)
		r.Say("Hello")
		r.Hangup()

		resp := rec.Result()
		assert.Equal(t, http.StatusOK, resp.StatusCode)
		assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml")
		data, err := io.ReadAll(resp.Body)
		assert.NoError(t, err)
		assert.Equal(t, `<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Say>
		<prosody rate="slow">Hello</prosody>
	</Say>
	<Say>
		<prosody rate="slow">Goodbye.</prosody>
	</Say>
	<Hangup></Hangup>
</Response>`, string(data))
	})

	t.Run("redirect", func(t *testing.T) {
		var mockConfig config.Config
		mockConfig.Twilio.VoiceLanguage = "en-US"
		ctx := mockConfig.Context(context.Background())
		rec := httptest.NewRecorder()

		r := newTwiMLResponse(ctx, rec)
		r.Say("Hello")
		r.Redirect("http://example.com")

		resp := rec.Result()
		assert.Equal(t, http.StatusOK, resp.StatusCode)
		assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml")
		data, err := io.ReadAll(resp.Body)
		assert.NoError(t, err)
		assert.Equal(t, `<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Say language="en-US">
		<prosody rate="slow">Hello</prosody>
	</Say>
	<Redirect>http://example.com</Redirect>
</Response>`, string(data))
	})

	t.Run("redirect-pause", func(t *testing.T) {
		var mockConfig config.Config
		mockConfig.Twilio.VoiceName = "Polly.Joanna-Neural"
		mockConfig.Twilio.VoiceLanguage = "en-US"
		ctx := mockConfig.Context(context.Background())
		rec := httptest.NewRecorder()

		r := newTwiMLResponse(ctx, rec)
		r.Say("Hello! This is GoAlert.")
		r.RedirectPauseSec("http://example.com", 3)

		resp := rec.Result()
		assert.Equal(t, http.StatusOK, resp.StatusCode)
		assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml")
		data, err := io.ReadAll(resp.Body)
		assert.NoError(t, err)
		assert.Equal(t, `<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Say language="en-US" voice="Polly.Joanna-Neural">
		<prosody rate="slow">Hello! This is GoAlert.</prosody>
	</Say>
	<Pause length="3"></Pause>
	<Redirect>http://example.com</Redirect>
</Response>`, string(data))
	})

	t.Run("unknown-gather", func(t *testing.T) {
		var mockConfig config.Config
		ctx := mockConfig.Context(context.Background())
		rec := httptest.NewRecorder()

		r := newTwiMLResponse(ctx, rec)
		r.SayUnknownDigit()
		r.Say("Hello")
		r.Gather("http://example.com")

		resp := rec.Result()
		assert.Equal(t, http.StatusOK, resp.StatusCode)
		assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml")
		data, err := io.ReadAll(resp.Body)
		assert.NoError(t, err)
		assert.Equal(t, `<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Gather numDigits="1" timeout="10" action="http://example.com">
		<Say>
			<prosody rate="slow">Sorry, I didn&#39;t understand that.</prosody>
		</Say>
		<Say>
			<prosody rate="slow">Hello</prosody>
		</Say>
		<Say>
			<prosody rate="slow">If you are done, you may simply hang up.</prosody>
		</Say>
		<Say>
			<prosody rate="slow">To repeat this message, press star.</prosody>
		</Say>
	</Gather>
</Response>`, string(data))
	})

	t.Run("ack test", func(t *testing.T) {
		var mockConfig config.Config
		ctx := mockConfig.Context(context.Background())
		rec := httptest.NewRecorder()

		r := newTwiMLResponse(ctx, rec)
		r.Say("Hello")
		r.AddOptions(optionAck)
		r.Gather("http://example.com")

		resp := rec.Result()
		assert.Equal(t, http.StatusOK, resp.StatusCode)
		assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml")
		data, err := io.ReadAll(resp.Body)
		assert.NoError(t, err)
		assert.Equal(t, `<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Gather numDigits="1" timeout="10" action="http://example.com">
		<Say>
			<prosody rate="slow">Hello</prosody>
		</Say>
		<Say>
			<prosody rate="slow">To acknowledge, press 4.</prosody>
		</Say>
		<Say>
			<prosody rate="slow">To repeat this message, press star.</prosody>
		</Say>
	</Gather>
</Response>`, string(data))
	})
	t.Run("esc test", func(t *testing.T) {
		var mockConfig config.Config
		ctx := mockConfig.Context(context.Background())
		rec := httptest.NewRecorder()

		r := newTwiMLResponse(ctx, rec)
		r.Say("Hello")
		r.AddOptions(optionEscalate)
		r.Gather("http://example.com")

		resp := rec.Result()
		assert.Equal(t, http.StatusOK, resp.StatusCode)
		assert.Contains(t, resp.Header.Get("Content-Type"), "application/xml")
		data, err := io.ReadAll(resp.Body)
		assert.NoError(t, err)
		assert.Equal(t, `<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Gather numDigits="1" timeout="10" action="http://example.com">
		<Say>
			<prosody rate="slow">Hello</prosody>
		</Say>
		<Say>
			<prosody rate="slow">To escalate, press 5.</prosody>
		</Say>
		<Say>
			<prosody rate="slow">To repeat this message, press star.</prosody>
		</Say>
	</Gather>
</Response>`, string(data))
	})
}