3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . IO ;
6
7
using System . Linq ;
8
+ using System . Reflection ;
7
9
using System . Security . Claims ;
10
+ using System . Text ;
8
11
using Google . Protobuf . Collections ;
9
12
using Google . Protobuf . WellKnownTypes ;
10
13
using Microsoft . AspNetCore . Http ;
@@ -18,17 +21,20 @@ namespace Microsoft.Azure.WebJobs.Script.Tests.Rpc
18
21
{
19
22
public class RpcMessageConversionExtensionsTests
20
23
{
24
+ private static readonly string TestImageLocation = "Rpc\\ Resources\\ functions.png" ;
25
+
21
26
[ Theory ]
22
27
[ InlineData ( "application/x-www-form-urlencoded’" , "say=Hi&to=Mom" ) ]
23
28
public void HttpObjects_StringBody ( string expectedContentType , object body )
24
29
{
25
30
var logger = MockNullLoggerFactory . CreateLogger ( ) ;
31
+ var capabilities = new Capabilities ( logger ) ;
26
32
27
33
var headers = new HeaderDictionary ( ) ;
28
34
headers . Add ( "content-type" , expectedContentType ) ;
29
35
HttpRequest request = HttpTestHelpers . CreateHttpRequest ( "GET" , "http://localhost/api/httptrigger-scenarios" , headers , body ) ;
30
36
31
- var rpcRequestObject = request . ToRpc ( logger ) ;
37
+ var rpcRequestObject = request . ToRpc ( logger , capabilities ) ;
32
38
Assert . Equal ( body . ToString ( ) , rpcRequestObject . Http . Body . String ) ;
33
39
34
40
string contentType ;
@@ -43,10 +49,11 @@ public void HttpObjects_StringBody(string expectedContentType, object body)
43
49
public void HttpObjects_Query ( string queryString , string [ ] expectedKeys , string [ ] expectedValues )
44
50
{
45
51
var logger = MockNullLoggerFactory . CreateLogger ( ) ;
52
+ var capabilities = new Capabilities ( logger ) ;
46
53
47
54
HttpRequest request = HttpTestHelpers . CreateHttpRequest ( "GET" , $ "http://localhost/api/httptrigger-scenarios?{ queryString } ") ;
48
55
49
- var rpcRequestObject = request . ToRpc ( logger ) ;
56
+ var rpcRequestObject = request . ToRpc ( logger , capabilities ) ;
50
57
// Same number of expected key value pairs
51
58
Assert . Equal ( rpcRequestObject . Http . Query . Count , expectedKeys . Length ) ;
52
59
Assert . Equal ( rpcRequestObject . Http . Query . Count , expectedValues . Length ) ;
@@ -188,6 +195,7 @@ public void SetCookie_ReturnsExpectedResult(string name, string value, RpcHttpCo
188
195
public void HttpObjects_ClaimsPrincipal ( )
189
196
{
190
197
var logger = MockNullLoggerFactory . CreateLogger ( ) ;
198
+ var capabilities = new Capabilities ( logger ) ;
191
199
192
200
HttpRequest request = HttpTestHelpers . CreateHttpRequest ( "GET" , $ "http://localhost/apihttptrigger-scenarios") ;
193
201
@@ -201,7 +209,7 @@ public void HttpObjects_ClaimsPrincipal()
201
209
202
210
request . HttpContext . User = new ClaimsPrincipal ( claimsIdentities ) ;
203
211
204
- var rpcRequestObject = request . ToRpc ( logger ) ;
212
+ var rpcRequestObject = request . ToRpc ( logger , capabilities ) ;
205
213
206
214
var identities = request . HttpContext . User . Identities . ToList ( ) ;
207
215
var rpcIdentities = rpcRequestObject . Http . Identities . ToList ( ) ;
@@ -247,5 +255,55 @@ internal static ClaimsIdentity MockEasyAuth(string provider, string name, string
247
255
248
256
return identity ;
249
257
}
258
+
259
+ [ Theory ]
260
+ [ InlineData ( "application/octet-stream" , "true" ) ]
261
+ [ InlineData ( "image/png" , "true" ) ]
262
+ [ InlineData ( "application/octet-stream" , null ) ]
263
+ [ InlineData ( "image/png" , null ) ]
264
+ public void HttpObjects_RawBodyBytes_Image_Length ( string contentType , string rawBytesEnabled )
265
+ {
266
+ var logger = MockNullLoggerFactory . CreateLogger ( ) ;
267
+ var capabilities = new Capabilities ( logger ) ;
268
+ if ( ! string . Equals ( rawBytesEnabled , null ) )
269
+ {
270
+ capabilities . UpdateCapabilities ( new MapField < string , string >
271
+ {
272
+ { LanguageWorkerConstants . RawHttpBodyBytes , rawBytesEnabled . ToString ( ) }
273
+ } ) ;
274
+ }
275
+
276
+ FileStream image = new FileStream ( TestImageLocation , FileMode . Open , FileAccess . Read ) ;
277
+ byte [ ] imageBytes = FileStreamToBytes ( image ) ;
278
+ string imageString = Encoding . UTF8 . GetString ( imageBytes ) ;
279
+
280
+ long imageBytesLength = imageBytes . Length ;
281
+ long imageStringLength = imageString . Length ;
282
+
283
+ var headers = new HeaderDictionary ( ) ;
284
+ headers . Add ( "content-type" , contentType ) ;
285
+ HttpRequest request = HttpTestHelpers . CreateHttpRequest ( "GET" , "http://localhost/api/httptrigger-scenarios" , headers , imageBytes ) ;
286
+
287
+ var rpcRequestObject = request . ToRpc ( logger , capabilities ) ;
288
+ if ( string . IsNullOrEmpty ( rawBytesEnabled ) )
289
+ {
290
+ Assert . Empty ( rpcRequestObject . Http . RawBody . Bytes ) ;
291
+ Assert . Equal ( imageStringLength , rpcRequestObject . Http . RawBody . String . Length ) ;
292
+ }
293
+ else
294
+ {
295
+ Assert . Empty ( rpcRequestObject . Http . RawBody . String ) ;
296
+ Assert . Equal ( imageBytesLength , rpcRequestObject . Http . RawBody . Bytes . ToByteArray ( ) . Length ) ;
297
+ }
298
+ }
299
+
300
+ private byte [ ] FileStreamToBytes ( FileStream file )
301
+ {
302
+ using ( var memoryStream = new MemoryStream ( ) )
303
+ {
304
+ file . CopyTo ( memoryStream ) ;
305
+ return memoryStream . ToArray ( ) ;
306
+ }
307
+ }
250
308
}
251
309
}
0 commit comments