Skip to content
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

Fix null reference access when ContentType is null #2501

Merged
merged 1 commit into from
Jul 21, 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
11 changes: 11 additions & 0 deletions Octokit.Tests/Exceptions/ApiExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
#if !NO_SERIALIZABLE
Expand Down Expand Up @@ -141,6 +142,16 @@ public void DoesNotThrowIfBodyIsNotDefined()
Assert.NotNull(stringRepresentation);
}

[Fact]
public void DoesNotThrowIfContentTypeIsNotDefined()
{
var response = CreateResponse(HttpStatusCode.GatewayTimeout, null, new Dictionary<string, string>(), null);

var exception = new ApiException(response);
var stringRepresentation = exception.ToString();
Assert.NotNull(stringRepresentation);
}

[Fact]
public void DoesNotPrintImageContent()
{
Expand Down
2 changes: 1 addition & 1 deletion Octokit/Exceptions/ApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ protected string HttpResponseBodySafe
{
get
{
return HttpResponse != null
return HttpResponse?.ContentType != null
&& !HttpResponse.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase)
&& HttpResponse.Body is string
? (string)HttpResponse.Body : string.Empty;
Expand Down