-
Notifications
You must be signed in to change notification settings - Fork 143
/
routes_test.go
107 lines (85 loc) · 2.7 KB
/
routes_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
package mailgun_test
import (
"context"
"testing"
"github.com/mailgun/mailgun-go/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRouteCRUD(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()
var countRoutes = func() int {
it := mg.ListRoutes(nil)
var page []mailgun.Route
it.Next(ctx, &page)
require.NoError(t, it.Err())
return it.TotalCount
}
routeCount := countRoutes()
newRoute, err := mg.CreateRoute(ctx, mailgun.Route{
Priority: 1,
Description: "Sample Route",
Expression: "match_recipient(\".*@samples.mailgun.org\")",
Actions: []string{
"forward(\"http://example.com/messages/\")",
"stop()",
},
})
require.NoError(t, err)
require.NotEqual(t, "", newRoute.Id)
defer func() {
require.NoError(t, mg.DeleteRoute(ctx, newRoute.Id))
_, err = mg.GetRoute(ctx, newRoute.Id)
require.NotNil(t, err)
}()
newCount := countRoutes()
require.False(t, newCount <= routeCount)
theRoute, err := mg.GetRoute(ctx, newRoute.Id)
require.NoError(t, err)
assert.Equal(t, newRoute, theRoute)
changedRoute, err := mg.UpdateRoute(ctx, newRoute.Id, mailgun.Route{
Priority: 2,
})
require.NoError(t, err)
assert.Equal(t, 2, changedRoute.Priority)
assert.Len(t, changedRoute.Actions, 2)
}
func TestRoutesIterator(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
it := mg.ListRoutes(&mailgun.ListOptions{Limit: 2})
var firstPage, secondPage, previousPage, lastPage []mailgun.Route
var ctx = context.Background()
// Calling Last() is invalid unless you first use First() or Next()
require.False(t, it.Last(ctx, &lastPage))
require.Len(t, lastPage, 0)
// Get our first page
require.True(t, it.Next(ctx, &firstPage))
require.NoError(t, it.Err())
require.True(t, len(firstPage) != 0)
firstIterator := *it
// Get our second page
require.True(t, it.Next(ctx, &secondPage))
require.NoError(t, it.Err())
require.True(t, len(secondPage) != 0)
// Pages should be different
require.NotEqual(t, firstPage, secondPage)
require.True(t, firstIterator.TotalCount != 0)
// Previous()
require.True(t, it.First(ctx, &firstPage))
require.True(t, it.Next(ctx, &secondPage))
require.True(t, it.Previous(ctx, &previousPage))
require.True(t, len(previousPage) != 0)
require.Equal(t, previousPage[0].Id, firstPage[0].Id)
// First()
require.True(t, it.First(ctx, &firstPage))
require.True(t, len(firstPage) != 0)
// Calling first resets the iterator to the first page
require.True(t, it.Next(ctx, &secondPage))
require.NotEqual(t, firstPage, secondPage)
// Last()
require.True(t, it.Last(ctx, &firstPage))
require.True(t, len(firstPage) != 0)
}