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

namespace DotNetOpenAuth.Test.Mocks {
	using System;
	using System.Collections.Generic;
	using System.Net;
	using System.Web;

	using DotNetOpenAuth.Messaging;
	using Validation;

	internal class CoordinatingHttpRequestInfo : HttpRequestInfo {
		private readonly Channel channel;

		private readonly IDictionary<string, string> messageData;

		private readonly IMessageFactory messageFactory;

		private readonly MessageReceivingEndpoint recipient;

		private IDirectedProtocolMessage message;

		/// <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>
		/// <param name="cookies">Cookies included in the incoming request.</param>
		internal CoordinatingHttpRequestInfo(
			Channel channel,
			IMessageFactory messageFactory,
			IDictionary<string, string> messageData,
			MessageReceivingEndpoint recipient,
			HttpCookieCollection cookies)
			: this(recipient, cookies) {
			Requires.NotNull(channel, "channel");
			Requires.NotNull(messageFactory, "messageFactory");
			Requires.NotNull(messageData, "messageData");
			this.channel = channel;
			this.messageData = messageData;
			this.messageFactory = messageFactory;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CoordinatingHttpRequestInfo"/> class
		/// that will not generate any message.
		/// </summary>
		/// <param name="recipient">The recipient.</param>
		/// <param name="cookies">Cookies included in the incoming request.</param>
		internal CoordinatingHttpRequestInfo(MessageReceivingEndpoint recipient, HttpCookieCollection cookies)
			: base(GetHttpVerb(recipient), recipient != null ? recipient.Location : new Uri("http://host/path"), cookies: cookies) {
			this.recipient = recipient;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CoordinatingHttpRequestInfo"/> class.
		/// </summary>
		/// <param name="message">The message being passed in through a mock transport.  May be null.</param>
		/// <param name="httpMethod">The HTTP method that the incoming request came in on, whether or not <paramref name="message"/> is null.</param>
		internal CoordinatingHttpRequestInfo(IDirectedProtocolMessage message, HttpDeliveryMethods httpMethod)
			: base(GetHttpVerb(httpMethod), message.Recipient) {
			this.message = message;
		}

		/// <summary>
		/// Gets the message deserialized from the remote channel.
		/// </summary>
		internal IDirectedProtocolMessage Message {
			get {
				if (this.message == null && this.messageData != null) {
					var message = this.messageFactory.GetNewRequestMessage(this.recipient, this.messageData);
					if (message != null) {
						this.channel.MessageDescriptions.GetAccessor(message).Deserialize(this.messageData);
						this.message = message;
					}
				}

				return this.message;
			}
		}

		private static string GetHttpVerb(MessageReceivingEndpoint recipient) {
			if (recipient == null) {
				return "GET";
			}

			return GetHttpVerb(recipient.AllowedMethods);
		}

		private static string GetHttpVerb(HttpDeliveryMethods httpMethod) {
			if ((httpMethod & HttpDeliveryMethods.GetRequest) != 0) {
				return "GET";
			}

			if ((httpMethod & HttpDeliveryMethods.PostRequest) != 0) {
				return "POST";
			}

			throw new ArgumentOutOfRangeException();
		}
	}
}