WebServicePlugin.cs 6.86 KB
//*************************************************************************************************
// WebServicePlugin.cs
// Owner: Mahipal Kante
//
// This Web Test plugin performs Data Binding on binary format for Web Service response
// When the Web Service response is 'application/soap+msbin1'
// (binary - used by Silverlight application with WCF RIA)
//
// Copyright(c) Microsoft Corporation, 2010
//*************************************************************************************************
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.Diagnostics.CodeAnalysis;

namespace WebTest.WebService.Plugin.Runtime
{
    public class WebServicePlugin : WebTestPlugin
    {
        /// Summary:
        ///     When overridden in a derived class, represents the method that will handle
        ///     the event associated with the completion of a Web page.
        ///
        /// Parameters:
        ///   sender:
        ///     The source of the event.
        ///
        ///   e:
        ///     A PostPageEventArgs that contains the event data.
        ///     
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public override void PostPage(object sender, PostPageEventArgs e)
        {
        }

        //
        // Summary:
        //     When overridden in a derived class, represents the method that will handle
        //     the event associated with the completion of an HTTP request.
        //
        // Parameters:
        //   sender:
        //     The source of the event.
        //
        //   e:
        //     A PostRequestEventArgs that contains the event data.
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public override void PostRequest(object sender, PostRequestEventArgs e)
        {
        }

        //
        // Summary:
        //     When overridden in a derived class, represents the method that will handle
        //     the event associated with the completion of a transaction that is defined
        //     in the Web test.
        //
        // Parameters:
        //   sender:
        //     The source of the event.
        //
        //   e:
        //     A PostTransactionEventArgs that contains the event data.
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public override void PostTransaction(object sender, PostTransactionEventArgs e)
        {
        }

        //
        // Summary:
        //     When overridden in a derived class, represents the method that will handle
        //     the event associated with the end of a Web test.
        //
        // Parameters:
        //   sender:
        //     The source of the event.
        //
        //   e:
        //     A Microsoft.VisualStudio.TestTools.WebTesting.PostWebTestEventArgs that contains
        //     the event data.
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public override void PostWebTest(object sender, PostWebTestEventArgs e)
        {
        }

        //
        // Summary:
        //     When overridden in a derived class, represents the method that will handle
        //     the event associated with the start of a Web page.
        //
        // Parameters:
        //   sender:
        //     The source of the event.
        //
        //   e:
        //     A PrePageEventArgs that contains the event data.
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public override void PrePage(object sender, PrePageEventArgs e)
        {
        }

        //
        // Summary:
        //     When overridden in a derived class, represents the method that will handle
        //     the event associated with the start of an HTTP request.
        //
        // Parameters:
        //   sender:
        //     The source of the event.
        //
        //   e:
        //     A PreRequestEventArgs that contains the event data.
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public override void PreRequest(object sender, PreRequestEventArgs e)
        {
        }

        /// <summary>
        /// This method performs Data Binding on MSBin1 binray format for Web Service response
        /// When the Web Service response is 'application/soap+msbin1'
        // (binary - used by Silverlight application with WCF RIA)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public override void PreRequestDataBinding(object sender, PreRequestDataBindingEventArgs e)
        {
            BinaryHttpBody binaryHttpBody = e.Request.Body as BinaryHttpBody;
            if (binaryHttpBody == null || string.IsNullOrEmpty(binaryHttpBody.ContentType))
            {
                return;
            }

            if (!binaryHttpBody.ContentType.Contains("msbin1"))
            {
                return;
            }

            ContentHandler contentHandler = ContentHandlerFactory.GetHandler(binaryHttpBody.ContentType);
            if (contentHandler != null)
            {
                contentHandler.MessageBytes = binaryHttpBody.Data;

                string text = contentHandler.MessageString;

                // do Data Binding on the string version of binary data
                string newText = DataBindingHelper.UpdateBindingSites(e.WebTest.Context, text);

                contentHandler.MessageString = newText;

                // set the new binary data into http body
                binaryHttpBody.Data = contentHandler.MessageBytes;
            }
        }

        //
        // Summary:
        //     When overridden in a derived class, represents the method that will handle
        //     the event associated with the start of a transaction that is defined in the
        //     Web test.
        //
        // Parameters:
        //   sender:
        //     The source of the event.
        //
        //   e:
        //     A PreTransactionEventArgs that contains the event data.
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public override void PreTransaction(object sender, PreTransactionEventArgs e)
        {
        }

        //
        // Summary:
        //     When overridden in a derived class, represents the method that will handle
        //     the event associated with the start of a Web test.
        //
        // Parameters:
        //   sender:
        //     The source of the event.
        //
        //   e:
        //     A Microsoft.VisualStudio.TestTools.WebTesting.PostWebTestEventArgs that contains
        //     the event data.
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers")]
        public override void PreWebTest(object sender, PreWebTestEventArgs e)
        {
        }
    }
}