Skip to content

Commit 8c4aaa8

Browse files
committed
tests: add tests and implement CopyAndValidate method
1 parent fefb0da commit 8c4aaa8

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,75 @@ public void ApnsInvalidCriticalSoundVolumeTooHigh()
15581558
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
15591559
}
15601560

1561+
[Fact]
1562+
public void WebpushNotificationWithLinkUrl()
1563+
{
1564+
var message = new Message()
1565+
{
1566+
Topic = "test-topic",
1567+
Webpush = new WebpushConfig()
1568+
{
1569+
Notification = new WebpushNotification()
1570+
{
1571+
Title = "title",
1572+
Body = "body",
1573+
Icon = "icon",
1574+
},
1575+
Options = new WebpushFcmOptions()
1576+
{
1577+
Link = "https://www.firebase.io/",
1578+
},
1579+
},
1580+
};
1581+
var expected = new JObject()
1582+
{
1583+
{ "topic", "test-topic" },
1584+
{
1585+
"webpush", new JObject()
1586+
{
1587+
{
1588+
"notification", new JObject()
1589+
{
1590+
{ "title", "title" },
1591+
{ "body", "body" },
1592+
{ "icon", "icon" },
1593+
}
1594+
},
1595+
{
1596+
"fcm_options", new JObject()
1597+
{
1598+
{ "link", "https://www.firebase.io/" },
1599+
}
1600+
},
1601+
}
1602+
},
1603+
};
1604+
this.AssertJsonEquals(expected, message);
1605+
}
1606+
1607+
[Fact]
1608+
public void WebpushNotificationWithInvalidLinkUrl()
1609+
{
1610+
var message = new Message()
1611+
{
1612+
Topic = "test-topic",
1613+
Webpush = new WebpushConfig()
1614+
{
1615+
Notification = new WebpushNotification()
1616+
{
1617+
Title = "title",
1618+
Body = "body",
1619+
Icon = "icon",
1620+
},
1621+
Options = new WebpushFcmOptions()
1622+
{
1623+
Link = "http://www.firebase.io/",
1624+
},
1625+
},
1626+
};
1627+
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
1628+
}
1629+
15611630
private void AssertJsonEquals(JObject expected, Message actual)
15621631
{
15631632
var json = NewtonsoftJsonSerializer.Instance.Serialize(actual.CopyAndValidate());

FirebaseAdmin/FirebaseAdmin/Messaging/WebpushConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ internal WebpushConfig CopyAndValidate()
6161
Headers = this.Headers?.Copy(),
6262
Data = this.Data?.Copy(),
6363
Notification = this.Notification?.CopyAndValidate(),
64+
Options = this.Options?.CopyAndValidate(),
6465
};
6566
}
6667
}

FirebaseAdmin/FirebaseAdmin/Messaging/WebpushFcmOptions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,24 @@ public sealed class WebpushFcmOptions
2828
/// </summary>
2929
[JsonProperty("link")]
3030
public string Link { get; set; }
31+
32+
/// <summary>
33+
/// Copies this Webpush FCM options, and validates the content of it to ensure that it can
34+
/// be serialized into the JSON format expected by the FCM service.
35+
/// </summary>
36+
internal WebpushFcmOptions CopyAndValidate()
37+
{
38+
if (this.Link != null && !this.Link.StartsWith("https"))
39+
{
40+
throw new ArgumentException("The link options should be a valid https url.");
41+
}
42+
43+
var copy = new WebpushFcmOptions()
44+
{
45+
Link = this.Link,
46+
};
47+
48+
return copy;
49+
}
3150
}
3251
}

0 commit comments

Comments
 (0)