In this post I will use a UnitTest project written in C# .NET 2.0 as client code.

And a ASP .NET Web Api project as Server (service).

 

Client code

Create a new UnitTest project and change target framework to .net 3.5.

Add nuget package JSON.NET.

 

 

 

 

namespace WordMerge.EndToEndTests
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Net;
    using Newtonsoft.Json;

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void Execute_a_get_request()
        {
            string url = "http://localhost:63544/api/document";
            string result = string.Empty;

            // Uses the System.Net.WebClient and not HttpClient, because .NET 2.0 must be supported.
            using (var client = new WebClient())
            {
                // Set the header so it knows we are requesting JSON.
                client.Headers[HttpRequestHeader.ContentType] = "application/json";

                result = client.DownloadString(url);
            }
            Assert.AreEqual(@"[""value1"",""value2""]", result);
        }


        [TestMethod]
        public void Execute_a_post_request()
        {
            string url = "http://localhost:63544/api/document";
            object result = string.Empty;

            // Uses the System.Net.WebClient and not HttpClient, because .NET 2.0 must be supported.
            using (var client = new WebClient())
            {
                // Set the header so it knows we are sending JSON.
                client.Headers[HttpRequestHeader.ContentType] = "application/json";

                string[] data = new string[] { "This", " is", " test", " input", " data." };

                // Serialise the data we are sending in to JSON
                string serialisedData = JsonConvert.SerializeObject(data);

                // Make the request
                var response = client.UploadString(url, serialisedData);

                // Deserialise the response into a GUID
                result = JsonConvert.DeserializeObject(response);
            }

            Assert.AreEqual(@"Succesfully uploaded: This, is, test, input, data.", result.ToString());
        }
    }
}

NuGet packages config

 

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Newtonsoft.Json" version="5.0.6" targetFramework="net35" />
</packages>

Server (service) code

Create an empty ASP .NET Web API project in Microsoft Visual Studio 2013.

namespace Service.api
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    public class DocumentController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public string Post([FromBody]string[] values)
        {
            string seperator = ",";
            string data = string.Join(seperator,values.ToList<string>());
            string result = string.Format("Succesfully uploaded: {0}", data);
            return result;
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }
}

Visual Studio Structure

image

NuGet packages config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.WebApi" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
</packages>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.