Adding more JSON serialization test cases.

This commit is contained in:
Nima Kamoosi 2015-12-02 14:33:30 -08:00
Родитель de82ab4ff5
Коммит 19bb124628
3 изменённых файлов: 100 добавлений и 1 удалений

Просмотреть файл

@ -1,5 +1,8 @@
using AuthenticationUtility;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SoapUtility.UserSessionServiceReference;
using System;
using System.IO;
using System.Net;
@ -10,6 +13,7 @@ namespace ServiceTests
public class JsonXppAuthTests
{
public string GetUserSessionOperationPath = ClientConfiguration.Default.UriString + "api/services/UserSessionService/AifUserSessionService/GetUserSessionInfo";
public string ApplyTimeZoneOperationPath = ClientConfiguration.Default.UriString + "api/services/UserSessionService/AifUserSessionService/ApplyTimeZone";
[TestMethod]
@ -34,4 +38,88 @@ namespace ServiceTests
}
}
}
}}
[TestMethod]
public void JsonSoapContractTest()
{
var request = HttpWebRequest.Create(ApplyTimeZoneOperationPath);
request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader();
request.Method = "POST";
DateTime inputDateTime = DateTime.Now;
var requestContract = new ApplyTimeZone();
requestContract.dateTime = inputDateTime;
requestContract.timeZoneOffset = 3;
var requestContractString = JsonConvert.SerializeObject(requestContract);
using (var stream = request.GetRequestStream())
{
using (var writer = new StreamWriter(stream))
{
writer.Write(requestContractString);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
string responseString = streamReader.ReadToEnd();
DateTime appliedTimeZone = JsonConvert.DeserializeObject<DateTime>(responseString);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.IsFalse(string.IsNullOrEmpty(responseString));
Console.WriteLine(responseString);
Assert.IsNotNull(appliedTimeZone);
Assert.AreNotEqual(appliedTimeZone.Hour, inputDateTime.Hour);
}
}
}
}
[TestMethod]
public void JsonWeaklyTypedContractTest()
{
var request = HttpWebRequest.Create(ApplyTimeZoneOperationPath);
request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader();
request.Method = "POST";
DateTime inputDateTime = DateTime.Now;
var requestContract = new
{
dateTime = inputDateTime,
timeZoneOffset = 3
};
var requestContractString = JsonConvert.SerializeObject(requestContract);
using (var stream = request.GetRequestStream())
{
using (var writer = new StreamWriter(stream))
{
writer.Write(requestContractString);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
string responseString = streamReader.ReadToEnd();
JToken jsonObject = JToken.Parse(responseString);
DateTime appliedTimeZone = jsonObject.Value<DateTime>();
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.IsFalse(string.IsNullOrEmpty(responseString));
Console.WriteLine(responseString);
Assert.IsNotNull(appliedTimeZone);
Assert.AreNotEqual(appliedTimeZone.Hour, inputDateTime.Hour);
}
}
}
}
}
}

Просмотреть файл

@ -36,6 +36,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.Runtime.Serialization" />
@ -70,6 +74,9 @@
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

Просмотреть файл

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
</packages>