//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
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 messageData;
private readonly IMessageFactory messageFactory;
private readonly MessageReceivingEndpoint recipient;
private IDirectedProtocolMessage message;
///
/// Initializes a new instance of the class
/// that will generate a message when the property getter is called.
///
/// The channel.
/// The message factory.
/// The message data.
/// The recipient.
/// Cookies included in the incoming request.
internal CoordinatingHttpRequestInfo(
Channel channel,
IMessageFactory messageFactory,
IDictionary 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;
}
///
/// Initializes a new instance of the class
/// that will not generate any message.
///
/// The recipient.
/// Cookies included in the incoming request.
internal CoordinatingHttpRequestInfo(MessageReceivingEndpoint recipient, HttpCookieCollection cookies)
: base(GetHttpVerb(recipient), recipient != null ? recipient.Location : new Uri("http://host/path"), cookies: cookies) {
this.recipient = recipient;
}
///
/// Initializes a new instance of the class.
///
/// The message being passed in through a mock transport. May be null.
/// The HTTP method that the incoming request came in on, whether or not is null.
internal CoordinatingHttpRequestInfo(IDirectedProtocolMessage message, HttpDeliveryMethods httpMethod)
: base(GetHttpVerb(httpMethod), message.Recipient) {
this.message = message;
}
///
/// Gets the message deserialized from the remote channel.
///
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();
}
}
}