forked from sfreiberg/gotwilio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwiml_sms_test.go
143 lines (116 loc) · 3.73 KB
/
twiml_sms_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
package gotwilio
import (
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"testing"
)
// TestTWiMLSmsRenderBasic
// example from: https://www.twilio.com/docs/sms/twiml#a-basic-twiml-sms-response-example
func TestTWiMLSmsRenderBasic(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, testLookupResponse)
}))
defer srv.Close()
twilio := NewTwilioClient("", "")
twilio.LookupURL = srv.URL
// init
space := regexp.MustCompile(`\s+`)
var mr MessagingResponse
// add message
body := "hello world!"
redirect := "https://demo.twilio.com/welcome/sms/"
mr.Message(&TWiMLSmsMessage{
Body: &body,
Redirect: &redirect,
})
xml, err := mr.TWiMLSmsRender()
if err != nil {
t.Fatalf("failed to render xml: %+v", err)
}
expected := `<?xml version="1.0" encoding="UTF-8"?> <Response> <Message> <Body>hello world!</Body> <Redirect>https://demo.twilio.com/welcome/sms/</Redirect> </Message> </Response>`
if expected != space.ReplaceAllString(xml, " ") {
t.Fatalf("TestTWiMLSmsRenderBasic - unexpected xml")
}
}
func TestTWiMLSmsRenderSend2Messages(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, testLookupResponse)
}))
defer srv.Close()
twilio := NewTwilioClient("", "")
twilio.LookupURL = srv.URL
// init
var mr MessagingResponse
// message 1
mr.Message(&TWiMLSmsMessage{
Message: "This is message 1 of 2.",
})
// message 2
mr.Message(&TWiMLSmsMessage{
Message: "This is message 2 of 2.",
})
xml, err := mr.TWiMLSmsRender()
if err != nil {
t.Fatalf("failed to render xml: %+v", err)
}
space := regexp.MustCompile(`\s+`)
expected := `<?xmlversion="1.0"encoding="UTF-8"?><Response><Message>Thisismessage1of2.</Message><Message>Thisismessage2of2.</Message></Response>`
if expected != space.ReplaceAllString(xml, "") {
t.Fatalf("TestTWiMLSmsRenderSend2Messages - unexpected xml")
}
}
func TestTWiMLSmsRenderSendingMMS(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, testLookupResponse)
}))
defer srv.Close()
twilio := NewTwilioClient("", "")
twilio.LookupURL = srv.URL
// init
var mr MessagingResponse
// add message
body := "Store Location: 123 Easy St."
redirect := "https://demo.twilio.com/owl.png"
mr.Message(&TWiMLSmsMessage{
Body: &body,
Redirect: &redirect,
})
xml, err := mr.TWiMLSmsRender()
if err != nil {
t.Fatalf("failed to render message with MMS: %+v", err)
}
space := regexp.MustCompile(`\s+`)
expected := `<?xmlversion="1.0"encoding="UTF-8"?><Response><Message><Body>StoreLocation:123EasySt.</Body><Redirect>https://demo.twilio.com/owl.png</Redirect></Message></Response>`
if expected != space.ReplaceAllString(xml, "") {
t.Fatalf("TestTWiMLSmsRenderSendingMMS - unexpected xml")
}
}
func TestTWiMLSmsRenderMessageStatus(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, testLookupResponse)
}))
defer srv.Close()
twilio := NewTwilioClient("", "")
twilio.LookupURL = srv.URL
// init
var mr MessagingResponse
// add message
action := "/SmsHandler.php"
method := "POST"
mr.Message(&TWiMLSmsMessage{
Message: "Store Location: 123 Easy St.",
Action: &action,
Method: &method,
})
xml, err := mr.TWiMLSmsRender()
if err != nil {
t.Fatalf("failed to render message status: %+v", err)
}
space := regexp.MustCompile(`\s+`)
expected := `<?xmlversion="1.0"encoding="UTF-8"?><Response><MessageAction="/SmsHandler.php"Method="POST">StoreLocation:123EasySt.</Message></Response>`
if expected != space.ReplaceAllString(xml, "") {
t.Fatalf("TestTWiMLSmsRenderMessageStatus - unexpected xml")
}
}