|
| 1 | +using System; |
| 2 | +using Microsoft.WindowsAzure.MobileServices.TestFramework; |
| 3 | + |
| 4 | +namespace Microsoft.WindowsAzure.MobileServices.Test |
| 5 | +{ |
| 6 | + [Tag("unit")] |
| 7 | + [Tag("http")] |
| 8 | + public class HttpUtilityTests : TestBase |
| 9 | + { |
| 10 | + |
| 11 | + [TestMethod] |
| 12 | + public static void TryParseQueryUri_ReturnsTrue_WhenQueryIsRelativeOrAbsoluteUri() |
| 13 | + { |
| 14 | + var data = new[] |
| 15 | + { |
| 16 | + new |
| 17 | + { |
| 18 | + ServiceUri = "http://www.test.com", |
| 19 | + Query = "/about?$filter=a eq b&$orderby=c", |
| 20 | + Absolute = false, |
| 21 | + Result = "http://www.test.com/about?$filter=a eq b&$orderby=c" |
| 22 | + }, |
| 23 | + new |
| 24 | + { |
| 25 | + ServiceUri = "http://www.test.com/", |
| 26 | + Query = "http://www.test.com/about?$filter=a eq b&$orderby=c", |
| 27 | + Absolute = true, |
| 28 | + Result = "http://www.test.com/about?$filter=a eq b&$orderby=c" |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + foreach (var item in data) |
| 33 | + { |
| 34 | + Uri result; |
| 35 | + bool absolute; |
| 36 | + Assert.IsTrue(HttpUtility.TryParseQueryUri(new Uri(item.ServiceUri), item.Query, out result, out absolute)); |
| 37 | + Assert.AreEqual(absolute, item.Absolute); |
| 38 | + AssertEx.QueryEquals(result.AbsoluteUri, item.Result); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + [TestMethod] |
| 43 | + public static void TryParseQueryUri_ReturnsFalse_WhenQueryIsNotRelativeOrAbsoluteUri() |
| 44 | + { |
| 45 | + var data = new[] |
| 46 | + { |
| 47 | + new |
| 48 | + { |
| 49 | + ServiceUri = "http://www.test.com", |
| 50 | + Query = "about?$filter=a eq b&$orderby=c", |
| 51 | + Result = "http://www.test.com/about?$filter=a eq b&$orderby=c" |
| 52 | + }, |
| 53 | + new |
| 54 | + { |
| 55 | + ServiceUri = "http://www.test.com/", |
| 56 | + Query = "$filter=a eq b&$orderby=c", |
| 57 | + Result = "http://www.test.com/about?$filter=a eq b&$orderby=c" |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + foreach (var item in data) |
| 62 | + { |
| 63 | + Uri result; |
| 64 | + bool absolute; |
| 65 | + Assert.IsFalse(HttpUtility.TryParseQueryUri(new Uri(item.ServiceUri), item.Query, out result, out absolute)); |
| 66 | + Assert.IsFalse(absolute); |
| 67 | + Assert.IsNull(result); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + [TestMethod] |
| 72 | + public void GetUriWithoutQuery_ReturnsUriWithPath() |
| 73 | + { |
| 74 | + Tuple<string, string>[] input = new[] |
| 75 | + { |
| 76 | + Tuple.Create("http://contoso.com/asdf?$filter=3", "http://contoso.com/asdf"), |
| 77 | + Tuple.Create("http://contoso.com/asdf/def?$filter=3", "http://contoso.com/asdf/def"), |
| 78 | + Tuple.Create("https://contoso.com/asdf/def?$filter=3", "https://contoso.com/asdf/def") |
| 79 | + }; |
| 80 | + |
| 81 | + foreach (var item in input) |
| 82 | + { |
| 83 | + AssertEx.QueryEquals(HttpUtility.GetUriWithoutQuery(new Uri(item.Item1)), item.Item2); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments