Msbin1MessageEditor.cs 2.62 KB
//*************************************************************************************************
// Msbin1MessageEditor.cs
// Owner: Mahipal Kante
//
// Web Test Plugin for editing 'application/soap+msbin1' content type Web Service Requests
// (used by Silverlight application with WCF RIA) 
//
// Copyright(c) Microsoft Corporation, 2010
//*************************************************************************************************
using Microsoft.VisualStudio.TestTools.WebTesting;
using WebTest.WebService.Plugin.ResultTab;
using WebTest.WebService.Plugin.Runtime;

namespace WebTest.WebService.Plugin.MessageEditors
{
    /// <summary>
    /// Editor for MSBin1 content type (WCF messages)
    /// </summary>
    public class Msbin1MessageEditor : IBinaryHttpBodyEditorPlugin
    {
        /// <summary>
        /// 
        /// </summary>
        public Msbin1MessageEditor()
        {
        }

        /// <summary>
        /// Determine if this plugin supports a content type.
        /// </summary>
        /// <param name="contentType">The content type to test.</param>
        /// <returns>Returns true if the plugin supports the specified content type.</returns>
        public bool SupportsContentType(string contentType)
        {
            return contentType.ToLower().Contains("msbin1");
        }

        /// <summary>
        /// Create a UserControl to edit the specified bytes.  This control will be hosted in the
        /// plugin dialog which provides OK and Cancel buttons.
        /// </summary>
        /// <param name="contentType">The content type of the BinaryHttpBody.</param>
        /// <param name="initialValue">
        /// The bytes to edit.  The bytes are the payload of a BinaryHttpBody.
        /// </param>
        /// <returns>
        /// A UserControl capable of displaying and editing the byte array value of 
        /// the specified content type.
        /// </returns>
        public object CreateEditor(string contentType, byte[] initialValue)
        {
            contentHandler = new Msbin1ContentHandler(contentType.ToLower().Contains("soap"));
            contentHandler.MessageBytes = initialValue;
            messageEditor = new MessageEditor(contentHandler);
            return messageEditor;
        }

        /// <summary>
        /// Gets the edited bytes after the OK button is clicked on the plugin dialog.
        /// </summary>
        public byte[] GetNewValue()
        {
            messageEditor.UpdateContent();
            return contentHandler.MessageBytes;
        }

        private MessageEditor messageEditor = null;
        private Msbin1ContentHandler contentHandler = null;
    }
}