VS 2013 Coded Web Performance Test with RAW JASON data in body


I was working for a customer to run Web test and Load test on their Java based APIs hosted on Azure IaaS VMs. I was desperately looking for some coded Web test which will send custom header and will Post RAW data (JSON) in body. But couldn't locate any such example. Once created and tested by myself, thought it would be helpful for others:

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.VisualStudio.TestTools.WebTesting;

 

namespace WebAndLoadTestProject2

{

    [Priority(0)]

    public class testCoded : WebTest

    {

        public testCoded()

        {

            this.PreAuthenticate = true;

            this.Proxy = "default";

        }

        public override IEnumerator<WebTestRequest> GetRequestEnumerator()

        {

            WebTestRequest wtRequest = new WebTestRequest(https://www.contoso.com/APICollection/getFlightInfo/);

            wtRequest.Timeout = 300;

            wtRequest.Method = "POST";

            wtRequest.Headers.Add(new WebTestRequestHeader("Content-Type", "application/json"));

            wtRequest.Headers.Add(new WebTestRequestHeader("Authorization", "bearer pranab.paul@contoso.com"));

            StringHttpBody requestBody = new StringHttpBody();

            requestBody.ContentType = "application/json";

            requestBody.InsertByteOrderMark = false;

            requestBody.BodyString = @"{

    ""itenaryType"":""international"",

    ""routes"": [{

        ""endDate"":""30-05-2015"",

        ""from"":""BANGALORE,INDIA"",

        ""startDate"":""30-05-2015"",

        ""timeBetween"":""10:00-12:00"",

        ""to"":""BOSTON,USA""

    }],

    ""travelType"":""singlecity""

}

";

            wtRequest.Body = requestBody;

            yield return wtRequest;

            wtRequest = null;

        }

    }

}

 

Comments (0)

Skip to main content