|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Runtime.Serialization; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using Newtonsoft.Json.Converters; |
| 6 | +using FluentAssertions; |
| 7 | +using Xunit; |
| 8 | +using Stripe.Infrastructure; |
| 9 | + |
| 10 | +namespace Stripe.Tests.Xunit |
| 11 | +{ |
| 12 | + public class encoding_parameters |
| 13 | + { |
| 14 | + public class TestObject |
| 15 | + { |
| 16 | + [JsonProperty("an_int")] |
| 17 | + public int? AnInt { get; set; } |
| 18 | + |
| 19 | + [JsonProperty("a_string")] |
| 20 | + public string AString { get; set; } |
| 21 | + |
| 22 | + [JsonProperty("a_dict")] |
| 23 | + public Dictionary<string, string> ADict { get; set; } |
| 24 | + |
| 25 | + [JsonProperty("a_list")] |
| 26 | + public int[] AList { get; set; } |
| 27 | + } |
| 28 | + |
| 29 | + public class UnencodableObject |
| 30 | + { |
| 31 | + [JsonProperty("dict_int_keys")] |
| 32 | + public Dictionary<int, string> DictIntKeys { get; set; } |
| 33 | + |
| 34 | + [JsonProperty("dict_int_values")] |
| 35 | + public Dictionary<string, int> DictIntValues { get; set; } |
| 36 | + } |
| 37 | + |
| 38 | + public class TestService : StripeService |
| 39 | + { |
| 40 | + public TestService() : base(null) |
| 41 | + { |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public TestService Service { get; } |
| 46 | + |
| 47 | + public encoding_parameters() |
| 48 | + { |
| 49 | + Service = new TestService(); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void parameters_should_be_encoded() |
| 54 | + { |
| 55 | + var obj = new TestObject |
| 56 | + { |
| 57 | + AnInt = 3, |
| 58 | + AString = "+foo?", |
| 59 | + ADict = new Dictionary<string, string> |
| 60 | + { |
| 61 | + {"a", "A"}, |
| 62 | + {"b", "B"}, |
| 63 | + }, |
| 64 | + AList = new int[] { 1, 2 }, |
| 65 | + }; |
| 66 | + var url = Service.ApplyAllParameters(obj, "", false); |
| 67 | + url.Should().Be("?an_int=3&a_string=%2Bfoo%3F&a_dict[a]=A&a_dict[b]=B&a_list[]=1&a_list[]=2"); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void empty_strings_should_be_encoded() |
| 72 | + { |
| 73 | + var obj = new TestObject |
| 74 | + { |
| 75 | + AString = "", |
| 76 | + }; |
| 77 | + var url = Service.ApplyAllParameters(obj, "", false); |
| 78 | + url.Should().Be("?a_string="); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void dictionary_keys_should_be_encoded() |
| 83 | + { |
| 84 | + var obj = new TestObject |
| 85 | + { |
| 86 | + ADict = new Dictionary<string, string> |
| 87 | + { |
| 88 | + {"invoice #", "1234"}, |
| 89 | + }, |
| 90 | + }; |
| 91 | + var url = Service.ApplyAllParameters(obj, "", false); |
| 92 | + url.Should().Be("?a_dict[invoice+%23]=1234"); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void dictionary_values_should_be_encoded() |
| 97 | + { |
| 98 | + var obj = new TestObject |
| 99 | + { |
| 100 | + ADict = new Dictionary<string, string> |
| 101 | + { |
| 102 | + {"invoice", "#1234"}, |
| 103 | + }, |
| 104 | + }; |
| 105 | + var url = Service.ApplyAllParameters(obj, "", false); |
| 106 | + url.Should().Be("?a_dict[invoice]=%231234"); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public void throws_if_dictionary_keys_are_not_strings() |
| 111 | + { |
| 112 | + var obj = new UnencodableObject |
| 113 | + { |
| 114 | + DictIntKeys = new Dictionary<int, string> |
| 115 | + { |
| 116 | + {1, "one"}, |
| 117 | + }, |
| 118 | + }; |
| 119 | + |
| 120 | + var exception = Assert.Throws<System.ArgumentException>(() => |
| 121 | + Service.ApplyAllParameters(obj, "", false) |
| 122 | + ); |
| 123 | + |
| 124 | + exception.Message.Should().Contain("Expected System.String as dictionary key type"); |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + public void throws_if_dictionary_values_are_not_strings() |
| 129 | + { |
| 130 | + var obj = new UnencodableObject |
| 131 | + { |
| 132 | + DictIntValues = new Dictionary<string, int> |
| 133 | + { |
| 134 | + {"one", 1}, |
| 135 | + }, |
| 136 | + }; |
| 137 | + |
| 138 | + var exception = Assert.Throws<System.ArgumentException>(() => |
| 139 | + Service.ApplyAllParameters(obj, "", false) |
| 140 | + ); |
| 141 | + |
| 142 | + exception.Message.Should().Contain("Expected System.String as dictionary value type"); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments