summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Mocks/TestMessageFactory.cs
blob: 5a47ab49486abf101b9f5e5abead6b89c876d869 (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
//-----------------------------------------------------------------------
// <copyright file="TestMessageFactory.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

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

	internal class TestMessageFactory : IMessageFactory {
		private bool signedMessages;
		private bool expiringMessages;
		private bool replayMessages;

		internal TestMessageFactory()
			: this(false, false, false) {
		}

		internal TestMessageFactory(bool signed, bool expiring, bool replay) {
			if ((!signed && expiring) || (!expiring && replay)) {
				throw new ArgumentException("Invalid combination of protection.");
			}
			this.signedMessages = signed;
			this.expiringMessages = expiring;
			this.replayMessages = replay;
		}

		#region IMessageFactory Members

		public IDirectedProtocolMessage GetNewRequestMessage(MessageReceivingEndpoint recipient, IDictionary<string, string> fields) {
			if (fields.ContainsKey("age")) {
				if (this.signedMessages) {
					if (this.expiringMessages) {
						if (this.replayMessages) {
							return new TestReplayProtectedMessage();
						}
						return new TestExpiringMessage();
					}
					return new TestSignedDirectedMessage();
				}
				var result = new TestDirectedMessage();
				if (fields.ContainsKey("GetOnly")) {
					result.HttpMethods = HttpDeliveryMethods.GetRequest;
				}
				return result;
			}
			return null;
		}

		public IDirectResponseProtocolMessage GetNewResponseMessage(IDirectedProtocolMessage request, IDictionary<string, string> fields) {
			TestMessage message = (TestMessage)this.GetNewRequestMessage(null, fields);
			message.OriginatingRequest = request;
			return message;
		}

		#endregion
	}
}