summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Messaging/StandardMessageFactoryTests.cs
blob: 86bf67e57157de3199d8c91b96822161bd7f973d (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//-----------------------------------------------------------------------
// <copyright file="StandardMessageFactoryTests.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.Messaging {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.Messaging.Reflection;
	using DotNetOpenAuth.Test.Mocks;
	using NUnit.Framework;

	[TestFixture]
	public class StandardMessageFactoryTests : MessagingTestBase {
		private static readonly Version V1 = new Version(1, 0);
		private static readonly MessageReceivingEndpoint receiver = new MessageReceivingEndpoint("http://receiver", HttpDeliveryMethods.PostRequest);

		private StandardMessageFactory factory;

		public override void SetUp() {
			base.SetUp();

			this.factory = new StandardMessageFactory();
		}

		/// <summary>
		/// Verifies that AddMessageTypes throws the appropriate exception on null input.
		/// </summary>
		[TestCase, ExpectedException(typeof(ArgumentNullException))]
		public void AddMessageTypesNull() {
			this.factory.AddMessageTypes(null);
		}

		/// <summary>
		/// Verifies that AddMessageTypes throws the appropriate exception on null input.
		/// </summary>
		[TestCase, ExpectedException(typeof(ArgumentException))]
		public void AddMessageTypesNullMessageDescription() {
			this.factory.AddMessageTypes(new MessageDescription[] { null });
		}

		/// <summary>
		/// Verifies very simple recognition of a single message type
		/// </summary>
		[TestCase]
		public void SingleRequestMessageType() {
			this.factory.AddMessageTypes(new MessageDescription[] { MessageDescriptions.Get(typeof(RequestMessageMock), V1) });
			var fields = new Dictionary<string, string> {
				{ "random", "bits" },
			};
			Assert.IsNull(this.factory.GetNewRequestMessage(receiver, fields));
			fields["Age"] = "18";
			Assert.IsInstanceOf(typeof(RequestMessageMock), this.factory.GetNewRequestMessage(receiver, fields));
		}

		/// <summary>
		/// Verifies very simple recognition of a single message type
		/// </summary>
		[TestCase]
		public void SingleResponseMessageType() {
			this.factory.AddMessageTypes(new MessageDescription[] { MessageDescriptions.Get(typeof(DirectResponseMessageMock), V1) });
			var fields = new Dictionary<string, string> {
				{ "random", "bits" },
			};
			IDirectedProtocolMessage request = new RequestMessageMock(receiver.Location, V1);
			Assert.IsNull(this.factory.GetNewResponseMessage(request, fields));
			fields["Age"] = "18";
			IDirectResponseProtocolMessage response = this.factory.GetNewResponseMessage(request, fields);
			Assert.IsInstanceOf<DirectResponseMessageMock>(response);
			Assert.AreSame(request, response.OriginatingRequest);

			// Verify that we can instantiate a response with a derived-type of an expected request message.
			request = new TestSignedDirectedMessage();
			response = this.factory.GetNewResponseMessage(request, fields);
			Assert.IsInstanceOf<DirectResponseMessageMock>(response);
			Assert.AreSame(request, response.OriginatingRequest);
		}

		private class DirectResponseMessageMock : IDirectResponseProtocolMessage {
			internal DirectResponseMessageMock(RequestMessageMock request) {
				this.OriginatingRequest = request;
			}

			internal DirectResponseMessageMock(TestDirectedMessage request) {
				this.OriginatingRequest = request;
			}

			[MessagePart(IsRequired = true)]
			public int Age { get; set; }

			#region IDirectResponseProtocolMessage Members

			public IDirectedProtocolMessage OriginatingRequest { get; private set; }

			#endregion

			#region IProtocolMessage Members

			public MessageProtections RequiredProtection {
				get { throw new NotImplementedException(); }
			}

			public MessageTransport Transport {
				get { throw new NotImplementedException(); }
			}

			#endregion

			#region IMessage Members

			public Version Version {
				get { throw new NotImplementedException(); }
			}

			public System.Collections.Generic.IDictionary<string, string> ExtraData {
				get { throw new NotImplementedException(); }
			}

			public void EnsureValidMessage() {
				throw new NotImplementedException();
			}

			#endregion
		}

		private class RequestMessageMock : IDirectedProtocolMessage {
			internal RequestMessageMock(Uri recipient, Version version) {
			}

			[MessagePart(IsRequired = true)]
			public int Age { get; set; }

			#region IDirectedProtocolMessage Members

			public HttpDeliveryMethods HttpMethods {
				get { throw new NotImplementedException(); }
			}

			public Uri Recipient {
				get { throw new NotImplementedException(); }
			}

			#endregion

			#region IProtocolMessage Members

			public MessageProtections RequiredProtection {
				get { throw new NotImplementedException(); }
			}

			public MessageTransport Transport {
				get { throw new NotImplementedException(); }
			}

			#endregion

			#region IMessage Members

			public Version Version {
				get { throw new NotImplementedException(); }
			}

			public System.Collections.Generic.IDictionary<string, string> ExtraData {
				get { throw new NotImplementedException(); }
			}

			public void EnsureValidMessage() {
				throw new NotImplementedException();
			}

			#endregion
		}
	}
}