Getting started with Cloud-based Load Test REST APIs

Charles Sterling

REST API’s are pretty cool, enabling you to do things like write your own custom reports, write your own interfaces and of course integrating load tests into your build. 

Looking at the sample referenced with our REST API documentation; at 400 lines to demonstrate 4 API’s I must admit I found it pretty daunting so I thought I would see if I could simplify that somewhat.

Just like the documentation indicates: the first place you want to start is to enable alternate credentials.  This is done by opening your user profile from Visual Studio Online. 

image

And enabling alternative credentials.

image

 

At this point we are ready to start writing a sample!

Since the goal of this demo is to keep it simple I decided to use ASP.NET and let it handle all the Async goo. 

 

imageimage

To do this all you need to do is set the property Async=”True” on the page(see below).  

While on the page I also tossed on a Textbox to display the result of the REST calls.

image

Here is the markup for that page:

> <%@ Page Language=”C#” AutoEventWireup=”true” async=”true” CodeBehind=”simple.aspx.cs” Inherits=”ssimple.simple” %> >

>   >

> DOCTYPE html> >

>   >

> <html xmlns=”http://www.w3.org/1999/xhtml”> >

> <head runat=”server”> >

>     <title>title> >

> head> >

> <body> >

>     <form id=”form1″ runat=”server”> >

>     <div> >

>         <asp:TextBox ID=”TextBox1″ runat=”server” TextMode=”MultiLine”>asp:TextBox> >

>     div> >

>     form> >

> body> >

> html> >

 

To make the code easier to read and access the functionality needed; bring in the following four namespaces and corresponding references:

> using System.Globalization; >

> using System.Web.Configuration; >

> using System.Net.Http; >

> using System.Net.Http.Headers; >

At this point I am ready to call my REST API: **/TESTDROPS.  **

**An important note: **You need to use the **VSCLT **URI for Cloud-based load test REST API’s So the entire URL would look like:

https://.vsclt.visualstudio.com/_apis/clt/testdrops”

While the REST API only takes one line of code (HttpClient.GetStringAsync…); a couple of lines of code are required to setup up the connection.

Since we are letting ASP.NET handle the Async handling you need to set this property in the event handler.

 

> protected async void Page_Load(object sender, EventArgs e) >

>         { >

>   >

>             HttpClient client = new HttpClient(); >

>             //setup the client connection >

>             var vsoAccountUsername = WebConfigurationManager.AppSettings[“VSOnlineAccountUsername”]; >

>             // var vsoAccountUsername = “Chass@outlook.com”; >

>             var vsoAccountPassword = WebConfigurationManager.AppSettings[“VSOnlineAccountPassword”]; >

>             client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(“Basic”, >

>             Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(string.Format(CultureInfo.InvariantCulture, “{0}:{1}”, vsoAccountUsername, vsoAccountPassword)))); >

>             TextBox1.Text = await client.GetStringAsync(“https://aidemo.vsclt.visualstudio.com/_apis/clt/testdrops”); >

>         } >

 

If you are getting an HTTP 401 error when running this sample more than likely your account doesn’t have a Visual Studio Ultimate Subscription associated with it or you haven’t enabled alternate credentials.

For those of you a little more old school here is a console application version: 

 

public static async void GetTestRuns()
        {
            try
            {
                var elsAccountUrl = new Uri(ConfigurationManager.AppSettings[“ElsOnlineAccountUrl”]);
                var vsoAccountUsername = ConfigurationManager.AppSettings[“VSOnlineAccountUsername”];
                var vsoAccountPassword = ConfigurationManager.AppSettings[“VSOnlineAccountPassword”];

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue(“application/json”));

                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(“Basic”,
                        Convert.ToBase64String(
                            System.Text.Encoding.ASCII.GetBytes(
                                string.Format(“{0}:{1}”, vsoAccountUsername, vsoAccountPassword))));

                    using (HttpResponseMessage response = client.GetAsync(elsAccountUrl+”/_apis/clt/testruns”).Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

 

 

 

If you haven’t played with App.Config files, here is an example:


   
       
   

   
       
        https://
{account}.vsclt.visualstudio.com”][7] />

       
       
       
   

 

To make it has easy as possible, I have also attached a zip file of the web project:

2451.ssimple.zip

http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-45-92-chass/2451.ssimple.zip

 

In case you are wondering what the return looks like I would recommend just running the URL: https://{youraccount}.vsclt.visualstudio.com/_apis/clt/testdrops

Here is a screenshot of the web application as well

image

 

 

 

SEO Tagging: “worlds simplest REST API Sample”

0 comments

Discussion is closed.