summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs
blob: 54670458c5d8c2d04ea138d2997ef85587646c92 (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
//-----------------------------------------------------------------------
// <copyright file="CoordinatingHttpRequestInfo.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.Mocks {
	using System.Collections.Generic;
	using System.Diagnostics.Contracts;
	using DotNetOpenAuth.Messaging;

	internal class CoordinatingHttpRequestInfo : HttpRequestInfo {
		private IDictionary<string, string> messageData;
		private IMessageFactory messageFactory;
		private MessageReceivingEndpoint recipient;

		/// <summary>
		/// Initializes a new instance of the <see cref="CoordinatingHttpRequestInfo"/> class
		/// that will generate a message when the <see cref="Message"/> property getter is called.
		/// </summary>
		/// <param name="messageFactory">The message factory.</param>
		/// <param name="messageData">The message data.</param>
		/// <param name="recipient">The recipient.</param>
		internal CoordinatingHttpRequestInfo(IMessageFactory messageFactory, IDictionary<string, string> messageData, MessageReceivingEndpoint recipient)
			: this(recipient) {
			Contract.Requires(messageFactory != null);
			Contract.Requires(messageData != null);
			this.messageFactory = messageFactory;
			this.messageData = messageData;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CoordinatingHttpRequestInfo"/> class
		/// that will not generate any message.
		/// </summary>
		/// <param name="recipient">The recipient.</param>
		internal CoordinatingHttpRequestInfo(MessageReceivingEndpoint recipient) {
			this.recipient = recipient;

			if (recipient == null || (recipient.AllowedMethods & HttpDeliveryMethods.GetRequest) != 0) {
				this.HttpMethod = "GET";
			} else if ((recipient.AllowedMethods & HttpDeliveryMethods.PostRequest) != 0) {
				this.HttpMethod = "POST";
			}
		}

		internal override IDirectedProtocolMessage Message {
			get {
				if (base.Message == null && this.messageData != null) {
					IDirectedProtocolMessage message = this.messageFactory.GetNewRequestMessage(this.recipient, this.messageData);
					if (message != null) {
						MessageSerializer.Get(message.GetType()).Deserialize(this.messageData, message);
					}
					base.Message = message;
				}

				return base.Message;
			}

			set {
				base.Message = value;
			}
		}
	}
}