summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/OpenId/Extensions/AttributeExchange/FetchRequestTests.cs
blob: 23887989ddf04b7a7e392ead196ac926a20fd6a6 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//-----------------------------------------------------------------------
// <copyright file="FetchRequestTests.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.OpenId.Extensions {
	using System;
	using System.IO;
	using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
	using DotNetOpenAuth.Test.OpenId;
	using NUnit.Framework;

	[TestFixture]
	public class FetchRequestTests : OpenIdTestBase {
		[TestCase, ExpectedException(typeof(ArgumentNullException))]
		public void AddAttributeRequestNull() {
			new FetchRequest().Attributes.Add(null);
		}

		[TestCase]
		public void AddAttributeRequest() {
			var req = new FetchRequest();
			req.Attributes.Add(new AttributeRequest() { TypeUri = "http://someUri" });
		}

		[TestCase]
		public void AddAttributeRequestStrangeUri() {
			var req = new FetchRequest();
			req.Attributes.Add(new AttributeRequest() { TypeUri = "=someUri*who*knows*but*this*is*legal" });
		}

		[TestCase, ExpectedException(typeof(ArgumentException))]
		public void AddAttributeRequestAgain() {
			var req = new FetchRequest();
			req.Attributes.Add(new AttributeRequest() { TypeUri = "http://UriTwice" });
			req.Attributes.Add(new AttributeRequest() { TypeUri = "http://UriTwice" });
		}

		[TestCase]
		public void RespondSimpleValue() {
			var req = new AttributeRequest();
			req.TypeUri = "http://someType";
			var resp = req.Respond("value");
			Assert.AreEqual(req.TypeUri, resp.TypeUri);
			Assert.AreEqual(1, resp.Values.Count);
			Assert.AreEqual("value", resp.Values[0]);
		}

		[TestCase]
		public void RespondTwoValues() {
			var req = new AttributeRequest();
			req.TypeUri = "http://someType";
			req.Count = 2;
			var resp = req.Respond("value1", "value2");
			Assert.AreEqual(req.TypeUri, resp.TypeUri);
			Assert.AreEqual(2, resp.Values.Count);
			Assert.AreEqual("value1", resp.Values[0]);
			Assert.AreEqual("value2", resp.Values[1]);
		}

		[TestCase, ExpectedException(typeof(ArgumentException))]
		public void RespondTooManyValues() {
			var req = new AttributeRequest();
			req.TypeUri = "http://someType";
			req.Count = 1;
			req.Respond("value1", "value2");
		}

		[TestCase, ExpectedException(typeof(ArgumentNullException))]
		public void RespondNull() {
			var req = new AttributeRequest();
			req.TypeUri = "http://someType";
			req.Count = 1;
			req.Respond(null);
		}

		[TestCase]
		public void EqualityTests() {
			var req1 = new FetchRequest();
			var req2 = new FetchRequest();
			Assert.AreEqual(req1, req2);

			req1.UpdateUrl = new Uri("http://hi");
			Assert.AreNotEqual(req1, req2);
			req2.UpdateUrl = new Uri("http://hi");
			Assert.AreEqual(req1, req2);

			// Add attributes in different orders deliberately.
			req1.Attributes.Add(new AttributeRequest("http://att1"));
			Assert.AreNotEqual(req1, req2);
			req2.Attributes.Add(new AttributeRequest("http://att2"));
			Assert.AreNotEqual(req1, req2);
			req1.Attributes.Add(new AttributeRequest("http://att2"));
			Assert.AreNotEqual(req1, req2);
			req2.Attributes.Add(new AttributeRequest("http://att1"));
			Assert.AreEqual(req1, req2);
		}

		/// <summary>
		/// Verifies that the class is serializable.
		/// </summary>
		[TestCase]
		public void Serializable() {
			var fetch = new FetchRequest();
			fetch.Attributes.AddRequired("http://someAttribute");
			var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
			var ms = new MemoryStream();
			formatter.Serialize(ms, fetch);
			ms.Position = 0;
			var fetch2 = formatter.Deserialize(ms);
			Assert.AreEqual(fetch, fetch2);
		}
	}
}