PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/wine-mono-0.0.4/mono/external/aspnetwebstack/test/System.Net.Http.Formatting.Test.Unit/UriExtensionsTests.cs

#
C# | 175 lines | 149 code | 25 blank | 1 comment | 1 complexity | cd8133e3363d73e8b75c8e7c3f343732 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, Unlicense, BSD-3-Clause, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, ISC, Apache-2.0, LGPL-2.0
  1. // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
  2. using System.Collections.Specialized;
  3. using System.Net.Http.Formatting.DataSets;
  4. using Microsoft.TestCommon;
  5. using Newtonsoft.Json.Linq;
  6. using Xunit;
  7. using Xunit.Extensions;
  8. using Assert = Microsoft.TestCommon.AssertEx;
  9. namespace System.Net.Http
  10. {
  11. public class UriExtensionsTests
  12. {
  13. private static readonly Uri TestAddress = new Uri("http://www.example.com");
  14. private static readonly Type TestType = typeof(string);
  15. [Fact]
  16. [Trait("Description", "UriExtensionMethods is public and static.")]
  17. public void TypeIsCorrect()
  18. {
  19. Assert.Type.HasProperties(typeof(UriExtensions), TypeAssert.TypeProperties.IsPublicVisibleClass | TypeAssert.TypeProperties.IsStatic);
  20. }
  21. [Fact]
  22. [Trait("Description", "ParseQueryString(Uri) throws with null 'this'.")]
  23. public void ParseQueryStringThrowsWithNull()
  24. {
  25. Assert.ThrowsArgumentNull(() => ((Uri)null).ParseQueryString(), "address");
  26. }
  27. [Theory]
  28. [TestDataSet(typeof(HttpUnitTestDataSets), "Uris")]
  29. [Trait("Description", "ParseQueryString(Uri) succeeds with valid URIs.")]
  30. public void ParseQueryStringSucceeds(Uri address)
  31. {
  32. NameValueCollection result = address.ParseQueryString();
  33. Assert.NotNull(result);
  34. bool addressContainsQuery = address.Query.Contains("?");
  35. if (!addressContainsQuery)
  36. {
  37. Assert.Empty(result);
  38. }
  39. else
  40. {
  41. Assert.True(result.Count > 0, "Uri with query string should return non-empty set.");
  42. }
  43. }
  44. [Fact]
  45. [Trait("Description", "TryReadQueryAsJson(Uri, out JsonObject) throws with null 'this'.")]
  46. public void TryReadQueryAsJsonThrowsWithNull()
  47. {
  48. JObject value;
  49. Assert.ThrowsArgumentNull(() => ((Uri)null).TryReadQueryAsJson(out value), "address");
  50. }
  51. [Theory]
  52. [TestDataSet(typeof(HttpUnitTestDataSets), "Uris")]
  53. [Trait("Description", "TryReadQueryAsJson(Uri, out JsonObject) succeeds with valid URIs.")]
  54. public void TryReadQueryAsJsonSucceeds(Uri address)
  55. {
  56. JObject value;
  57. Assert.True(address.TryReadQueryAsJson(out value), "Expected 'true' as result");
  58. Assert.NotNull(value);
  59. Assert.IsType<JObject>(value);
  60. }
  61. [Fact]
  62. [Trait("Description", "TryReadQueryAs(Uri, Type, out object) throws with null 'this'.")]
  63. public void TryReadQueryAsThrowsWithNull()
  64. {
  65. object value;
  66. Assert.ThrowsArgumentNull(() => ((Uri)null).TryReadQueryAs(TestType, out value), "address");
  67. Assert.ThrowsArgumentNull(() => TestAddress.TryReadQueryAs(null, out value), "type");
  68. }
  69. [Fact]
  70. [Trait("Description", "TryReadQueryAs(Uri, Type, out object) succeeds with valid URIs.")]
  71. public void TryReadQueryAsSucceeds()
  72. {
  73. object value;
  74. UriBuilder address = new UriBuilder("http://some.host");
  75. address.Query = "a=2";
  76. Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject1), out value), "Expected 'true' reading valid data");
  77. SimpleObject1 so1 = (SimpleObject1)value;
  78. Assert.NotNull(so1);
  79. Assert.Equal(2, so1.a);
  80. address.Query = "b=true";
  81. Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject2), out value), "Expected 'true' reading valid data");
  82. SimpleObject2 so2 = (SimpleObject2)value;
  83. Assert.NotNull(so2);
  84. Assert.True(so2.b, "Value should have been true");
  85. address.Query = "c=hello";
  86. Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject3), out value), "Expected 'true' reading valid data");
  87. SimpleObject3 so3 = (SimpleObject3)value;
  88. Assert.NotNull(so3);
  89. Assert.Equal("hello", so3.c);
  90. address.Query = "c=";
  91. Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject3), out value), "Expected 'true' reading valid data");
  92. so3 = (SimpleObject3)value;
  93. Assert.NotNull(so3);
  94. Assert.Equal("", so3.c);
  95. address.Query = "c=null";
  96. Assert.True(address.Uri.TryReadQueryAs(typeof(SimpleObject3), out value), "Expected 'true' reading valid data");
  97. so3 = (SimpleObject3)value;
  98. Assert.NotNull(so3);
  99. Assert.Equal("null", so3.c);
  100. }
  101. [Fact]
  102. [Trait("Description", "TryReadQueryAs<T>(Uri, out T) throws with null 'this'.")]
  103. public void TryReadQueryAsTThrowsWithNull()
  104. {
  105. object value;
  106. Assert.ThrowsArgumentNull(() => ((Uri)null).TryReadQueryAs<object>(out value), "address");
  107. }
  108. [Fact]
  109. [Trait("Description", "TryReadQueryAs<T>(Uri, out T) succeeds with valid URIs.")]
  110. public void TryReadQueryAsTSucceeds()
  111. {
  112. UriBuilder address = new UriBuilder("http://some.host");
  113. address.Query = "a=2";
  114. SimpleObject1 so1;
  115. Assert.True(address.Uri.TryReadQueryAs<SimpleObject1>(out so1), "Expected 'true' reading valid data");
  116. Assert.NotNull(so1);
  117. Assert.Equal(2, so1.a);
  118. address.Query = "b=true";
  119. SimpleObject2 so2;
  120. Assert.True(address.Uri.TryReadQueryAs<SimpleObject2>(out so2), "Expected 'true' reading valid data");
  121. Assert.NotNull(so2);
  122. Assert.True(so2.b, "Value should have been true");
  123. address.Query = "c=hello";
  124. SimpleObject3 so3;
  125. Assert.True(address.Uri.TryReadQueryAs<SimpleObject3>(out so3), "Expected 'true' reading valid data");
  126. Assert.NotNull(so3);
  127. Assert.Equal("hello", so3.c);
  128. address.Query = "c=";
  129. Assert.True(address.Uri.TryReadQueryAs<SimpleObject3>(out so3), "Expected 'true' reading valid data");
  130. Assert.NotNull(so3);
  131. Assert.Equal("", so3.c);
  132. address.Query = "c=null";
  133. Assert.True(address.Uri.TryReadQueryAs<SimpleObject3>(out so3), "Expected 'true' reading valid data");
  134. Assert.NotNull(so3);
  135. Assert.Equal("null", so3.c);
  136. }
  137. public class SimpleObject1
  138. {
  139. public int a { get; set; }
  140. }
  141. public class SimpleObject2
  142. {
  143. public bool b { get; set; }
  144. }
  145. public class SimpleObject3
  146. {
  147. public string c { get; set; }
  148. }
  149. }
  150. }