summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OpenId/IdentifierTests.cs
blob: 2b012dddd90a2012a465312aeaf5cd15e069dbf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//-----------------------------------------------------------------------
// <copyright file="IdentifierTests.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OpenId {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using DotNetOpenAuth.OpenId;
	using Microsoft.VisualStudio.TestTools.UnitTesting;

	[TestClass]
	public class IdentifierTests {
		private string uri = "http://www.yahoo.com/";
		private string uriNoScheme = "www.yahoo.com";
		private string uriHttps = "https://www.yahoo.com/";
		private string xri = "=arnott*andrew";

		[TestMethod]
		public void Parse() {
			Assert.IsInstanceOfType(Identifier.Parse(this.uri), typeof(UriIdentifier));
			Assert.IsInstanceOfType(Identifier.Parse(this.xri), typeof(XriIdentifier));
		}

		/// <summary>
		/// Tests conformance with 2.0 spec section 7.2#2
		/// </summary>
		[TestMethod]
		public void ParseEndUserSuppliedXriIdentifer() {
			List<char> symbols = new List<char>(XriIdentifier.GlobalContextSymbols);
			symbols.Add('(');
			List<string> prefixes = new List<string>();
			prefixes.AddRange(symbols.Select(s => s.ToString()));
			prefixes.AddRange(symbols.Select(s => "xri://" + s.ToString()));
			foreach (string prefix in prefixes) {
				var id = Identifier.Parse(prefix + "andrew");
				Assert.IsInstanceOfType(id, typeof(XriIdentifier));
			}
		}

		/// <summary>
		/// Verifies conformance with 2.0 spec section 7.2#3
		/// </summary>
		[TestMethod]
		public void ParseEndUserSuppliedUriIdentifier() {
			// verify a fully-qualified Uri
			var id = Identifier.Parse(this.uri);
			Assert.IsInstanceOfType(id, typeof(UriIdentifier));
			Assert.AreEqual(this.uri, ((UriIdentifier)id).Uri.AbsoluteUri);
			// verify an HTTPS Uri
			id = Identifier.Parse(this.uriHttps);
			Assert.IsInstanceOfType(id, typeof(UriIdentifier));
			Assert.AreEqual(this.uriHttps, ((UriIdentifier)id).Uri.AbsoluteUri);
			// verify that if the scheme is missing it is added automatically
			id = Identifier.Parse(this.uriNoScheme);
			Assert.IsInstanceOfType(id, typeof(UriIdentifier));
			Assert.AreEqual(this.uri, ((UriIdentifier)id).Uri.AbsoluteUri);
		}

		[TestMethod, ExpectedException(typeof(ArgumentNullException))]
		public void ParseNull() {
			Identifier.Parse(null);
		}

		[TestMethod, ExpectedException(typeof(ArgumentException))]
		public void ParseEmpty() {
			Identifier.Parse(string.Empty);
		}
	}
}