Skip to content

Commit

Permalink
test: update test
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Jan 30, 2024
1 parent d0bdf8a commit 793c8d2
Showing 1 changed file with 9 additions and 33 deletions.
42 changes: 9 additions & 33 deletions Notation.Plugin.AzureKeyVault.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,41 +95,17 @@ internal interface IResponseMock
delegate bool TryGetHeaderCallback(string name, ref string value);


[Fact]
public void HandleAzureException_valid()
{
// Arrange
Mock<Response> responseMock = new Mock<Response>();
responseMock.SetupGet(r => r.Status).Returns(200);
responseMock.SetupGet(r => r.Content).Returns(BinaryData.FromString("{\"error\":{\"message\":\"TestErrorMessage\"}}"));

// mock headers
responseMock.CallBase = true;
responseMock.Protected().As<IResponseMock>().Setup(m => m.TryGetHeader(It.IsAny<string>(), out It.Ref<string>.IsAny))
.Returns(new TryGetHeaderCallback((string name, ref string value) =>
{
value = "ETAG";
Console.WriteLine(name);
return true;
}));

var exception = new RequestFailedException(responseMock.Object);

// Act
var errorResponse = Program.HandleAzureException(exception);

// Assert exit code 1
Assert.Equal("TestErrorMessage", errorResponse.ErrorMessage);
Assert.Equal("ERROR", errorResponse.ErrorCode);
}

[Fact]
public void HandleAzureException_content_is_not_json()
[Theory]
[InlineData(200, "{\"error\":{\"message\":\"TestErrorMessage\"}}", "TestErrorMessage")]
[InlineData(500, "{\"error\":{\"message\":\"TestErrorMessage\"}", "Service request failed.\nStatus: 500\n\nHeaders:\n")]
[InlineData(500, "{\"error2\":{\"message\":\"TestErrorMessage\"}}", "Service request failed.\nStatus: 500\n\nHeaders:\n")]
[InlineData(500, "{\"error\":{\"message2\":\"TestErrorMessage\"}}", "Service request failed.\nStatus: 500\n\nHeaders:\n")]
public void HandleAzureException(int code, string content, string expectedErrorMessage)
{
// Arrange
Mock<Response> responseMock = new Mock<Response>();
responseMock.SetupGet(r => r.Status).Returns(500);
responseMock.SetupGet(r => r.Content).Returns(BinaryData.FromString("{\"error\":{\"message\":\"TestErrorMessage\"}"));
responseMock.SetupGet(r => r.Status).Returns(code);
responseMock.SetupGet(r => r.Content).Returns(BinaryData.FromString(content));

// mock headers
responseMock.CallBase = true;
Expand All @@ -147,7 +123,7 @@ public void HandleAzureException_content_is_not_json()
var errorResponse = Program.HandleAzureException(exception);

// Assert exit code 1
Assert.Equal("Service request failed.\nStatus: 500\n\nHeaders:\n", errorResponse.ErrorMessage);
Assert.Equal(expectedErrorMessage, errorResponse.ErrorMessage);
Assert.Equal("ERROR", errorResponse.ErrorCode);
}
}
Expand Down

0 comments on commit 793c8d2

Please sign in to comment.