summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs
blob: 46e3373972a9208a60b10e7c22b0f897031b5a8f (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="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;
		private Channel channel;

		/// <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="channel">The channel.</param>
		/// <param name="messageFactory">The message factory.</param>
		/// <param name="messageData">The message data.</param>
		/// <param name="recipient">The recipient.</param>
		internal CoordinatingHttpRequestInfo(Channel channel, IMessageFactory messageFactory, IDictionary<string, string> messageData, MessageReceivingEndpoint recipient)
			: this(recipient) {
			Contract.Requires(channel != null);
			Contract.Requires(messageFactory != null);
			Contract.Requires(messageData != null);
			this.channel = channel;
			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) {
				this.UrlBeforeRewriting = recipient.Location;
			}

			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) {
						this.channel.MessageDescriptions.GetAccessor(message).Deserialize(this.messageData);
					}
					base.Message = message;
				}

				return base.Message;
			}

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