Skip to content

Commit dc243eb

Browse files
committed
Fix InitiateCall
In addition, an integration test has been added.
1 parent 326d2a8 commit dc243eb

File tree

7 files changed

+67
-8
lines changed

7 files changed

+67
-8
lines changed

voice/call.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,13 @@ func InitiateCall(client *messagebird.Client, source, destination string, callfl
140140
body.Webhook.URL = webhook.URL
141141
body.Webhook.Token = webhook.Token
142142
}
143-
call := &Call{}
144-
err := client.Request(call, http.MethodPost, apiRoot+"/calls/", body)
145-
return call, err
143+
var resp struct {
144+
Data []Call `json:"data"`
145+
}
146+
if err := client.Request(&resp, http.MethodPost, apiRoot+"/calls", body); err != nil {
147+
return nil, err
148+
}
149+
return &resp.Data[0], nil
146150
}
147151

148152
// Delete deletes the Call.

voice/call_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package voice
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestInitiateCall(t *testing.T) {
9+
client, ok := testClient(t)
10+
if !ok {
11+
t.SkipNow()
12+
}
13+
14+
source, destination := "31000000000", "31000000000"
15+
callflow := CallFlow{
16+
Title: "Say test",
17+
Steps: []CallFlowStep{
18+
&CallFlowSayStep{
19+
Voice: "male",
20+
Payload: "You are about to experience a great adventure which reaches from the inner mind to the outer limits",
21+
Language: "en-US",
22+
},
23+
&CallFlowPauseStep{
24+
Length: time.Second,
25+
},
26+
&CallFlowHangupStep{},
27+
},
28+
}
29+
_, err := InitiateCall(client, source, destination, callflow, nil)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
}

voice/callflow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestCallFlowJSONUnmarshal(t *testing.T) {
131131
}
132132

133133
func TestCreateCallFlow(t *testing.T) {
134-
mbClient, stop := testClient(http.StatusOK, []byte(`{
134+
mbClient, stop := testRequest(http.StatusOK, []byte(`{
135135
"data": [
136136
{
137137
"id": "the-id",

voice/main_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/messagebird/go-rest-api"
1313
)
1414

15-
func testClient(status int, body []byte) (*messagebird.Client, func()) {
15+
func testRequest(status int, body []byte) (*messagebird.Client, func()) {
1616
mbServer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1717
w.Header().Set("Content-Type", "application/json")
1818
w.WriteHeader(status)
@@ -33,3 +33,25 @@ func testClient(status int, body []byte) (*messagebird.Client, func()) {
3333
}
3434
return mbClient, func() { mbServer.Close() }
3535
}
36+
37+
func testClient(t *testing.T) (*messagebird.Client, bool) {
38+
key, ok := os.LookupEnv("MB_TEST_KEY")
39+
if !ok {
40+
return nil, false
41+
}
42+
client := &messagebird.Client{
43+
AccessKey: key,
44+
HTTPClient: &http.Client{},
45+
DebugLog: log.New(testWriter{T: t}, "", 0),
46+
}
47+
return client, true
48+
}
49+
50+
type testWriter struct {
51+
T *testing.T
52+
}
53+
54+
func (tw testWriter) Write(b []byte) (int, error) {
55+
tw.T.Logf("%s", b)
56+
return len(b), nil
57+
}

voice/paginator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestPaginatorStream(t *testing.T) {
1010
type myStruct struct {
1111
Val int
1212
}
13-
mbClient, stop := testClient(http.StatusOK, []byte(`{
13+
mbClient, stop := testRequest(http.StatusOK, []byte(`{
1414
"data": [
1515
{ "Val": 1 },
1616
{ "Val": 2 },

voice/recording_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func TestRecordingGetFile(t *testing.T) {
1010
fileContents := []byte("this is not really a WAV file")
11-
mbClient, stop := testClient(http.StatusOK, []byte(fileContents))
11+
mbClient, stop := testRequest(http.StatusOK, []byte(fileContents))
1212
defer stop()
1313

1414
rec := &Recording{

voice/transcription_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func TestTranscriptionGetContents(t *testing.T) {
99
text := "the quick brown fox jumps over the lazy dog"
10-
mbClient, stop := testClient(http.StatusOK, []byte(text))
10+
mbClient, stop := testRequest(http.StatusOK, []byte(text))
1111
defer stop()
1212

1313
trans := &Transcription{

0 commit comments

Comments
 (0)