17
17
using Google . Cloud . Functions . Framework . GcfEvents ;
18
18
using Microsoft . AspNetCore . Http ;
19
19
using System ;
20
- using System . Collections . Generic ;
21
20
using System . IO ;
21
+ using System . Linq ;
22
22
using System . Text ;
23
23
using System . Text . Json ;
24
24
using System . Threading . Tasks ;
@@ -37,6 +37,8 @@ public class GcfConvertersTest
37
37
[ InlineData ( "firestore_simple.json" , "google.cloud.firestore.document.v1.written" , "//firestore.googleapis.com/projects/project-id/databases/(default)" , "documents/gcf-test/2Vm2mI1d0wIaK2Waj5to" ) ]
38
38
[ InlineData ( "pubsub_text.json" , "google.cloud.pubsub.topic.v1.messagePublished" , "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test" , null ) ]
39
39
[ InlineData ( "legacy_pubsub.json" , "google.cloud.pubsub.topic.v1.messagePublished" , "//pubsub.googleapis.com/projects/sample-project/topics/gcf-test" , null ) ]
40
+ [ InlineData ( "raw_pubsub.json" , "google.cloud.pubsub.topic.v1.messagePublished" , "//pubsub.googleapis.com/projects/unknown-project!/topics/unknown-topic!" , null ) ]
41
+ [ InlineData ( "emulator_pubsub.json" , "google.cloud.pubsub.topic.v1.messagePublished" , "//pubsub.googleapis.com/projects/unknown-project!/topics/unknown-topic!" , null ) ]
40
42
[ InlineData ( "firebase-db1.json" , "google.firebase.database.ref.v1.written" , "//firebasedatabase.googleapis.com/projects/_/locations/us-central1/instances/my-project-id" , "refs/gcf-test/xyz" ) ]
41
43
[ InlineData ( "firebase-db2.json" , "google.firebase.database.ref.v1.written" , "//firebasedatabase.googleapis.com/projects/_/locations/europe-west1/instances/my-project-id" , "refs/gcf-test/xyz" ) ]
42
44
[ InlineData ( "firebase-auth1.json" , "google.firebase.auth.user.v1.created" , "//firebaseauth.googleapis.com/projects/my-project-id" , "users/UUpby3s4spZre6kHsgVSPetzQ8l2" ) ]
@@ -109,6 +111,10 @@ public async Task MinimalValidEvent()
109
111
public Task InvalidRequest_UnableToDeserialize ( ) =>
110
112
AssertInvalidRequest ( "{INVALIDJSON 'data':{}, 'context':{'eventId':'xyz', 'eventType': 'google.pubsub.topic.publish', 'resource':{'service': 'svc', 'name': 'resname'}}}" ) ;
111
113
114
+ [ Fact ]
115
+ public Task InvalidRequest_UnknownType ( ) =>
116
+ AssertInvalidRequest ( "{'data':{}, 'context':{'eventId':'xyz', 'eventType': 'google.surprise', 'resource':{'service': 'svc', 'name': 'resname'}}}" ) ;
117
+
112
118
[ Fact ]
113
119
public Task InvalidRequest_NoData ( ) =>
114
120
AssertInvalidRequest ( "{'context':{'eventId':'xyz', 'eventType': 'google.pubsub.topic.publish', 'resource':{'service': 'svc', 'name': 'resname'}}}" ) ;
@@ -125,6 +131,35 @@ public Task InvalidRequest_NoType() =>
125
131
public Task InvalidRequest_NoResourceName ( ) =>
126
132
AssertInvalidRequest ( "{'data':{}, 'context':{'eventId':'xyz', 'eventType': 'google.pubsub.topic.publish', 'resource':{'service': 'svc'}}}" ) ;
127
133
134
+ // Minimal valid JSON for a raw Pub/Sub event, so all the subsequent invalid tests can be "this JSON with something removed" (or added)
135
+ [ Fact ]
136
+ public async Task MinimalValidEvent_RawPubSub ( )
137
+ {
138
+ string json = "{'message':{'messageId':'xyz'}, 'subscription':'projects/x/subscriptions/y'}" ;
139
+ var cloudEvent = await ConvertJson ( json ) ;
140
+ Assert . Equal ( "xyz" , cloudEvent . Id ) ;
141
+ }
142
+
143
+ [ Fact ]
144
+ public Task InvalidRequest_MissingMessageIdFromRawPubSub ( ) =>
145
+ AssertInvalidRequest ( "{'message':{}, 'subscription':'projects/x/subscriptions/y'}" ) ;
146
+
147
+ [ Fact ]
148
+ public Task InvalidRequest_OnlySubscriptionFromRawPubSub ( ) =>
149
+ AssertInvalidRequest ( "{'subscription':'projects/x/subscriptions/y'" ) ;
150
+
151
+ [ Fact ]
152
+ public Task InvalidRequest_OnlyMessageFromRawPubSub ( ) =>
153
+ AssertInvalidRequest ( "{'message':{'messageId':'1'}}" ) ;
154
+
155
+ [ Fact ]
156
+ public Task InvalidRequest_RawPubSubAndContext ( ) =>
157
+ AssertInvalidRequest ( "{'message':{'messageId':'xyz'}, 'subscription':'projects/x/subscriptions/y', 'context':{}}" ) ;
158
+
159
+ [ Fact ]
160
+ public Task InvalidRequest_RawPubSubAndData ( ) =>
161
+ AssertInvalidRequest ( "{'message':{'messageId':'xyz'}, 'subscription':'projects/x/subscriptions/y', 'data':{}}" ) ;
162
+
128
163
[ Theory ]
129
164
[ InlineData ( "firebase-analytics-no-app-id.json" ) ]
130
165
[ InlineData ( "firebase-analytics-no-event-name.json" ) ]
@@ -134,6 +169,37 @@ public async Task InvalidRequest_FirebaseAnalytics(string resourceName)
134
169
await Assert . ThrowsAsync < GcfConverters . ConversionException > ( ( ) => GcfConverters . ConvertGcfEventToCloudEvent ( context . Request , s_jsonFormatter ) ) ;
135
170
}
136
171
172
+ [ Theory ]
173
+ [ InlineData ( null , GcfConverters . DefaultRawPubSubTopic ) ]
174
+ [ InlineData ( "/not/a/matching/path" , GcfConverters . DefaultRawPubSubTopic ) ]
175
+ [ InlineData ( "/projects/abc/topics/bcd" , "projects/abc/topics/bcd" ) ]
176
+ public async Task RawPubSubTopic_NoPath ( string ? path , string expectedTopicResourceName )
177
+ {
178
+ var context = GcfEventResources . CreateHttpContext ( "raw_pubsub.json" , path ) ;
179
+ var cloudEvent = await GcfConverters . ConvertGcfEventToCloudEvent ( context . Request , s_jsonFormatter ) ;
180
+ Assert . Equal ( $ "//pubsub.googleapis.com/{ expectedTopicResourceName } ", cloudEvent . Source ? . ToString ( ) ) ;
181
+ string expectedTopic = expectedTopicResourceName . Split ( '/' ) . Last ( ) ;
182
+ Assert . Equal ( expectedTopic , ( string ) cloudEvent [ "topic" ] ! ) ;
183
+ }
184
+
185
+ [ Fact ]
186
+ public async Task RawPubSub_SimpleProperties ( )
187
+ {
188
+ var context = GcfEventResources . CreateHttpContext ( "raw_pubsub.json" ) ;
189
+ var cloudEvent = await GcfConverters . ConvertGcfEventToCloudEvent ( context . Request , s_jsonFormatter ) ;
190
+ Assert . Equal ( new DateTimeOffset ( 2022 , 2 , 15 , 11 , 28 , 32 , 942 , TimeSpan . Zero ) , cloudEvent . Time ) ;
191
+ Assert . Equal ( "4102184774039362" , cloudEvent . Id ) ;
192
+ }
193
+
194
+ [ Fact ]
195
+ public async Task EmulatorPubSub_SimpleProperties ( )
196
+ {
197
+ var context = GcfEventResources . CreateHttpContext ( "emulator_pubsub.json" ) ;
198
+ var cloudEvent = await GcfConverters . ConvertGcfEventToCloudEvent ( context . Request , s_jsonFormatter ) ;
199
+ Assert . Null ( cloudEvent . Time ) ;
200
+ Assert . Equal ( "1" , cloudEvent . Id ) ;
201
+ }
202
+
137
203
private static async Task AssertInvalidRequest ( string json , string ? contentType = null ) =>
138
204
await Assert . ThrowsAsync < GcfConverters . ConversionException > ( ( ) => ConvertJson ( json , contentType ) ) ;
139
205
0 commit comments