Skip to content

Commit 80dc21f

Browse files
status filter added to sms list messages function (#73)
* status filter added to sms list messages function * fix Recording struct field name changed issue
1 parent e0543f9 commit 80dc21f

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

internal/mbtest/test_server.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package mbtest
22

33
import (
4+
"crypto/tls"
45
"io/ioutil"
6+
"net"
57
"net/http"
68
"net/http/httptest"
79
"net/url"
@@ -94,3 +96,18 @@ func WillReturnAccessKeyError() {
9496
}
9597
`), http.StatusUnauthorized)
9698
}
99+
100+
// HTTPTestTransport builds http transport that allows to pass custom http handler to http server
101+
func HTTPTestTransport(handler http.Handler) (*http.Transport, func()) {
102+
s := httptest.NewTLSServer(handler)
103+
104+
transport := &http.Transport{
105+
DialTLS: func(network, _ string) (net.Conn, error) {
106+
return tls.Dial(network, s.Listener.Addr().String(), &tls.Config{
107+
InsecureSkipVerify: true,
108+
})
109+
},
110+
}
111+
112+
return transport, s.Close
113+
}

sms/message.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type ListParams struct {
6161
Originator string
6262
Direction string
6363
Type string
64+
Status string
6465
Limit int
6566
Offset int
6667
}
@@ -190,6 +191,9 @@ func paramsForMessageList(params *ListParams) (*url.Values, error) {
190191
if params.Originator != "" {
191192
urlParams.Set("originator", params.Originator)
192193
}
194+
if params.Status != "" {
195+
urlParams.Set("status", params.Status)
196+
}
193197
if params.Limit != 0 {
194198
urlParams.Set("limit", strconv.Itoa(params.Limit))
195199
}

sms/message_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sms
22

33
import (
44
"net/http"
5+
"strings"
56
"testing"
67
"time"
78

@@ -308,6 +309,35 @@ func TestList(t *testing.T) {
308309
}
309310
}
310311

312+
func TestListScheduled(t *testing.T) {
313+
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
314+
expectedStatusFilter := "status=scheduled"
315+
if !strings.Contains(r.URL.String(), expectedStatusFilter) {
316+
t.Errorf("API call should contain filter by status (%v), but is is not %v", expectedStatusFilter, r.URL.String())
317+
}
318+
w.Write(mbtest.Testdata(t, "messageListScheduledObject.json"))
319+
})
320+
transport, teardown := mbtest.HTTPTestTransport(h)
321+
defer teardown()
322+
323+
client := mbtest.Client(t)
324+
client.HTTPClient.Transport = transport
325+
326+
messageList, err := List(client, &ListParams{Status: "scheduled"})
327+
if err != nil {
328+
t.Fatalf("Didn't expect an error while requesting Messages: %s", err)
329+
}
330+
if messageList.Count != 1 {
331+
t.Errorf("Unexpected result for the MessageList count: %d, expected: 1", messageList.Count)
332+
}
333+
if messageList.TotalCount != 1 {
334+
t.Errorf("Unexpected result for the MessageList total count: %d, expected: 1", messageList.TotalCount)
335+
}
336+
if len(messageList.Items) != 1 {
337+
t.Errorf("Unexpected result for the MessageList number of items in list: %d, expected: 1", len(messageList.Items))
338+
}
339+
}
340+
311341
func TestRequestDataForMessage(t *testing.T) {
312342
currentTime := time.Now()
313343
messageParams := &Params{
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"offset": 0,
3+
"limit": 20,
4+
"count": 1,
5+
"totalCount": 1,
6+
"links": {
7+
"first": "https://rest.messagebird.com/messages/?offset=0",
8+
"previous": null,
9+
"next": null,
10+
"last": "https://rest.messagebird.com/messages/?offset=0"
11+
},
12+
"items": [
13+
{
14+
"id": "6fe65f90454aa61536e6a88b88972670",
15+
"href": "https://rest.messagebird.com/messages/6fe65f90454aa61536e6a88b88972670",
16+
"direction": "mt",
17+
"type": "sms",
18+
"originator": "TestName",
19+
"body": "Hello World",
20+
"reference": null,
21+
"validity": null,
22+
"gateway": 239,
23+
"typeDetails": {},
24+
"datacoding": "plain",
25+
"mclass": 1,
26+
"scheduledDatetime": null,
27+
"createdDatetime": "2015-01-05T10:02:59+00:00",
28+
"recipients": {
29+
"totalCount": 1,
30+
"totalSentCount": 1,
31+
"totalDeliveredCount": 0,
32+
"totalDeliveryFailedCount": 0,
33+
"items": [
34+
{
35+
"recipient": 31612345678,
36+
"status": "scheduled",
37+
"statusDatetime": "2015-01-05T10:02:59+00:00"
38+
}
39+
]
40+
}
41+
}
42+
]
43+
}

voice/recording.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (rec *Recording) UnmarshalJSON(data []byte) error {
9595

9696
// Transcriptions returns a paginator for retrieving all Transcription objects.
9797
func (rec *Recording) Transcriptions(client *messagebird.Client, callID string) *Paginator {
98-
path := apiRoot+rec.links["self"]+"/transcriptions"
98+
path := apiRoot + rec.Links["self"] + "/transcriptions"
9999
return newPaginator(client, path, reflect.TypeOf(Transcription{}))
100100
}
101101

0 commit comments

Comments
 (0)