|
| 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 Xunit; |
| 5 | +using AnalyzerTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpAnalyzerTest<Microsoft.Azure.Functions.Analyzers.AvoidNonStaticHttpClientAnalyzer, Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier>; |
| 6 | +using Verify = Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier<Microsoft.Azure.Functions.Analyzers.AvoidNonStaticHttpClientAnalyzer>; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.CodeAnalysis.Testing; |
| 9 | +using System.Collections.Immutable; |
| 10 | + |
| 11 | +namespace WebJobs.Script.Tests.Analyzers |
| 12 | +{ |
| 13 | + public class AvoidNonStaticHttpClientAnalyzerTests |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public async Task StaticHttpClient_NoDiagnostic() |
| 17 | + { |
| 18 | + string testCode = @" |
| 19 | +using Microsoft.Azure.WebJobs; |
| 20 | +using Microsoft.Extensions.Logging; |
| 21 | +using System.Net.Http; |
| 22 | +
|
| 23 | +namespace FunctionApp |
| 24 | +{ |
| 25 | + public static class SomeFunction |
| 26 | + { |
| 27 | + public static HttpClient httpClient = new HttpClient(); |
| 28 | +
|
| 29 | + [FunctionName(nameof(SomeFunction))] |
| 30 | + public static void Run([QueueTrigger(""myqueue-items"", Connection = """")] string myQueueItem, ILogger log) |
| 31 | + { |
| 32 | + httpClient.GetAsync(""https://www.microsoft.com""); |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | +"; |
| 37 | + |
| 38 | + var test = new AnalyzerTest(); |
| 39 | + test.ReferenceAssemblies = ReferenceAssemblies.Net.Net50.WithPackages(ImmutableArray.Create( |
| 40 | + new PackageIdentity("Microsoft.NET.Sdk.Functions", "3.0.11"), |
| 41 | + new PackageIdentity("Microsoft.Azure.WebJobs.Extensions.Storage", "3.0.10"))); |
| 42 | + |
| 43 | + test.TestCode = testCode; |
| 44 | + |
| 45 | + // 0 diagnostics expected |
| 46 | + |
| 47 | + await test.RunAsync(); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task LocalHttpClientVariable_Diagnostic() |
| 53 | + { |
| 54 | + string testCode = @" |
| 55 | +using Microsoft.Azure.WebJobs; |
| 56 | +using Microsoft.Extensions.Logging; |
| 57 | +using System.Net.Http; |
| 58 | +
|
| 59 | +namespace FunctionApp |
| 60 | +{ |
| 61 | + public static class SomeFunction |
| 62 | + { |
| 63 | + [FunctionName(nameof(SomeFunction))] |
| 64 | + public static void Run([QueueTrigger(""myqueue-items"", Connection = """")] string myQueueItem, ILogger log) |
| 65 | + { |
| 66 | + var httpClient = new HttpClient(); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | +"; |
| 71 | + |
| 72 | + var test = new AnalyzerTest(); |
| 73 | + test.ReferenceAssemblies = ReferenceAssemblies.Net.Net50.WithPackages(ImmutableArray.Create( |
| 74 | + new PackageIdentity("Microsoft.NET.Sdk.Functions", "3.0.11"), |
| 75 | + new PackageIdentity("Microsoft.Azure.WebJobs.Extensions.Storage", "3.0.10"))); |
| 76 | + |
| 77 | + test.TestCode = testCode; |
| 78 | + |
| 79 | + test.ExpectedDiagnostics.Add(Verify.Diagnostic() |
| 80 | + .WithSeverity(Microsoft.CodeAnalysis.DiagnosticSeverity.Warning) |
| 81 | + .WithSpan(13, 34, 13, 50)); |
| 82 | + |
| 83 | + await test.RunAsync(); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public async Task LocalNestedHttpClientVariable_Diagnostic() |
| 88 | + { |
| 89 | + string testCode = @" |
| 90 | +using Microsoft.Azure.WebJobs; |
| 91 | +using Microsoft.Extensions.Logging; |
| 92 | +using System.Net.Http; |
| 93 | +
|
| 94 | +namespace FunctionApp |
| 95 | +{ |
| 96 | + public static class SomeFunction |
| 97 | + { |
| 98 | + [FunctionName(nameof(SomeFunction))] |
| 99 | + public static void Run([QueueTrigger(""myqueue-items"", Connection = """")] string myQueueItem, ILogger log) |
| 100 | + { |
| 101 | + if (true) |
| 102 | + { |
| 103 | + using (var httpClient = new HttpClient()) |
| 104 | + { |
| 105 | + httpClient.GetAsync(""https://www.microsoft.com""); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | +"; |
| 112 | + |
| 113 | + var test = new AnalyzerTest(); |
| 114 | + test.ReferenceAssemblies = ReferenceAssemblies.Net.Net50.WithPackages(ImmutableArray.Create( |
| 115 | + new PackageIdentity("Microsoft.NET.Sdk.Functions", "3.0.11"), |
| 116 | + new PackageIdentity("Microsoft.Azure.WebJobs.Extensions.Storage", "3.0.10"))); |
| 117 | + |
| 118 | + test.TestCode = testCode; |
| 119 | + |
| 120 | + test.ExpectedDiagnostics.Add(Verify.Diagnostic() |
| 121 | + .WithSeverity(Microsoft.CodeAnalysis.DiagnosticSeverity.Warning) |
| 122 | + .WithSpan(15, 41, 15, 57)); |
| 123 | + |
| 124 | + await test.RunAsync(); |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + public async Task MethodArgument_Diagnostic() |
| 129 | + { |
| 130 | + string testCode = @" |
| 131 | +using Microsoft.Azure.WebJobs; |
| 132 | +using Microsoft.Extensions.Logging; |
| 133 | +using System.Net.Http; |
| 134 | +using System.Threading.Tasks; |
| 135 | +
|
| 136 | +namespace FunctionApp |
| 137 | +{ |
| 138 | + public static class SomeFunction |
| 139 | + { |
| 140 | + [FunctionName(nameof(SomeFunction))] |
| 141 | + public static void Run([QueueTrigger(""myqueue-items"", Connection = """")] string myQueueItem, ILogger log) |
| 142 | + { |
| 143 | + CallHttp(new HttpClient()); |
| 144 | + } |
| 145 | + private static Task<HttpResponseMessage> CallHttp(HttpClient httpClient) |
| 146 | + { |
| 147 | + return httpClient.GetAsync(""https://www.microsoft.com""); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +"; |
| 152 | + |
| 153 | + var test = new AnalyzerTest(); |
| 154 | + test.ReferenceAssemblies = ReferenceAssemblies.Net.Net50.WithPackages(ImmutableArray.Create( |
| 155 | + new PackageIdentity("Microsoft.NET.Sdk.Functions", "3.0.11"), |
| 156 | + new PackageIdentity("Microsoft.Azure.WebJobs.Extensions.Storage", "3.0.10"))); |
| 157 | + |
| 158 | + test.TestCode = testCode; |
| 159 | + |
| 160 | + test.ExpectedDiagnostics.Add(Verify.Diagnostic() |
| 161 | + .WithSeverity(Microsoft.CodeAnalysis.DiagnosticSeverity.Warning) |
| 162 | + .WithSpan(14, 26, 14, 42)); |
| 163 | + |
| 164 | + await test.RunAsync(); |
| 165 | + } |
| 166 | + |
| 167 | + [Fact] |
| 168 | + public async Task HttpClientDerivedClass_Diagnostic() |
| 169 | + { |
| 170 | + string testCode = @" |
| 171 | +using Microsoft.Azure.WebJobs; |
| 172 | +using Microsoft.Extensions.Logging; |
| 173 | +using System.Net.Http; |
| 174 | +
|
| 175 | +namespace FunctionApp |
| 176 | +{ |
| 177 | + public static class SomeFunction |
| 178 | + { |
| 179 | + [FunctionName(nameof(SomeFunction))] |
| 180 | + public static void Run([QueueTrigger(""myqueue-items"", Connection = """")]string myQueueItem, ILogger log) |
| 181 | + { |
| 182 | + var httpClient = new CustomHttpClient(); |
| 183 | + httpClient.GetAsync(""https://www.microsoft.com""); |
| 184 | + } |
| 185 | + } |
| 186 | +
|
| 187 | + public class CustomHttpClient : HttpClient { } |
| 188 | +} |
| 189 | +"; |
| 190 | + |
| 191 | + var test = new AnalyzerTest(); |
| 192 | + test.ReferenceAssemblies = ReferenceAssemblies.Net.Net50.WithPackages(ImmutableArray.Create( |
| 193 | + new PackageIdentity("Microsoft.NET.Sdk.Functions", "3.0.11"), |
| 194 | + new PackageIdentity("Microsoft.Azure.WebJobs.Extensions.Storage", "3.0.10"))); |
| 195 | + |
| 196 | + test.TestCode = testCode; |
| 197 | + |
| 198 | + test.ExpectedDiagnostics.Add(Verify.Diagnostic() |
| 199 | + .WithSeverity(Microsoft.CodeAnalysis.DiagnosticSeverity.Warning) |
| 200 | + .WithSpan(13, 30, 13, 52)); |
| 201 | + |
| 202 | + await test.RunAsync(); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments