diff --git a/src/Sarif/Core/SarifLog.cs b/src/Sarif/Core/SarifLog.cs index 797fa58f3..5412183bc 100644 --- a/src/Sarif/Core/SarifLog.cs +++ b/src/Sarif/Core/SarifLog.cs @@ -76,7 +76,7 @@ public static SarifLog Load(Stream source, bool deferred = false) /// /// /// - /// If the SarifLog has been posted. + /// If the SarifLog has been posted successfully. public static async Task Post(Uri postUri, string filePath, IFileSystem fileSystem, @@ -101,8 +101,16 @@ public static SarifLog Load(Stream source, bool deferred = false) return false; } - await Post(postUri, new MemoryStream(fileBytes), httpClient); - Console.WriteLine($"Posted log file successfully to: {postUri}"); + HttpResponseMessage response = await Post(postUri, new MemoryStream(fileBytes), httpClient); + string responseText = await response.Content.ReadAsStringAsync(); + + if (!response.IsSuccessStatusCode) + { + Console.WriteLine($"Error Posting log file to: {postUri}. Endpoint provided status code {response.StatusCode} and message: {responseText}"); + return false; + } + + Console.WriteLine($"Posted log file successfully to: {postUri}. Endpoint provided status code {response.StatusCode} and message: {responseText}"); return true; } diff --git a/src/Test.UnitTests.Sarif/Core/SarifLogTests.cs b/src/Test.UnitTests.Sarif/Core/SarifLogTests.cs index d24aaf867..7ea42ceb5 100644 --- a/src/Test.UnitTests.Sarif/Core/SarifLogTests.cs +++ b/src/Test.UnitTests.Sarif/Core/SarifLogTests.cs @@ -263,7 +263,7 @@ public async Task SarifLog_PostFile_WithInvalidParameters_ShouldThrowException() } [Fact] - public async Task SarifLog_PostFile_ShouldPostTests() + public async Task SarifLog_PostFile_PostTests() { using var assertionScope = new AssertionScope(); @@ -318,6 +318,17 @@ public async Task SarifLog_PostFile_ShouldPostTests() fileSystem.Object, httpClient: new HttpClient(httpMock)); logPosted.Should().BeFalse("with warning level ToolExecutionNotifications"); + + steam = (MemoryStream)CreateSarifLogStreamWithToolExecutionNotifications(FailureLevel.Error); + fileSystem + .Setup(f => f.FileReadAllBytes(It.IsAny())) + .Returns(steam.ToArray()); + httpMock.Mock(HttpMockHelper.CreateBadRequestResponse()); + logPosted = await SarifLog.Post(postUri: postUri, + filePath, + fileSystem.Object, + httpClient: new HttpClient(httpMock)); + logPosted.Should().BeTrue("with error level ToolExecutionNotifications, but the server returns BadRequest"); } [Fact]