Skip to content

Commit

Permalink
Raise ArgumentException on null or empty IDs (#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe authored May 13, 2019
1 parent 2759786 commit fdd767c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Stripe.net/Services/_base/Service.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Stripe
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -301,6 +302,13 @@ protected virtual string ClassUrl()

protected virtual string InstanceUrl(string id)
{
if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentException(
"The resource ID cannot be null or whitespace.",
nameof(id));
}

return $"{this.ClassUrl()}/{WebUtility.UrlEncode(id)}";
}

Expand Down
22 changes: 22 additions & 0 deletions src/Stripe.net/Services/_base/ServiceNested.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Stripe
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -168,11 +169,32 @@ protected Task<EntityReturned> UpdateNestedEntityAsync(

protected virtual string ClassUrl(string parentId)
{
if (string.IsNullOrWhiteSpace(parentId))
{
throw new ArgumentException(
"The parent resource ID cannot be null or whitespace.",
nameof(parentId));
}

return this.BasePath.Replace("{PARENT_ID}", parentId);
}

protected virtual string InstanceUrl(string parentId, string id)
{
if (string.IsNullOrWhiteSpace(parentId))
{
throw new ArgumentException(
"The parent resource ID cannot be null or whitespace.",
nameof(parentId));
}

if (string.IsNullOrWhiteSpace(id))
{
throw new ArgumentException(
"The resource ID cannot be null or whitespace.",
nameof(id));
}

return $"{this.ClassUrl(parentId)}/{WebUtility.UrlEncode(id)}";
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/StripeTests/Services/_base/ServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace StripeTests
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
Expand All @@ -24,6 +25,33 @@ public void Get_ExpandProperties()
Assert.Contains("multi_word_property", client.LastOptions.Expand);
}

[Fact]
public void Get_ThrowsIfIdIsNull()
{
var client = new TestClient();
var service = new TestService { Client = client };

Assert.Throws<ArgumentException>(() => service.Get(null));
}

[Fact]
public void Get_ThrowsIfIdIsEmpty()
{
var client = new TestClient();
var service = new TestService { Client = client };

Assert.Throws<ArgumentException>(() => service.Get(string.Empty));
}

[Fact]
public void Get_ThrowsIfIdIsWhitespace()
{
var client = new TestClient();
var service = new TestService { Client = client };

Assert.Throws<ArgumentException>(() => service.Get(" "));
}

[Fact]
public void List_ExpandProperties()
{
Expand Down

0 comments on commit fdd767c

Please sign in to comment.