JsonHttpPipeline only deserializes JSON responses

The JsonHttpPipeline should only try to deserialize responses that have
a Content-Type of application/json.
This commit is contained in:
Haacked
2013-10-06 21:05:18 -07:00
parent 9a33c68dbc
commit fa1473264c
6 changed files with 60 additions and 4 deletions
@@ -75,6 +75,21 @@ namespace Octokit.Tests.Http
Assert.Equal("phant", lastHeader.Value);
Assert.Equal("{}", response.Body);
Assert.Equal(httpStatusCode, response.StatusCode);
Assert.Null(response.ContentType);
}
public async Task SetsContentType(HttpStatusCode httpStatusCode)
{
var responseMessage = new HttpResponseMessage
{
StatusCode = httpStatusCode,
Content = new StringContent("{}", Encoding.UTF8, "application/json"),
};
var tester = new HttpClientAdapterTester();
var response = await tester.BuildResponseTester<object>(responseMessage);
Assert.Equal("application/json", response.ContentType);
}
}
+22 -1
View File
@@ -1,6 +1,7 @@
using System;
using Octokit.Http;
using Xunit;
using Xunit.Extensions;
namespace Octokit.Tests.Http
{
@@ -80,7 +81,11 @@ namespace Octokit.Tests.Http
public void DeserializesResponse()
{
const string data = "works";
var response = new ApiResponse<string> { Body = SimpleJson.SerializeObject(data) };
var response = new ApiResponse<string>
{
Body = SimpleJson.SerializeObject(data),
ContentType = "application/json"
};
var jsonPipeline = new JsonHttpPipeline();
jsonPipeline.DeserializeResponse(response);
@@ -88,6 +93,22 @@ namespace Octokit.Tests.Http
Assert.NotNull(response.BodyAsObject);
Assert.Equal(data, response.BodyAsObject);
}
[Fact]
public void IgnoresResponsesNotIdentifiedAsJson()
{
const string data = "works";
var response = new ApiResponse<string>
{
Body = SimpleJson.SerializeObject(data),
ContentType = "text/html"
};
var jsonPipeline = new JsonHttpPipeline();
jsonPipeline.DeserializeResponse(response);
Assert.Null(response.BodyAsObject);
}
}
}
}
+1
View File
@@ -23,5 +23,6 @@ namespace Octokit.Http
public Uri ResponseUri { get; set; }
public ApiInfo ApiInfo { get; set; }
public HttpStatusCode StatusCode { get; set; }
public string ContentType { get; set; }
}
}
+14
View File
@@ -26,11 +26,13 @@ namespace Octokit.Http
Ensure.ArgumentNotNull(responseMessage, "responseMessage");
string responseBody = null;
string contentType = null;
using (var content = responseMessage.Content)
{
if (content != null)
{
responseBody = await responseMessage.Content.ReadAsStringAsync();
contentType = GetContentType(content);
}
}
@@ -38,6 +40,7 @@ namespace Octokit.Http
{
Body = responseBody,
StatusCode = responseMessage.StatusCode,
ContentType = contentType
};
foreach (var h in responseMessage.Headers)
@@ -56,7 +59,9 @@ namespace Octokit.Http
{
requestMessage = new HttpRequestMessage(request.Method, request.Endpoint);
foreach (var header in request.Headers)
{
requestMessage.Headers.Add(header.Key, header.Value);
}
var body = request.Body as string;
if (body != null)
@@ -80,5 +85,14 @@ namespace Octokit.Http
return requestMessage;
}
static string GetContentType(HttpContent httpContent)
{
if (httpContent.Headers != null && httpContent.Headers.ContentType != null)
{
return httpContent.Headers.ContentType.MediaType;
}
return null;
}
}
}
+1
View File
@@ -17,5 +17,6 @@ namespace Octokit.Http
Uri ResponseUri { get; set; }
ApiInfo ApiInfo { get; set; }
HttpStatusCode StatusCode { get; set; }
string ContentType { get; set; }
}
}
+7 -3
View File
@@ -1,4 +1,5 @@
using System.Net.Http;
using System;
using System.Net.Http;
namespace Octokit.Http
{
@@ -43,8 +44,11 @@ namespace Octokit.Http
{
Ensure.ArgumentNotNull(response, "response");
var json = _serializer.Deserialize<T>(response.Body);
response.BodyAsObject = json;
if (response.ContentType != null && response.ContentType.Equals("application/json", StringComparison.Ordinal))
{
var json = _serializer.Deserialize<T>(response.Body);
response.BodyAsObject = json;
}
}
}
}