//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.Test.Mocks { using System; using System.IO; using System.Net; using System.Text; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OAuth.ChannelElements; internal class TestWebRequestHandler : IWebRequestHandler { private StringBuilder postEntity; /// /// Gets or sets the callback used to provide the mock response for the mock request. /// internal Func Callback { get; set; } /// /// Gets the stream that was written out as if on an HTTP request. /// internal Stream RequestEntityStream { get { if (this.postEntity == null) { return null; } return new MemoryStream(Encoding.UTF8.GetBytes(this.postEntity.ToString())); } } /// /// Gets the stream that was written out as if on an HTTP request as an ordinary string. /// internal string RequestEntityAsString { get { return this.postEntity != null ? this.postEntity.ToString() : null; } } #region IWebRequestHandler Members /// /// Prepares an that contains an POST entity for sending the entity. /// /// The that should contain the entity. /// /// The writer the caller should write out the entity data to. /// public TextWriter GetRequestStream(HttpWebRequest request) { this.postEntity = new StringBuilder(); return new StringWriter(this.postEntity); } /// /// Processes an and converts the /// to a instance. /// /// The to handle. /// /// An instance of describing the response. /// public Response GetResponse(HttpWebRequest request) { if (this.Callback == null) { throw new InvalidOperationException("Set the Callback property first."); } return this.Callback(request); } #endregion } }