|
1 | 1 | using Xunit; |
2 | 2 | using Moq; |
3 | | -using Notation.Plugin.AzureKeyVault.Command; |
| 3 | +using Azure; |
4 | 4 | using Notation.Plugin.Protocol; |
5 | 5 | using System.IO; |
6 | 6 | using System.Threading.Tasks; |
7 | 7 | using System; |
| 8 | +using Moq.Protected; |
8 | 9 |
|
9 | 10 | namespace Notation.Plugin.AzureKeyVault.Tests |
10 | 11 | { |
@@ -84,5 +85,47 @@ public async Task ExecuteAsync_HandlesInvalidCommands(string command) |
84 | 85 | await Assert.ThrowsAsync<ValidationException>(() => Program.ExecuteAsync(args)); |
85 | 86 | } |
86 | 87 | } |
| 88 | + // we need this because of method being protected |
| 89 | + internal interface IResponseMock |
| 90 | + { |
| 91 | + bool TryGetHeader(string name, out string value); |
| 92 | + } |
| 93 | + |
| 94 | + // we need this to be able to define the callback with out parameter |
| 95 | + delegate bool TryGetHeaderCallback(string name, ref string value); |
| 96 | + |
| 97 | + |
| 98 | + [Theory] |
| 99 | + [InlineData(200, "{\"error\":{\"message\":\"TestErrorMessage\"}}", "TestErrorMessage")] |
| 100 | + [InlineData(500, "{\"error\":{\"message\":\"TestErrorMessage\"}", "Service request failed.\nStatus: 500\n\nHeaders:\n")] |
| 101 | + [InlineData(500, "{\"error2\":{\"message\":\"TestErrorMessage\"}}", "Service request failed.\nStatus: 500\n\nHeaders:\n")] |
| 102 | + [InlineData(500, "{\"error\":{\"message2\":\"TestErrorMessage\"}}", "Service request failed.\nStatus: 500\n\nHeaders:\n")] |
| 103 | + [InlineData(500, "{\"error\":{\"message\":\"\"}}", "\nStatus: 500\n\nHeaders:\n")] |
| 104 | + public void HandleAzureException(int code, string content, string expectedErrorMessage) |
| 105 | + { |
| 106 | + // Arrange |
| 107 | + Mock<Response> responseMock = new Mock<Response>(); |
| 108 | + responseMock.SetupGet(r => r.Status).Returns(code); |
| 109 | + responseMock.SetupGet(r => r.Content).Returns(BinaryData.FromString(content)); |
| 110 | + |
| 111 | + // mock headers |
| 112 | + responseMock.CallBase = true; |
| 113 | + responseMock.Protected().As<IResponseMock>().Setup(m => m.TryGetHeader(It.IsAny<string>(), out It.Ref<string>.IsAny)) |
| 114 | + .Returns(new TryGetHeaderCallback((string name, ref string value) => |
| 115 | + { |
| 116 | + value = "ETAG"; |
| 117 | + Console.WriteLine(name); |
| 118 | + return true; |
| 119 | + })); |
| 120 | + |
| 121 | + var exception = new RequestFailedException(responseMock.Object); |
| 122 | + |
| 123 | + // Act |
| 124 | + var errorResponse = Program.HandleAzureException(exception); |
| 125 | + |
| 126 | + // Assert exit code 1 |
| 127 | + Assert.Equal(expectedErrorMessage, errorResponse.ErrorMessage); |
| 128 | + Assert.Equal("ERROR", errorResponse.ErrorCode); |
| 129 | + } |
87 | 130 | } |
88 | 131 | } |
0 commit comments