-
Notifications
You must be signed in to change notification settings - Fork 33
/
ApplePush_Tests.cs
153 lines (125 loc) · 5.79 KB
/
ApplePush_Tests.cs
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
using System;
using Newtonsoft.Json;
using Xunit;
namespace dotAPNS.Tests
{
public class ApplePush_Tests
{
[Fact]
public void Adding_ContentAvailable_To_Push_With_Badge_or_Sound_Fails()
{
var pushWithContentAvailable = ApplePush.CreateContentAvailable();
Assert.Throws<InvalidOperationException>(() => pushWithContentAvailable.AddBadge(0));
Assert.Throws<InvalidOperationException>(() => pushWithContentAvailable.AddSound("sound"));
}
[Fact]
public void Adding_Token_and_VoipToken_Together_Fails()
{
var pushWithToken = ApplePush.CreateContentAvailable().AddToken("token");
var pushWithVoipToken = ApplePush.CreateContentAvailable(true).AddVoipToken("voip");
var alertPushWithToken = ApplePush.CreateAlert(new ApplePushAlert("title", "body")).AddToken("token");
Assert.Throws<InvalidOperationException>(() => pushWithToken.AddVoipToken("voip"));
Assert.Throws<InvalidOperationException>(() => pushWithVoipToken.AddToken("token"));
Assert.Throws<InvalidOperationException>(() => alertPushWithToken.AddVoipToken("voip"));
}
[Fact]
public void Ensure_Type_Correspond_To_Payload()
{
var pushWithContentAvailable = ApplePush.CreateContentAvailable();
var pushWithAlert = ApplePush.CreateAlert(new ApplePushAlert("title", "body"));
Assert.Equal(ApplePushType.Background, pushWithContentAvailable.Type);
Assert.Equal(ApplePushType.Alert, pushWithAlert.Type);
}
[Fact]
public void Ensure_Priority_Corresponds_To_Payload()
{
var pushWithContentAvailable = ApplePush.CreateContentAvailable();
var pushWithAlert = ApplePush.CreateAlert(new ApplePushAlert("title", "body"));
Assert.Equal(5, pushWithContentAvailable.Priority);
Assert.Equal(10, pushWithAlert.Priority);
}
[Fact]
public void Adding_Voip_To_Alert_Push_Throws_InvalidOperationException()
{
var alertPush = ApplePush.CreateAlert(new ApplePushAlert("title", "body"));
Assert.Throws<InvalidOperationException>(() => alertPush.AddVoipToken("voip"));
}
[Fact]
public void Adding_Voip_To_NonVoip_Type_Throws_InvalidOperationException()
{
var backgroundPush = ApplePush.CreateContentAvailable();
var alert = ApplePush.CreateContentAvailable();
Assert.Throws<InvalidOperationException>(() => backgroundPush.AddVoipToken("voip"));
}
[Fact]
public void Adding_Token_To_Voip_Type_Throws_InvalidOperationException()
{
var backgroundPush = ApplePush.CreateContentAvailable(true);
Assert.Throws<InvalidOperationException>(() => backgroundPush.AddToken("token"));
}
[Fact]
public void CreateContentAvailable_Has_Background_Type()
{
var voipPush = ApplePush.CreateContentAvailable();
Assert.Equal(ApplePushType.Background, voipPush.Type);
}
[Fact]
public void CreateContentAvailableAsVoip_Has_Voip_Type()
{
var voipPush = ApplePush.CreateContentAvailable(true);
Assert.Equal(ApplePushType.Voip, voipPush.Type);
}
[Fact]
public void AddCustomProperty_Correctly_Adds_String_Value()
{
var push = ApplePush
.CreateAlert("testAlert")
.AddCustomProperty("customPropertyKey", "customPropertyValue");
var payload = push.GeneratePayload();
string payloadJson = JsonConvert.SerializeObject(payload);
const string referencePayloadJson = "{\"aps\":{\"alert\":\"testAlert\"},\"customPropertyKey\":\"customPropertyValue\"}";
Assert.Equal(referencePayloadJson, payloadJson);
}
[Fact]
public void AddCustomProperty_Correctly_Adds_Complex_Value()
{
var push = ApplePush
.CreateAlert("testAlert")
.AddCustomProperty("customPropertyKey", new { value1 = "123", value2 = 456 });
var payload = push.GeneratePayload();
string payloadJson = JsonConvert.SerializeObject(payload);
const string referencePayloadJson = "{\"aps\":{\"alert\":\"testAlert\"},\"customPropertyKey\":{\"value1\":\"123\",\"value2\":456}}";
Assert.Equal(referencePayloadJson, payloadJson);
}
[Fact]
public void Setting_Custom_Priority()
{
var push = ApplePush.CreateContentAvailable();
Assert.Equal(5, push.Priority);
push.SetPriority(10);
Assert.Equal(10, push.Priority);
}
[Fact]
public void AddContentAvailable()
{
var push = new ApplePush(ApplePushType.Background);
push.AddContentAvailable();
var payload = push.GeneratePayload();
string payloadJson = JsonConvert.SerializeObject(payload);
const string referenceJson = "{\"aps\":{\"content-available\":\"1\"}}";
Assert.Equal(referenceJson, payloadJson);
}
[Fact]
public void Creating_Push_With_ContentAvailable_MutableContent_Alert()
{
var push = new ApplePush(ApplePushType.Alert)
.AddContentAvailable()
.AddMutableContent()
.AddAlert("title", "body");
var payload = push.GeneratePayload();
string payloadJson = JsonConvert.SerializeObject(payload);
const string referenceJson = "{\"aps\":{\"content-available\":\"1\",\"mutable-content\":\"1\",\"alert\":{\"title\":\"title\",\"body\":\"body\"}}}";
Assert.Equal(referenceJson, payloadJson);
}
}
}