5
5
import java .lang .reflect .Method ;
6
6
import java .lang .reflect .Parameter ;
7
7
import java .util .*;
8
+
9
+ import com .microsoft .azure .functions .rpc .messages .NullableTypes .NullableString ;
8
10
import com .microsoft .azure .functions .HttpRequestMessage ;
9
11
import com .microsoft .azure .functions .HttpStatus ;
10
12
import com .microsoft .azure .functions .rpc .messages .RpcHttp ;
@@ -26,13 +28,23 @@ public void HttpRequestIntBody(HttpRequestMessage<Integer> request) {
26
28
public void HttpRequestBinaryBody (HttpRequestMessage <byte []> request ) {
27
29
}
28
30
29
- public static RpcHttp getTestRpcHttp (Object inputBody ) throws Exception {
31
+ public static RpcHttp getTestRpcHttp (
32
+ Object inputBody ,
33
+ Map <String , String > headersMap ,
34
+ Map <String , String > queryMap ) throws Exception {
30
35
TypedData .Builder dataBuilder = TypedData .newBuilder ();
31
36
RpcHttp .Builder httpBuilder = RpcHttp .newBuilder ()
32
37
.setStatusCode (Integer .toString (HttpStatus .OK .value ()));
33
- Map <String , String > headers = new HashMap <>();
34
- headers .put ("header" , "testHeader" );
35
- headers .forEach (httpBuilder ::putHeaders );
38
+ if (headersMap != null ) {
39
+ for (String key : headersMap .keySet ()) {
40
+ httpBuilder .putNullableHeaders (key , NullableString .newBuilder ().setValue (headersMap .get (key )).build ());
41
+ }
42
+ }
43
+ if (queryMap != null ) {
44
+ for (String key : queryMap .keySet ()) {
45
+ httpBuilder .putNullableQuery (key , NullableString .newBuilder ().setValue (queryMap .get (key )).build ());
46
+ }
47
+ }
36
48
RpcUnspecifiedDataTarget bodyTarget = new RpcUnspecifiedDataTarget ();
37
49
Object body = inputBody ;
38
50
bodyTarget .setValue (body );
@@ -41,14 +53,86 @@ public static RpcHttp getTestRpcHttp(Object inputBody) throws Exception {
41
53
return httpBuilder .build ();
42
54
}
43
55
56
+ @ Test
57
+ public void rpcHttpDataSource_To_HttpRequestMessage_NullableQueryParamsEmpty () throws Exception {
58
+
59
+ Method httpRequestMessageStringBodyMethod = getFunctionMethod ("HttpRequestStringBody" );
60
+ Map <String , String > queryMap = new HashMap <String , String >() {{
61
+ put ("name" , "" );
62
+ }};
63
+ Parameter [] parameters = httpRequestMessageStringBodyMethod .getParameters ();
64
+ String sourceKey = "testRpcHttp" ;
65
+ RpcHttp input = getTestRpcHttp (null , null , queryMap );
66
+ RpcHttpRequestDataSource rpcHttp = new RpcHttpRequestDataSource (sourceKey , input );
67
+ Optional <BindingData > actualBindingData = rpcHttp .computeByName (sourceKey ,
68
+ parameters [0 ].getParameterizedType ());
69
+ BindingData actualArg = actualBindingData .orElseThrow (WrongMethodTypeException ::new );
70
+ HttpRequestMessage <?> requestMsg = (HttpRequestMessage <?>) actualArg .getValue ();
71
+ assertEquals (requestMsg .getQueryParameters ().get ("name" ), "" );
72
+ }
73
+
74
+ @ Test
75
+ public void rpcHttpDataSource_To_HttpRequestMessage_NullableQueryParamsNonEmpty () throws Exception {
76
+
77
+ Method httpRequestMessageStringBodyMethod = getFunctionMethod ("HttpRequestStringBody" );
78
+ Map <String , String > queryMap = new HashMap <String , String >() {{
79
+ put ("name" , "random" );
80
+ }};
81
+ Parameter [] parameters = httpRequestMessageStringBodyMethod .getParameters ();
82
+ String sourceKey = "testRpcHttp" ;
83
+ RpcHttp input = getTestRpcHttp (null , null , queryMap );
84
+ RpcHttpRequestDataSource rpcHttp = new RpcHttpRequestDataSource (sourceKey , input );
85
+ Optional <BindingData > actualBindingData = rpcHttp .computeByName (sourceKey ,
86
+ parameters [0 ].getParameterizedType ());
87
+ BindingData actualArg = actualBindingData .orElseThrow (WrongMethodTypeException ::new );
88
+ HttpRequestMessage <?> requestMsg = (HttpRequestMessage <?>) actualArg .getValue ();
89
+ assertEquals (requestMsg .getQueryParameters ().get ("name" ), "random" );
90
+ }
91
+
92
+ @ Test
93
+ public void rpcHttpDataSource_To_HttpRequestMessage_NullableHeadersEmpty () throws Exception {
94
+
95
+ Method httpRequestMessageStringBodyMethod = getFunctionMethod ("HttpRequestStringBody" );
96
+ Map <String , String > headersMap = new HashMap <String , String >() {{
97
+ put ("cookie" , "" );
98
+ }};
99
+ Parameter [] parameters = httpRequestMessageStringBodyMethod .getParameters ();
100
+ String sourceKey = "testRpcHttp" ;
101
+ RpcHttp input = getTestRpcHttp (null , headersMap , null );
102
+ RpcHttpRequestDataSource rpcHttp = new RpcHttpRequestDataSource (sourceKey , input );
103
+ Optional <BindingData > actualBindingData = rpcHttp .computeByName (sourceKey ,
104
+ parameters [0 ].getParameterizedType ());
105
+ BindingData actualArg = actualBindingData .orElseThrow (WrongMethodTypeException ::new );
106
+ HttpRequestMessage <?> requestMsg = (HttpRequestMessage <?>) actualArg .getValue ();
107
+ assertEquals (requestMsg .getHeaders ().get ("cookie" ), "" );
108
+ }
109
+
110
+ @ Test
111
+ public void rpcHttpDataSource_To_HttpRequestMessage_NullableHeadersNonEmpty () throws Exception {
112
+
113
+ Method httpRequestMessageStringBodyMethod = getFunctionMethod ("HttpRequestStringBody" );
114
+ Map <String , String > headersMap = new HashMap <String , String >() {{
115
+ put ("cookie" , "foo=bar" );
116
+ }};
117
+ Parameter [] parameters = httpRequestMessageStringBodyMethod .getParameters ();
118
+ String sourceKey = "testRpcHttp" ;
119
+ RpcHttp input = getTestRpcHttp (null , headersMap , null );
120
+ RpcHttpRequestDataSource rpcHttp = new RpcHttpRequestDataSource (sourceKey , input );
121
+ Optional <BindingData > actualBindingData = rpcHttp .computeByName (sourceKey ,
122
+ parameters [0 ].getParameterizedType ());
123
+ BindingData actualArg = actualBindingData .orElseThrow (WrongMethodTypeException ::new );
124
+ HttpRequestMessage <?> requestMsg = (HttpRequestMessage <?>) actualArg .getValue ();
125
+ assertEquals (requestMsg .getHeaders ().get ("cookie" ), "foo=bar" );
126
+ }
127
+
44
128
@ Test
45
129
public void rpcHttpDataSource_To_HttpRequestMessage_StringBody () throws Exception {
46
130
47
131
Method httpRequestMessageStringBodyMethod = getFunctionMethod ("HttpRequestStringBody" );
48
132
49
133
Parameter [] parameters = httpRequestMessageStringBodyMethod .getParameters ();
50
134
String sourceKey = "testRpcHttp" ;
51
- RpcHttp input = getTestRpcHttp ("testStringBody" );
135
+ RpcHttp input = getTestRpcHttp ("testStringBody" , null , null );
52
136
RpcHttpRequestDataSource rpcHttp = new RpcHttpRequestDataSource (sourceKey , input );
53
137
Optional <BindingData > actualBindingData = rpcHttp .computeByName (sourceKey ,
54
138
parameters [0 ].getParameterizedType ());
@@ -64,7 +148,7 @@ public void rpcHttpDataSource_To_HttpRequestMessage_IntegerBody() throws Excepti
64
148
65
149
Parameter [] parameters = httpRequestMessageStringBodyMethod .getParameters ();
66
150
String sourceKey = "testRpcHttp" ;
67
- RpcHttp input = getTestRpcHttp (1234 );
151
+ RpcHttp input = getTestRpcHttp (1234 , null , null );
68
152
RpcHttpRequestDataSource rpcHttp = new RpcHttpRequestDataSource (sourceKey , input );
69
153
Optional <BindingData > actualBindingData = rpcHttp .computeByName (sourceKey ,
70
154
parameters [0 ].getParameterizedType ());
@@ -82,7 +166,7 @@ public void rpcHttpDataSource_To_HttpRequestMessage_byteArrayBody() throws Excep
82
166
String sourceKey = "testRpcHttp" ;
83
167
String expectedString = "Example String" ;
84
168
byte [] inputBytes = expectedString .getBytes ();
85
- RpcHttp input = getTestRpcHttp (inputBytes );
169
+ RpcHttp input = getTestRpcHttp (inputBytes , null , null );
86
170
RpcHttpRequestDataSource rpcHttp = new RpcHttpRequestDataSource (sourceKey , input );
87
171
Optional <BindingData > actualBindingData = rpcHttp .computeByName (sourceKey ,
88
172
parameters [0 ].getParameterizedType ());
0 commit comments