Skip to content

Commit

Permalink
fix: JsonReaderException unexpected character encountered while parsi…
Browse files Browse the repository at this point in the history
…ng (#113)
  • Loading branch information
aldanchenko authored Apr 4, 2022
1 parent b9561c0 commit ef05143
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Snyk.VisualStudio.Extension.Shared/Service/SnykApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ public async Task<SastSettings> GetSastSettingsAsync()
var response = await this.SendSastSettingsRequestAsync();
var responseContent = await response.Content.ReadAsStringAsync();

return Json.Deserialize<SastSettings>(responseContent);
try
{
return Json.Deserialize<SastSettings>(responseContent);
}
catch (Exception e)
{
// In case of invalid json string return null.
return null;
}
}

/// <inheritdoc/>
Expand Down
20 changes: 20 additions & 0 deletions Snyk.VisualStudio.Extension.Tests/SnykApiServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,25 @@ public async Task SnykApiService_InvalidAuthTokenProvided_ReturnNullAsync()

Assert.Null(sastSettings);
}

[Fact]
public async Task SnykApiService_InvalidUrlProvided_ReturnNullAsync()
{
var optionsMock = new Mock<ISnykOptions>();

optionsMock
.Setup(options => options.CustomEndpoint)
.Returns("https://snyk.io/");

optionsMock
.Setup(options => options.ApiToken)
.Returns(Environment.GetEnvironmentVariable("TEST_API_TOKEN"));

var apiService = new SnykApiService(optionsMock.Object);

var sastSettings = await apiService.GetSastSettingsAsync();

Assert.Null(sastSettings);
}
}
}

0 comments on commit ef05143

Please sign in to comment.