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 ;
5
+ using System . Collections . Generic ;
6
+ using System . Linq ;
7
+ using System . Security . Claims ;
8
+ using System . Threading . Tasks ;
9
+ using Microsoft . AspNetCore . Http ;
10
+ using Microsoft . AspNetCore . Http . Features ;
11
+ using Microsoft . Azure . WebJobs . Extensions . Http ;
12
+ using Microsoft . Azure . WebJobs . Script . WebHost . Authentication ;
13
+ using Microsoft . Azure . WebJobs . Script . WebHost . Middleware ;
14
+ using Microsoft . Azure . WebJobs . Script . WebHost . Security . Authentication ;
15
+ using Microsoft . Extensions . Logging ;
16
+ using Microsoft . Extensions . Primitives ;
17
+ using Microsoft . WebJobs . Script . Tests ;
18
+ using Newtonsoft . Json . Linq ;
19
+ using Xunit ;
20
+
21
+ namespace Microsoft . Azure . WebJobs . Script . Tests . Handlers
22
+ {
23
+ public class SystemTraceMiddlewareTests
24
+ {
25
+ private readonly TestLoggerProvider _loggerProvider ;
26
+ private readonly SystemTraceMiddleware _middleware ;
27
+
28
+ public SystemTraceMiddlewareTests ( )
29
+ {
30
+ _loggerProvider = new TestLoggerProvider ( ) ;
31
+ var loggerFactory = new LoggerFactory ( ) ;
32
+ loggerFactory . AddProvider ( _loggerProvider ) ;
33
+
34
+ RequestDelegate requestDelegate = async ( HttpContext context ) =>
35
+ {
36
+ await Task . Delay ( 25 ) ;
37
+ } ;
38
+
39
+ var logger = loggerFactory . CreateLogger < SystemTraceMiddleware > ( ) ;
40
+ _middleware = new SystemTraceMiddleware ( requestDelegate , logger ) ;
41
+ }
42
+
43
+ [ Fact ]
44
+ public async Task SendAsync_WritesExpectedTraces ( )
45
+ {
46
+ string requestId = Guid . NewGuid ( ) . ToString ( ) ;
47
+ var context = new DefaultHttpContext ( ) ;
48
+ Uri uri = new Uri ( "http://functions.com/api/testfunc?code=123" ) ;
49
+ var requestFeature = context . Request . HttpContext . Features . Get < IHttpRequestFeature > ( ) ;
50
+ requestFeature . Method = "GET" ;
51
+ requestFeature . Scheme = uri . Scheme ;
52
+ requestFeature . Path = uri . GetComponents ( UriComponents . KeepDelimiter | UriComponents . Path , UriFormat . Unescaped ) ;
53
+ requestFeature . PathBase = string . Empty ;
54
+ requestFeature . QueryString = uri . GetComponents ( UriComponents . KeepDelimiter | UriComponents . Query , UriFormat . Unescaped ) ;
55
+
56
+ var headers = new HeaderDictionary ( ) ;
57
+ headers . Add ( ScriptConstants . AntaresLogIdHeaderName , new StringValues ( requestId ) ) ;
58
+ requestFeature . Headers = headers ;
59
+
60
+ var claims = new List < Claim >
61
+ {
62
+ new Claim ( SecurityConstants . AuthLevelClaimType , AuthorizationLevel . Function . ToString ( ) )
63
+ } ;
64
+ var identity = new ClaimsIdentity ( claims , AuthLevelAuthenticationDefaults . AuthenticationScheme ) ;
65
+ context . User = new ClaimsPrincipal ( identity ) ;
66
+
67
+ await _middleware . Invoke ( context ) ;
68
+
69
+ var logs = _loggerProvider . GetAllLogMessages ( ) . ToArray ( ) ;
70
+ Assert . Equal ( 2 , logs . Length ) ;
71
+
72
+ // validate executing trace
73
+ var log = logs [ 0 ] ;
74
+ Assert . Equal ( typeof ( SystemTraceMiddleware ) . FullName , log . Category ) ;
75
+ Assert . Equal ( LogLevel . Information , log . Level ) ;
76
+ var idx = log . FormattedMessage . IndexOf ( ':' ) ;
77
+ var message = log . FormattedMessage . Substring ( 0 , idx ) . Trim ( ) ;
78
+ Assert . Equal ( "Executing HTTP request" , message ) ;
79
+ var details = log . FormattedMessage . Substring ( idx + 1 ) . Trim ( ) ;
80
+ var jo = JObject . Parse ( details ) ;
81
+ Assert . Equal ( 3 , jo . Count ) ;
82
+ Assert . Equal ( requestId , jo [ "requestId" ] ) ;
83
+ Assert . Equal ( "GET" , jo [ "method" ] ) ;
84
+ Assert . Equal ( "/api/testfunc" , jo [ "uri" ] ) ;
85
+
86
+ // validate executed trace
87
+ log = logs [ 1 ] ;
88
+ Assert . Equal ( typeof ( SystemTraceMiddleware ) . FullName , log . Category ) ;
89
+ Assert . Equal ( LogLevel . Information , log . Level ) ;
90
+ idx = log . FormattedMessage . IndexOf ( ':' ) ;
91
+ message = log . FormattedMessage . Substring ( 0 , idx ) . Trim ( ) ;
92
+ Assert . Equal ( "Executed HTTP request" , message ) ;
93
+ details = log . FormattedMessage . Substring ( idx + 1 ) . Trim ( ) ;
94
+ jo = JObject . Parse ( details ) ;
95
+ Assert . Equal ( 6 , jo . Count ) ;
96
+ Assert . Equal ( requestId , jo [ "requestId" ] ) ;
97
+ Assert . Equal ( "GET" , jo [ "method" ] ) ;
98
+ Assert . Equal ( "/api/testfunc" , jo [ "uri" ] ) ;
99
+ Assert . Equal ( 200 , jo [ "status" ] ) ;
100
+ var duration = ( long ) jo [ "duration" ] ;
101
+ Assert . True ( duration > 0 ) ;
102
+
103
+ var authentication = ( JArray ) jo [ "identities" ] ;
104
+ Assert . Equal ( 1 , authentication . Count ) ;
105
+ var keyIdentity = authentication . Single ( ) ;
106
+ Assert . Equal ( AuthLevelAuthenticationDefaults . AuthenticationScheme , keyIdentity [ "type" ] ) ;
107
+ Assert . Equal ( "Function" , keyIdentity [ "level" ] ) ;
108
+ }
109
+ }
110
+ }
0 commit comments