5
5
using System . Collections . Generic ;
6
6
using System . IO ;
7
7
using System . Linq ;
8
- using System . Reflection ;
9
8
using System . Security . Claims ;
10
9
using System . Text ;
11
10
using Google . Protobuf ;
@@ -26,18 +25,112 @@ public class RpcMessageConversionExtensionsTests
26
25
private static readonly string TestImageLocation = "Rpc\\ Resources\\ functions.png" ;
27
26
28
27
[ Theory ]
29
- [ InlineData ( "application/x-www-form-urlencoded’" , "say=Hi&to=Mom" ) ]
30
- public void HttpObjects_StringBody ( string expectedContentType , object body )
28
+ [ InlineData ( "application/x-www-form-urlencoded’" , "say=Hi&to=Mom" , true ) ]
29
+ [ InlineData ( "application/x-www-form-urlencoded’" , "say=Hi&to=Mom" , false ) ]
30
+ public void HttpObjects_StringBody ( string expectedContentType , object body , bool rcpHttpBodyOnly )
31
31
{
32
32
var logger = MockNullLoggerFactory . CreateLogger ( ) ;
33
33
var capabilities = new Capabilities ( logger ) ;
34
+ if ( rcpHttpBodyOnly )
35
+ {
36
+ capabilities . UpdateCapabilities ( new MapField < string , string >
37
+ {
38
+ { LanguageWorkerConstants . RpcHttpBodyOnly , rcpHttpBodyOnly . ToString ( ) }
39
+ } ) ;
40
+ }
34
41
35
42
var headers = new HeaderDictionary ( ) ;
36
43
headers . Add ( "content-type" , expectedContentType ) ;
37
44
HttpRequest request = HttpTestHelpers . CreateHttpRequest ( "GET" , "http://localhost/api/httptrigger-scenarios" , headers , body ) ;
38
45
39
46
var rpcRequestObject = request . ToRpc ( logger , capabilities ) ;
40
47
Assert . Equal ( body . ToString ( ) , rpcRequestObject . Http . Body . String ) ;
48
+ if ( rcpHttpBodyOnly )
49
+ {
50
+ Assert . Equal ( null , rpcRequestObject . Http . RawBody ) ;
51
+ Assert . Equal ( body . ToString ( ) , rpcRequestObject . Http . Body . String ) ;
52
+ }
53
+ else
54
+ {
55
+ Assert . Equal ( body . ToString ( ) , rpcRequestObject . Http . RawBody . String ) ;
56
+ Assert . Equal ( body . ToString ( ) , rpcRequestObject . Http . Body . String ) ;
57
+ }
58
+
59
+ string contentType ;
60
+ rpcRequestObject . Http . Headers . TryGetValue ( "content-type" , out contentType ) ;
61
+ Assert . Equal ( expectedContentType , contentType ) ;
62
+ }
63
+
64
+ [ Theory ]
65
+ [ InlineData ( "application/json" , "{\" name\" :\" John\" }" , true ) ]
66
+ [ InlineData ( "application/json" , "{\" name\" :\" John\" }" , false ) ]
67
+ public void HttpObjects_JsonBody ( string expectedContentType , string body , bool rcpHttpBodyOnly )
68
+ {
69
+ var logger = MockNullLoggerFactory . CreateLogger ( ) ;
70
+ var capabilities = new Capabilities ( logger ) ;
71
+ if ( rcpHttpBodyOnly )
72
+ {
73
+ capabilities . UpdateCapabilities ( new MapField < string , string >
74
+ {
75
+ { LanguageWorkerConstants . RpcHttpBodyOnly , rcpHttpBodyOnly . ToString ( ) }
76
+ } ) ;
77
+ }
78
+
79
+ var headers = new HeaderDictionary ( ) ;
80
+ headers . Add ( "content-type" , expectedContentType ) ;
81
+ HttpRequest request = HttpTestHelpers . CreateHttpRequest ( "GET" , "http://localhost/api/httptrigger-scenarios" , headers , body ) ;
82
+
83
+ var rpcRequestObject = request . ToRpc ( logger , capabilities ) ;
84
+ if ( rcpHttpBodyOnly )
85
+ {
86
+ Assert . Equal ( null , rpcRequestObject . Http . RawBody ) ;
87
+ Assert . Equal ( body . ToString ( ) , rpcRequestObject . Http . Body . String ) ;
88
+ }
89
+ else
90
+ {
91
+ Assert . Equal ( body . ToString ( ) , rpcRequestObject . Http . RawBody . String ) ;
92
+ Assert . Equal ( JsonConvert . DeserializeObject ( body ) , JsonConvert . DeserializeObject ( rpcRequestObject . Http . Body . Json ) ) ;
93
+ }
94
+
95
+ string contentType ;
96
+ rpcRequestObject . Http . Headers . TryGetValue ( "content-type" , out contentType ) ;
97
+ Assert . Equal ( expectedContentType , contentType ) ;
98
+ }
99
+
100
+ [ Theory ]
101
+ [ InlineData ( "application/octet-stream" , true ) ]
102
+ [ InlineData ( "application/octet-stream" , false ) ]
103
+ [ InlineData ( "multipart/form-data; boundary=----WebKitFormBoundaryTYtz7wze2XXrH26B" , true ) ]
104
+ [ InlineData ( "multipart/form-data; boundary=----WebKitFormBoundaryTYtz7wze2XXrH26B" , false ) ]
105
+ public void HttpTrigger_Post_ByteArray ( string expectedContentType , bool rcpHttpBodyOnly )
106
+ {
107
+ var logger = MockNullLoggerFactory . CreateLogger ( ) ;
108
+ var capabilities = new Capabilities ( logger ) ;
109
+ if ( rcpHttpBodyOnly )
110
+ {
111
+ capabilities . UpdateCapabilities ( new MapField < string , string >
112
+ {
113
+ { LanguageWorkerConstants . RpcHttpBodyOnly , rcpHttpBodyOnly . ToString ( ) }
114
+ } ) ;
115
+ }
116
+
117
+ var headers = new HeaderDictionary ( ) ;
118
+ headers . Add ( "content-type" , expectedContentType ) ;
119
+ byte [ ] body = new byte [ ] { 1 , 2 , 3 , 4 , 5 } ;
120
+
121
+ HttpRequest request = HttpTestHelpers . CreateHttpRequest ( "POST" , "http://localhost/api/httptrigger-scenarios" , headers , body ) ;
122
+
123
+ var rpcRequestObject = request . ToRpc ( logger , capabilities ) ;
124
+ if ( rcpHttpBodyOnly )
125
+ {
126
+ Assert . Equal ( null , rpcRequestObject . Http . RawBody ) ;
127
+ Assert . Equal ( body , rpcRequestObject . Http . Body . Bytes ) ;
128
+ }
129
+ else
130
+ {
131
+ Assert . Equal ( body , rpcRequestObject . Http . Body . Bytes ) ;
132
+ Assert . Equal ( Encoding . UTF8 . GetString ( body ) , rpcRequestObject . Http . RawBody . String ) ;
133
+ }
41
134
42
135
string contentType ;
43
136
rpcRequestObject . Http . Headers . TryGetValue ( "content-type" , out contentType ) ;
0 commit comments