Skip to content

fix: fallback to private body and headers for response deserialization #1168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2022
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
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"]);
}
}
}