Skip to content

Commit

Permalink
Added the ability to set up custom media type formatters for serializ…
Browse files Browse the repository at this point in the history
…ation when making request.
  • Loading branch information
toddmeinershagen committed Jan 24, 2015
1 parent 1d035e0 commit 461fc5b
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.0\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down
9 changes: 8 additions & 1 deletion James.Testing.Rest.IntegrationTests/Models/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

namespace James.Testing.Rest.IntegrationTests.Models
{
public class Person
public interface IPerson
{
Guid Id { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
}

public class Person : IPerson
{
public Guid Id { get; set; }
public string FirstName { get; set; }
Expand Down
41 changes: 41 additions & 0 deletions James.Testing.Rest.IntegrationTests/RequestTests/GetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http.Formatting;
using FluentAssertions;
using James.Testing.Rest.IntegrationTests.Models;
using Nancy;
using Newtonsoft.Json;
using NUnit.Framework;
using HttpStatusCode = System.Net.HttpStatusCode;

Expand Down Expand Up @@ -87,6 +89,45 @@ public void given_uri_for_existing_resource_with_headers_when_getting_as_dynamic
.Verify(r => r.Body.dateOfBirth == "11/23/1972 12:00:00 AM");
}

[Test]
public void given_uri_for_existing_resource_with_custom_mediatypeformatter_when_getting_should_return_resource_properly()
{
Request
.WithUri(GetUriString(GetModule.QueryResource))
.WithQueryValue("FirstName", "Todd")
.WithQueryValue("LastName", "Meinershagen")
.WithFormatter(new JsonMediaTypeFormatter
{
SerializerSettings = new JsonSerializerSettings
{
Converters = new List<JsonConverter> {new PersonConverter()}
}
})
.Get<IPerson>()
.VerifyThat(r => r.StatusCode.Should().Be(HttpStatusCode.OK))
.VerifyThat(r => r.Body.FirstName.Should().Be("Todd"))
.VerifyThat(r => r.Body.LastName.Should().Be("Meinershagen"));
}

public class PersonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(IPerson);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
return serializer.Deserialize<Person>(reader);
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}

[Test]
public void given_uri_for_non_existing_resource_when_getting_as_dynamic_should_return_with_bad_request_status_with_dynamic_error_object()
{
Expand Down
219 changes: 0 additions & 219 deletions James.Testing.Rest.IntegrationTests/RequestTests/GetTests.cs.orig

This file was deleted.

1 change: 1 addition & 0 deletions James.Testing.Rest.IntegrationTests/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentAssertions" version="3.1.229" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.0" targetFramework="net45" />
<package id="Nancy" version="0.23.2" targetFramework="net45" />
<package id="Nancy.Hosting.Self" version="0.23.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
Expand Down
11 changes: 8 additions & 3 deletions James.Testing.Rest/GetRequest.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
using System;
using System.Net.Http;
using System.Net.Http.Formatting;

namespace James.Testing.Rest
{
internal class GetRequest<TResponse, TError> : RequestBase<TResponse, TError>
{
public GetRequest(string uriString, object headers, DynamicDictionary query)
private readonly MediaTypeFormatter _formatter;

public GetRequest(string uriString, object headers, DynamicDictionary query, MediaTypeFormatter formatter)
: base(uriString, headers, query)
{}
{
_formatter = formatter;
}

protected override IResponse<TResponse, TError> GetResponse(Uri uri, HttpClient client)
{
var response = client.GetAsync(uri.PathAndQuery).Result;
return new Response<TResponse, TError>(response);
return new Response<TResponse, TError>(response, _formatter);
}
}
}
2 changes: 1 addition & 1 deletion James.Testing.Rest/James.Testing.Rest.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<projectUrl>https://github.com/toddmeinershagen/James.Testing</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes><![CDATA[Added overload for WithUri() that accepts a list of objects to format the url. Example: Request.WithUri("http://someuri.com/{0}", 12345) would render as http://someuri.com/12345.]]></releaseNotes>
<releaseNotes><![CDATA[Added the ability to make a request with a different MediaTypeFormatter using the .WithFormatter() method for custom serialization techniques.]]></releaseNotes>
<copyright>Copyright 2014</copyright>
<tags>test .net dotnet testing rest</tags>
<dependencies>
Expand Down
4 changes: 2 additions & 2 deletions James.Testing.Rest/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.6.4.0")]
[assembly: AssemblyFileVersion("0.6.4.0")]
[assembly: AssemblyVersion("0.6.5.0")]
[assembly: AssemblyFileVersion("0.6.5.0")]

[assembly: InternalsVisibleTo("James.Testing.Rest.IntegrationTests")]
10 changes: 9 additions & 1 deletion James.Testing.Rest/Request.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net.Http.Formatting;
using System.Threading;
using JetBrains.Annotations;

Expand All @@ -9,6 +10,7 @@ public class Request
internal readonly string UriString;
private object _headers;
private DynamicDictionary _query;
private MediaTypeFormatter _formatter;

private Request(string uriString)
{
Expand Down Expand Up @@ -47,6 +49,12 @@ public Request WithQueryValue(string name, string value)
return this;
}

public Request WithFormatter(MediaTypeFormatter formatter)
{
_formatter = formatter;
return this;
}

public IResponse<dynamic, dynamic> Get()
{
return Get<dynamic, dynamic>();
Expand All @@ -59,7 +67,7 @@ public IResponse<TResponse, dynamic> Get<TResponse>()

public IResponse<TResponse, TError> Get<TResponse, TError>()
{
return Execute(new GetRequest<TResponse, TError>(UriString, _headers, _query));
return Execute(new GetRequest<TResponse, TError>(UriString, _headers, _query, _formatter));
}

public IResponse<dynamic, dynamic> Post(dynamic body)
Expand Down
Loading

0 comments on commit 461fc5b

Please sign in to comment.