Skip to content

Commit

Permalink
fix issue 505 and add patch test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
mirsking committed Sep 1, 2016
1 parent ee38228 commit f7fafd6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@ protected void TestPostStatusCodeIs(string uriStringAfterServiceRoot, int status
Assert.Equal(statusCode, response.StatusCode);
}
}

protected async void TestPatchStatusCodeIs(string uriStringAfterServiceRoot, string patchContent, HttpStatusCode statusCode)
{
var requestUri = string.Format("{0}/{1}", this.ServiceBaseUri, uriStringAfterServiceRoot);
var request = new HttpRequestMessage(new HttpMethod("PATCH"), requestUri);

request.Content = new StringContent(patchContent);
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

HttpClient client = new HttpClient();
HttpResponseMessage response = await client.SendAsync(request);

Assert.Equal(statusCode, response.StatusCode);
}

#endregion

protected void ResetDataSource()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<HintPath>..\..\..\packages\FluentAssertions.4.13.0\lib\net45\FluentAssertions.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.OData.Client, Version=6.15.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Microsoft.OData.Client.6.15.0\lib\net40\Microsoft.OData.Client.dll</HintPath>
<Private>True</Private>
Expand All @@ -96,6 +97,10 @@
<HintPath>..\..\..\packages\Microsoft.Spatial.6.15.0\lib\portable-net45+win+wpa81\Microsoft.Spatial.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Net.Http" />
<Reference Include="System.ServiceModel.Web" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Net;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Xunit;

namespace Microsoft.OData.Service.Sample.Tests
Expand Down Expand Up @@ -158,5 +162,46 @@ public void TestRawValuedEnumPropertyAccess()
{
TestGetPayloadIs("People('russellwhyte')/FavoriteFeature/$value", "Feature1");
}

[Fact]
public void TestPatchSuccessfully()
{
// Get origin content and sessionId.
var uriStringAfterServiceRoot = "Airports('KLAX')";
var originContent = default(string);
Action<string> getContent = p => originContent = p;
TestGetPayload(uriStringAfterServiceRoot, getContent);
var sessionId = GetSessionIdFromResponse(originContent);
Assert.NotNull(sessionId);

// Patch it.
uriStringAfterServiceRoot = string.Format(@"(S({0}))/{1}", sessionId, uriStringAfterServiceRoot);
var changedRegion = "TestRegion";
var changedAddress = "1 World Way, Los Angeles, CA, 90045";
string patchContent =
string.Format(
"{{\r\n \"Location\":{{\r\n \"Address\":\"{0}\",\r\n \"City\":{{\r\n \"Region\":\"{1}\"\r\n }}\r\n }}\r\n}}",
changedAddress,
changedRegion);
TestPatchStatusCodeIs(uriStringAfterServiceRoot, patchContent, HttpStatusCode.NoContent);

// Test patch results.
dynamic content = JsonConvert.DeserializeObject(originContent);
content.Location.Address = changedAddress;
content.Location.City.Region = changedRegion;
string changedContent = JsonConvert.SerializeObject(content);
TestGetPayloadContains(uriStringAfterServiceRoot, changedContent);
}

private static string GetSessionIdFromResponse(string response)
{
var match = Regex.Match(response, @"/\(S\((\w+)\)\)");
if (match.Success)
{
return match.Groups[1].Value;
}

return default(string);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<package id="Microsoft.OData.Core" version="6.15.0" targetFramework="net45" />
<package id="Microsoft.OData.Edm" version="6.15.0" targetFramework="net45" />
<package id="Microsoft.Spatial" version="6.15.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="xunit" version="2.1.0" targetFramework="net45" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.1.0" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private static void SetValues(object instance, Type type, IReadOnlyDictionary<st

if (dic != null)
{
value = Activator.CreateInstance(propertyInfo.PropertyType);
value = propertyInfo.GetValue(instance);
SetValues(value, propertyInfo.PropertyType, dic);
}
else if (propertyInfo.PropertyType.IsGenericType)
Expand Down

0 comments on commit f7fafd6

Please sign in to comment.