Skip to content

Commit

Permalink
fix: fallback to private body and headers for response deserialization (
Browse files Browse the repository at this point in the history
  • Loading branch information
childish-sambino authored Mar 22, 2022
1 parent 5ed9472 commit badfc6a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/SendGrid/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,37 @@ public HttpResponseHeaders Headers
/// <summary>
/// Converts string formatted response body to a Dictionary.
/// </summary>
/// <param name="content">https://docs.microsoft.com/dotnet/api/system.net.http.httpcontent.</param>
/// <returns>Dictionary object representation of HttpContent.</returns>
public virtual async Task<Dictionary<string, dynamic>> DeserializeResponseBodyAsync()
public virtual async Task<Dictionary<string, dynamic>> DeserializeResponseBodyAsync(HttpContent content = null)
{
if (this._body is null)
content = content ?? this._body;
if (content is null)
{
return new Dictionary<string, dynamic>();
}

var stringContent = await this._body.ReadAsStringAsync().ConfigureAwait(false);
var stringContent = await content.ReadAsStringAsync().ConfigureAwait(false);
var dsContent = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(stringContent);
return dsContent;
}

/// <summary>
/// Converts string formatted response headers to a Dictionary.
/// </summary>
/// <param name="headers">https://docs.microsoft.com/dotnet/api/system.net.http.headers.httpresponseheaders.</param>
/// <returns>Dictionary object representation of HttpResponseHeaders.</returns>
public virtual Dictionary<string, string> DeserializeResponseHeaders()
public virtual Dictionary<string, string> DeserializeResponseHeaders(HttpResponseHeaders headers = null)
{
var dsContent = new Dictionary<string, string>();
if (this._headers == null)

headers = headers ?? this._headers;
if (headers == null)
{
return dsContent;
}

foreach (var pair in this._headers)
foreach (var pair in headers)
{
dsContent.Add(pair.Key, pair.Value.First());
}
Expand Down
21 changes: 20 additions & 1 deletion tests/SendGrid.Tests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public async Task DeserializeResponseBodyAsync_JsonHttpContent_ReturnsBodyAsDict
Assert.Equal(new JArray() { "alerts.read" }, responseBody["scopes"]);
}

[Fact]
public async Task DeserializeResponseBodyAsync_OverrideHttpContent_ReturnsBodyAsDictionary()
{
var content = "{\"scopes\": [\"alerts.read\"]}";
var response = new Response(HttpStatusCode.OK, null, null);
Dictionary<string, dynamic> responseBody = await response.DeserializeResponseBodyAsync(new StringContent(content));
Assert.Equal(new JArray() { "alerts.read" }, responseBody["scopes"]);
}

[Fact]
public void DeserializeResponseHeaders_NullHttpResponseHeaders_ReturnsEmptyDictionary()
{
Expand All @@ -37,13 +46,23 @@ public void DeserializeResponseHeaders_NullHttpResponseHeaders_ReturnsEmptyDicti
}

[Fact]
public void DeserializeResponseHeaders_NullHttpResponseHeaders_ReturnsHeadersAsDictionary()
public void DeserializeResponseHeaders_HttpResponseHeaders_ReturnsHeadersAsDictionary()
{
var message = new HttpResponseMessage();
message.Headers.Add("HeaderKey", "HeaderValue");
var response = new Response(HttpStatusCode.OK, null, message.Headers);
Dictionary<string, string> responseHeadersDeserialized = response.DeserializeResponseHeaders();
Assert.Equal("HeaderValue", responseHeadersDeserialized["HeaderKey"]);
}

[Fact]
public void DeserializeResponseHeaders_OverrideHttpResponseHeaders_ReturnsHeadersAsDictionary()
{
var message = new HttpResponseMessage();
message.Headers.Add("HeaderKey", "HeaderValue");
var response = new Response(HttpStatusCode.OK, null, null);
Dictionary<string, string> responseHeadersDeserialized = response.DeserializeResponseHeaders(message.Headers);
Assert.Equal("HeaderValue", responseHeadersDeserialized["HeaderKey"]);
}
}
}

0 comments on commit badfc6a

Please sign in to comment.