5
5
using Xunit ;
6
6
using Moq ;
7
7
using System . Text ;
8
+ using Microsoft . Extensions . Primitives ;
9
+ using System . Linq ;
8
10
9
11
public class HttpContextExtensionsTests
10
12
{
@@ -52,7 +54,7 @@ public void ToApiGatewayHttpV2Request_ShouldReturnValidApiGatewayHttpApiV2ProxyR
52
54
request . QueryString = new QueryString ( "?status=pending" ) ;
53
55
request . Headers [ "User-Agent" ] = "TestAgent" ;
54
56
request . Headers [ "Accept" ] = "application/json" ;
55
- request . Headers [ "Cookie" ] = "session=abc123; theme=dark; complex=this+has+spaces ;" ;
57
+ request . Headers [ "Cookie" ] = "session=abc123; theme=dark; complex=this+has+special ;" ;
56
58
57
59
var result = context . ToApiGatewayHttpV2Request ( ) ;
58
60
@@ -64,7 +66,7 @@ public void ToApiGatewayHttpV2Request_ShouldReturnValidApiGatewayHttpApiV2ProxyR
64
66
Assert . Equal ( 3 , result . Cookies . Length ) ;
65
67
Assert . Contains ( "session=abc123" , result . Cookies ) ;
66
68
Assert . Contains ( "theme=dark" , result . Cookies ) ;
67
- Assert . Contains ( "complex=this%2bhas%2bspaces " , result . Cookies ) ;
69
+ Assert . Contains ( "complex=this%2bhas%2bspecial " , result . Cookies ) ;
68
70
Assert . Equal ( "123" , result . PathParameters [ "userId" ] ) ;
69
71
Assert . Equal ( "GET" , result . RequestContext . Http . Method ) ;
70
72
Assert . Equal ( "/api/users/123/orders" , result . RequestContext . Http . Path ) ;
@@ -159,39 +161,37 @@ public void ToApiGatewayRequest_ShouldReturnValidApiGatewayProxyRequest()
159
161
request . Path = "/api/users/123/orders" ;
160
162
request . QueryString = new QueryString ( "?status=pending&tag=important&tag=urgent" ) ;
161
163
request . Headers [ "User-Agent" ] = "TestAgent" ;
162
- request . Headers [ "Accept" ] = new Microsoft . Extensions . Primitives . StringValues ( new [ ] { "text/html" , "application/json" } ) ;
163
- request . Headers [ "Cookie" ] = "session=abc123; theme=dark" ;
164
+ request . Headers [ "Accept" ] = new StringValues ( new [ ] { "text/html" , "application/json" } ) ;
165
+ request . Headers [ "Cookie" ] = "session=abc123; theme=dark; complex=this+has+special; " ;
164
166
request . Headers [ "X-Custom-Header" ] = "value1" ;
165
167
166
168
_mockHttpRequestUtility . Setup ( x => x . ExtractHeaders ( It . IsAny < IHeaderDictionary > ( ) ) )
167
169
. Returns ( (
168
170
new Dictionary < string , string >
169
171
{
170
- { "User-Agent" , "TestAgent" } ,
171
- { "Accept" , "application/json" } ,
172
- { "Cookie" , "session=abc123; theme=dark" } ,
173
- { "X-Custom-Header" , "value1" }
172
+ { "User-Agent" , "TestAgent" } ,
173
+ { "Accept" , "application/json" } ,
174
+ { "X-Custom-Header" , "value1" }
174
175
} ,
175
176
new Dictionary < string , IList < string > >
176
177
{
177
- { "User-Agent" , new List < string > { "TestAgent" } } ,
178
- { "Accept" , new List < string > { "text/html" , "application/json" } } ,
179
- { "Cookie" , new List < string > { "session=abc123; theme=dark" } } ,
180
- { "X-Custom-Header" , new List < string > { "value1" } }
178
+ { "User-Agent" , new List < string > { "TestAgent" } } ,
179
+ { "Accept" , new List < string > { "text/html" , "application/json" } } ,
180
+ { "X-Custom-Header" , new List < string > { "value1" } }
181
181
}
182
182
) ) ;
183
183
184
184
_mockHttpRequestUtility . Setup ( x => x . ExtractQueryStringParameters ( It . IsAny < IQueryCollection > ( ) ) )
185
185
. Returns ( (
186
186
new Dictionary < string , string >
187
187
{
188
- { "status" , "pending" } ,
189
- { "tag" , "urgent" }
188
+ { "status" , "pending" } ,
189
+ { "tag" , "urgent" }
190
190
} ,
191
191
new Dictionary < string , IList < string > >
192
192
{
193
- { "status" , new List < string > { "pending" } } ,
194
- { "tag" , new List < string > { "important" , "urgent" } }
193
+ { "status" , new List < string > { "pending" } } ,
194
+ { "tag" , new List < string > { "important" , "urgent" } }
195
195
}
196
196
) ) ;
197
197
@@ -204,13 +204,11 @@ public void ToApiGatewayRequest_ShouldReturnValidApiGatewayProxyRequest()
204
204
205
205
Assert . Equal ( "TestAgent" , result . Headers [ "User-Agent" ] ) ;
206
206
Assert . Equal ( "application/json" , result . Headers [ "Accept" ] ) ;
207
- Assert . Equal ( "session=abc123; theme=dark" , result . Headers [ "Cookie" ] ) ;
208
207
Assert . Equal ( "value1" , result . Headers [ "X-Custom-Header" ] ) ;
209
208
210
- Assert . Equal ( new List < string > { "TestAgent" } , result . MultiValueHeaders [ "User-Agent" ] ) ;
211
- Assert . Equal ( new List < string > { "text/html" , "application/json" } , result . MultiValueHeaders [ "Accept" ] ) ;
212
- Assert . Equal ( new List < string > { "session=abc123; theme=dark" } , result . MultiValueHeaders [ "Cookie" ] ) ;
213
- Assert . Equal ( new List < string > { "value1" } , result . MultiValueHeaders [ "X-Custom-Header" ] ) ;
209
+ var expectedCookieString = $ "session={ HttpUtility . UrlEncode ( "abc123" ) } ; theme={ HttpUtility . UrlEncode ( "dark" ) } ; complex={ HttpUtility . UrlEncode ( "this+has+special" ) } ";
210
+ Assert . Equal ( expectedCookieString , result . Headers [ "Cookie" ] ) ;
211
+ Assert . Equal ( [ expectedCookieString ] , result . MultiValueHeaders [ "Cookie" ] ) ;
214
212
215
213
Assert . Equal ( "pending" , result . QueryStringParameters [ "status" ] ) ;
216
214
Assert . Equal ( "urgent" , result . QueryStringParameters [ "tag" ] ) ;
0 commit comments