Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions RestAssured.Net.Tests/ResponseBodyDeserializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,30 @@ public void ObjectCanBeDeserializedFromJson()
Assert.That(firstPlace.IsCapital, Is.EqualTo(this.isCapital));
}

/// <summary>
/// A test demonstrating RestAssuredNet syntax for deserializing
/// a JSON response into an object when performing an HTTP GET.
/// </summary>
[Test]
public void ObjectCanBeDeserializedAsTypeFromJson()
{
this.CreateStubForJsonResponseBody();

Location responseLocation = Given()
.When()
.Get($"{MOCK_SERVER_BASE_URL}/json-deserialization")
.DeserializeTo<Location>();

Assert.That(responseLocation.Country, Is.EqualTo(this.country));
Assert.That(responseLocation.Places?.Count, Is.EqualTo(2));

Place firstPlace = responseLocation.Places!.First();

Assert.That(firstPlace.Name, Is.EqualTo(this.placeName));
Assert.That(firstPlace.Inhabitants, Is.EqualTo(this.placeInhabitants));
Assert.That(firstPlace.IsCapital, Is.EqualTo(this.isCapital));
}

/// <summary>
/// A test demonstrating RestAssuredNet syntax for deserializing
/// a JSON response into an object when performing an HTTP GET
Expand Down
52 changes: 33 additions & 19 deletions RestAssured.Net/Response/Deserialization/Deserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

namespace RestAssured.Response.Deserialization
{
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Newtonsoft.Json;
using RestAssured.Response.Exceptions;
Expand All @@ -36,9 +38,9 @@ internal class Deserializer
/// <param name="jsonSerializerSettings">The <see cref="JsonSerializerSettings"/> to use when deserializing to JSON.</param>
/// <returns>The deserialized response body object.</returns>
/// <exception cref="DeserializationException">Thrown when deserialization of the response body fails.</exception>
internal static object DeserializeResponseInto(HttpResponseMessage response, Type type, DeserializeAs deserializeAs, JsonSerializerSettings jsonSerializerSettings)
internal static object? DeserializeResponseInto(HttpResponseMessage response, Type type, DeserializeAs deserializeAs, JsonSerializerSettings jsonSerializerSettings)
{
string responseBodyAsString = response.Content.ReadAsStringAsync().Result;
string responseBodyAsString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();

if (string.IsNullOrEmpty(responseBodyAsString))
{
Expand All @@ -51,39 +53,51 @@ internal static object DeserializeResponseInto(HttpResponseMessage response, Typ
{
case DeserializeAs.UseResponseContentTypeHeaderValue:
{
responseMediaType = response.Content.Headers.ContentType?.MediaType;
break;
responseMediaType = response.Content.Headers.ContentType?.MediaType;
break;
}

case DeserializeAs.Json:
{
responseMediaType = "application/json";
break;
responseMediaType = "application/json";
break;
}

case DeserializeAs.Xml:
{
responseMediaType = "application/xml";
break;
responseMediaType = "application/xml";
break;
}
}

if (responseMediaType == null || responseMediaType.Contains("json"))
{
return JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result, type, jsonSerializerSettings) ?? string.Empty;
return JsonConvert.DeserializeObject(responseBodyAsString, type, jsonSerializerSettings);
}
else if (responseMediaType.Contains("xml"))

if (responseMediaType.Contains("xml"))
{
XmlSerializer xmlSerializer = new XmlSerializer(type);
using (TextReader reader = new StringReader(response.Content.ReadAsStringAsync().Result))
{
return xmlSerializer.Deserialize(reader) !;
}
}
else
{
throw new DeserializationException($"Unable to deserialize response with Content-Type '{responseMediaType}'");
using TextReader reader = new StringReader(responseBodyAsString);
return xmlSerializer.Deserialize(reader);
}

throw new DeserializationException($"Unable to deserialize response with Content-Type '{responseMediaType}'");
}

/// <summary>
/// Deserializes an <see cref="HttpResponseMessage"/> body into the specified type.
/// </summary>
/// <typeparam name="T">The type to deserialize the response body into.</typeparam>
/// <param name="response">The <see cref="HttpResponseMessage"/> containing the body to be deserialized.</param>
/// <param name="deserializeAs">Indicates how to interpret the response content when deserializing.</param>
/// <param name="jsonSerializerSettings">The <see cref="JsonSerializerSettings"/> to use when deserializing to JSON.</param>
/// <returns>The deserialized response body object.</returns>
/// <exception cref="DeserializationException">Thrown when deserialization of the response body fails.</exception>
internal static T? DeserializeResponseInto<T>(HttpResponseMessage response, DeserializeAs deserializeAs, JsonSerializerSettings jsonSerializerSettings)
where T : class
{
return DeserializeResponseInto(response, typeof(T), deserializeAs, jsonSerializerSettings) as T;
}
}
}
}
14 changes: 13 additions & 1 deletion RestAssured.Net/Response/VerifiableResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,23 @@ public VerifiableResponse UsingJsonSerializerSettings(JsonSerializerSettings jso
/// <param name="type">The object type to deserialize into.</param>
/// <param name="deserializeAs">Indicates how to interpret the response content when deserializing.</param>
/// <returns>The deserialized response object.</returns>
public object DeserializeTo(Type type, DeserializeAs deserializeAs = DeserializeAs.UseResponseContentTypeHeaderValue)
public object? DeserializeTo(Type type, DeserializeAs deserializeAs = DeserializeAs.UseResponseContentTypeHeaderValue)
{
return Deserializer.DeserializeResponseInto(this.Response, type, deserializeAs, this.jsonSerializerSettings);
}

/// <summary>
/// Deserializes the response content into the specified type and returns it.
/// </summary>
/// <typeparam name="T">The type to deserialize the response body into.</typeparam>
/// <param name="deserializeAs">Indicates how to interpret the response content when deserializing.</param>
/// <returns>The deserialized response object.</returns>
public T? DeserializeTo<T>(DeserializeAs deserializeAs = DeserializeAs.UseResponseContentTypeHeaderValue)
where T : class
{
return Deserializer.DeserializeResponseInto<T>(this.Response, deserializeAs, this.jsonSerializerSettings);
}

/// <summary>
/// Logs response details to the standard output.
/// </summary>
Expand Down
Loading