|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.AspNetCore.Http; |
| 9 | +using Microsoft.AspNetCore.Http.Features; |
| 10 | +using Microsoft.Azure.WebJobs.Script.Extensions; |
| 11 | +using Microsoft.Azure.WebJobs.Script.WebHost; |
| 12 | +using Microsoft.Azure.WebJobs.Script.WebHost.Extensions; |
| 13 | +using Microsoft.Extensions.Primitives; |
| 14 | +using Moq; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Extensions |
| 18 | +{ |
| 19 | + public class HttpContextExtensionsTests |
| 20 | + { |
| 21 | + [Fact] |
| 22 | + public async Task SetOfflineResponseAsync_ReturnsCorrectResponse() |
| 23 | + { |
| 24 | + // create a test offline file |
| 25 | + var rootPath = Path.GetTempPath(); |
| 26 | + var offlineFilePath = Path.Combine(Path.GetTempPath(), ScriptConstants.AppOfflineFileName); |
| 27 | + string content = FileUtility.ReadResourceString($"{ScriptConstants.ResourcePath}.{ScriptConstants.AppOfflineFileName}", typeof(HttpException).Assembly); |
| 28 | + File.WriteAllText(offlineFilePath, content); |
| 29 | + |
| 30 | + var sendFileFeatureMock = new Mock<IHttpSendFileFeature>(); |
| 31 | + sendFileFeatureMock.Setup(s => s.SendFileAsync(offlineFilePath, 0, null, CancellationToken.None)).Returns(Task.FromResult<int>(0)); |
| 32 | + |
| 33 | + // simulate an App Service request |
| 34 | + var vars = new Dictionary<string, string> |
| 35 | + { |
| 36 | + { EnvironmentSettingNames.AzureWebsiteInstanceId, "123" } |
| 37 | + }; |
| 38 | + using (var env = new TestScopedEnvironmentVariable(vars)) |
| 39 | + { |
| 40 | + // without header (thus an internal request) |
| 41 | + var context = new DefaultHttpContext(); |
| 42 | + context.Features.Set(sendFileFeatureMock.Object); |
| 43 | + Assert.True(context.Request.IsAppServiceInternalRequest()); |
| 44 | + await context.SetOfflineResponseAsync(rootPath); |
| 45 | + Assert.Equal(StatusCodes.Status503ServiceUnavailable, context.Response.StatusCode); |
| 46 | + Assert.Equal(0, context.Response.Body.Length); |
| 47 | + sendFileFeatureMock.Verify(p => p.SendFileAsync(offlineFilePath, 0, null, CancellationToken.None), Times.Never); |
| 48 | + |
| 49 | + // with header (thus an external request) |
| 50 | + context = new DefaultHttpContext(); |
| 51 | + context.Features.Set(sendFileFeatureMock.Object); |
| 52 | + context.Request.Headers.Add(ScriptConstants.AntaresLogIdHeaderName, new StringValues("456")); |
| 53 | + Assert.False(context.Request.IsAppServiceInternalRequest()); |
| 54 | + await context.SetOfflineResponseAsync(rootPath); |
| 55 | + Assert.Equal(StatusCodes.Status503ServiceUnavailable, context.Response.StatusCode); |
| 56 | + Assert.Equal("text/html", context.Response.Headers["Content-Type"]); |
| 57 | + sendFileFeatureMock.Verify(p => p.SendFileAsync(offlineFilePath, 0, null, CancellationToken.None), Times.Once); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments