Skip to content

Commit

Permalink
Merge pull request WebApiContrib#72 from panesofglass/master
Browse files Browse the repository at this point in the history
Fix failing tests
  • Loading branch information
panesofglass committed Jun 27, 2012
2 parents 811d2e2 + 3e2563c commit ff1e88f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
22 changes: 11 additions & 11 deletions src/WebApiContrib/MessageHandlers/LoggingHandler.cs
Expand Up @@ -2,6 +2,8 @@
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using WebApiContrib.Data;
using WebApiContrib.Messages;
Expand All @@ -24,21 +26,19 @@ public LoggingHandler(HttpMessageHandler innerHandler, ILoggingRepository reposi
_repository = repository;
}

protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Log the request information
LogRequestLoggingInfo(request);

// Execute the request
var response = base.SendAsync(request, cancellationToken);

response.ContinueWith((responseMsg) =>
return base.SendAsync(request, cancellationToken).ContinueWith(task =>
{
var response = task.Result;
// Extract the response logging info then persist the information
LogResponseLoggingInfo(responseMsg.Result);
LogResponseLoggingInfo(response);
return response;
});

return response;
}

private void LogRequestLoggingInfo(HttpRequestMessage request)
Expand All @@ -54,9 +54,9 @@ private void LogRequestLoggingInfo(HttpRequestMessage request)
if (request.Content != null)
{
request.Content.ReadAsByteArrayAsync()
.ContinueWith((task) =>
.ContinueWith(task =>
{
info.BodyContent = System.Text.UTF8Encoding.UTF8.GetString(task.Result);
info.BodyContent = Encoding.UTF8.GetString(task.Result);
_repository.Log(info);
});
Expand All @@ -82,9 +82,9 @@ private void LogResponseLoggingInfo(HttpResponseMessage response)
if (response.Content != null)
{
response.Content.ReadAsByteArrayAsync()
.ContinueWith(t =>
.ContinueWith(task =>
{
var responseMsg = System.Text.UTF8Encoding.UTF8.GetString(t.Result);
var responseMsg = Encoding.UTF8.GetString(task.Result);
info.BodyContent = responseMsg;
_repository.Log(info);
});
Expand Down
13 changes: 5 additions & 8 deletions test/WebApiContribTests/MessageHandlers/EncodingHandlerTests.cs
Expand Up @@ -4,7 +4,6 @@
using System.Net.Http.Headers;
using System.Web.Http;
using NUnit.Framework;
using WebApiContrib.Formatting;
using WebApiContrib.MessageHandlers;
using WebApiContribTests.Helpers;

Expand Down Expand Up @@ -32,14 +31,12 @@ public void Post_Lots_Of_Contacts_Using_EncodingHandler_Test()
content.Add(c);
}

var request = new HttpRequestMessage();
var request = new HttpRequestMessage();
request.Content = new ObjectContent(typeof(List<Contact>), content, config.Formatters.JsonFormatter);
client.PostAsync("http://anything/api/contacts", request.Content).ContinueWith(task =>
{
var response = task.Result;
Assert.IsNotNull(response);
Assert.IsTrue(response.StatusCode == HttpStatusCode.Created);
});
var response = client.PostAsync("http://anything/api/contacts", request.Content).Result;

Assert.IsNotNull(response);
Assert.IsTrue(response.StatusCode == HttpStatusCode.Created);
}
}
}
21 changes: 7 additions & 14 deletions test/WebApiContribTests/MessageHandlers/LoggingHandlerTests.cs
Expand Up @@ -4,7 +4,6 @@
using System.Net.Http.Headers;
using System.Web.Http;
using NUnit.Framework;
using WebApiContrib.Formatting;
using WebApiContrib.MessageHandlers;
using WebApiContrib.Testing;
using WebApiContribTests.Helpers;
Expand All @@ -29,20 +28,14 @@ public void Log_Simple_Request_Test_Should_Log_Request_And_Response()
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var content = new List<Contact>();
var c = new Contact { Id = 1, Birthday = DateTime.Now.AddYears(-20) };
content.Add(c);
var content = new List<Contact> { new Contact { Id = 1, Birthday = DateTime.Now.AddYears(-20) } };
var request = new HttpRequestMessage { Content = new ObjectContent(typeof (List<Contact>), content, config.Formatters.JsonFormatter) };
var response = client.PostAsync("http://anything/api/contacts", request.Content).Result;

var request = new HttpRequestMessage();
request.Content = new ObjectContent(typeof(List<Contact>), content, config.Formatters.JsonFormatter);
client.PostAsync("http://anything/api/contacts", request.Content).ContinueWith(task =>
{
var response = task.Result;
Assert.IsNotNull(response);
Assert.AreEqual(2, dummyRepository.LogMessageCount);
Assert.IsTrue(dummyRepository.HasRequestMessageTypeBeenReceived, "No request message has been logged");
Assert.IsTrue(dummyRepository.HasResponseMessageTypeBeenReceived, "No Response message has been received");
});
Assert.IsNotNull(response);
Assert.AreEqual(2, dummyRepository.LogMessageCount);
Assert.IsTrue(dummyRepository.HasRequestMessageTypeBeenReceived, "No request message has been logged");
Assert.IsTrue(dummyRepository.HasResponseMessageTypeBeenReceived, "No Response message has been received");
}
}
}

0 comments on commit ff1e88f

Please sign in to comment.