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 Microsoft . AspNetCore . Http ;
8
+ using Microsoft . Azure . WebJobs . Script . Description ;
9
+ using Microsoft . Azure . WebJobs . Script . Grpc ;
10
+ using Microsoft . Extensions . Logging ;
11
+ using Moq ;
12
+ using Xunit ;
13
+ using Yarp . ReverseProxy . Forwarder ;
14
+
15
+ namespace Microsoft . Azure . WebJobs . Script . Tests
16
+ {
17
+ public class DefaultHttpProxyServiceTests
18
+ {
19
+ private readonly Mock < IHttpForwarder > _httpForwarderMock ;
20
+ private readonly Mock < ILogger < DefaultHttpProxyService > > _loggerMock ;
21
+ private readonly DefaultHttpProxyService _proxyService ;
22
+
23
+ public DefaultHttpProxyServiceTests ( )
24
+ {
25
+ _httpForwarderMock = new Mock < IHttpForwarder > ( ) ;
26
+ _loggerMock = new Mock < ILogger < DefaultHttpProxyService > > ( ) ;
27
+ _proxyService = new DefaultHttpProxyService ( _httpForwarderMock . Object , _loggerMock . Object ) ;
28
+ }
29
+
30
+ [ Fact ]
31
+ public void StartForwarding_SetsCorrelationHeader ( )
32
+ {
33
+ var httpContext = new DefaultHttpContext ( ) ;
34
+ httpContext . Items . Add ( ScriptConstants . AzureFunctionsHttpTriggerContext , new ( ) ) ;
35
+ var invocationId = Guid . NewGuid ( ) ;
36
+ var context = new ScriptInvocationContext
37
+ {
38
+ FunctionMetadata = new FunctionMetadata { Name = "TestFunction" } ,
39
+ ExecutionContext = new ExecutionContext { InvocationId = invocationId } ,
40
+ Inputs = new List < ( string Name , DataType Type , object Val ) >
41
+ {
42
+ ( "req" , DataType . String , httpContext . Request )
43
+ } ,
44
+ Properties = new Dictionary < string , object > ( )
45
+ } ;
46
+
47
+ var httpUri = new Uri ( "http://localhost" ) ;
48
+
49
+ _proxyService . StartForwarding ( context , httpUri ) ;
50
+
51
+ var httpRequest = ( HttpRequest ) context . Inputs . First ( ) . Val ;
52
+ Assert . True ( httpRequest . Headers . ContainsKey ( ScriptConstants . HttpProxyCorrelationHeader ) ) ;
53
+ Assert . Equal ( invocationId . ToString ( ) , httpRequest . Headers [ ScriptConstants . HttpProxyCorrelationHeader ] ) ;
54
+ }
55
+
56
+ [ Fact ]
57
+ public void StartForwarding_OverridesExistingCorrelationHeader ( )
58
+ {
59
+ var httpContext = new DefaultHttpContext ( ) ;
60
+ httpContext . Items . Add ( ScriptConstants . AzureFunctionsHttpTriggerContext , new ( ) ) ;
61
+ var invocationId = Guid . NewGuid ( ) ;
62
+ var context = new ScriptInvocationContext
63
+ {
64
+ FunctionMetadata = new FunctionMetadata { Name = "TestFunction" } ,
65
+ ExecutionContext = new ExecutionContext { InvocationId = invocationId } ,
66
+ Inputs = new List < ( string Name , DataType Type , object Val ) >
67
+ {
68
+ ( "req" , DataType . String , httpContext . Request )
69
+ } ,
70
+ Properties = new Dictionary < string , object > ( )
71
+ } ;
72
+
73
+ var httpUri = new Uri ( "http://localhost" ) ;
74
+ var existingCorrelationId = Guid . NewGuid ( ) . ToString ( ) ;
75
+ var httpRequest = ( HttpRequest ) context . Inputs . First ( ) . Val ;
76
+ httpRequest . Headers [ ScriptConstants . HttpProxyCorrelationHeader ] = existingCorrelationId ;
77
+
78
+ _proxyService . StartForwarding ( context , httpUri ) ;
79
+ Assert . Equal ( invocationId . ToString ( ) , httpRequest . Headers [ ScriptConstants . HttpProxyCorrelationHeader ] ) ;
80
+ }
81
+ }
82
+ }
0 commit comments