@@ -18,6 +18,7 @@ import (
1818 "github.com/stretchr/testify/assert"
1919
2020 ofctx "github.com/OpenFunction/functions-framework-go/context"
21+ "github.com/OpenFunction/functions-framework-go/functions"
2122 "github.com/OpenFunction/functions-framework-go/runtime/async"
2223)
2324
@@ -166,6 +167,157 @@ func TestCloudEventFunction(t *testing.T) {
166167 }
167168}
168169
170+ func TestMultipleFunctions (t * testing.T ) {
171+ env := `{
172+ "name": "function-demo",
173+ "version": "v1.0.0",
174+ "port": "8080",
175+ "runtime": "Knative",
176+ "httpPattern": "/"
177+ }`
178+ var ceDemo = struct {
179+ message map [string ]string
180+ headers map [string ]string
181+ }{
182+ message : map [string ]string {
183+ "msg" : "Hello World!" ,
184+ },
185+ headers : map [string ]string {
186+ "Ce-Specversion" : "1.0" ,
187+ "Ce-Type" : "cloudevents.openfunction.samples.helloworld" ,
188+ "Ce-Source" : "cloudevents.openfunction.samples/helloworldsource" ,
189+ "Ce-Id" : "536808d3-88be-4077-9d7a-a3f162705f79" ,
190+ },
191+ }
192+
193+ ctx := context .Background ()
194+ fwk , err := createFramework (env )
195+ if err != nil {
196+ t .Fatalf ("failed to create framework: %v" , err )
197+ }
198+
199+ fwk .RegisterPlugins (nil )
200+
201+ // register multiple functions
202+ functions .HTTP ("http" , fakeHTTPFunction ,
203+ functions .WithFunctionPath ("/http" ),
204+ functions .WithFunctionMethods ("GET" ),
205+ )
206+
207+ functions .CloudEvent ("ce" , fakeCloudEventsFunction ,
208+ functions .WithFunctionPath ("/ce" ),
209+ )
210+
211+ functions .OpenFunction ("ofn" , fakeBindingsFunction ,
212+ functions .WithFunctionPath ("/ofn" ),
213+ functions .WithFunctionMethods ("GET" , "POST" ),
214+ )
215+
216+ if err := fwk .TryRegisterFunctions (ctx ); err != nil {
217+ t .Fatalf ("failed to start registering functions: %v" , err )
218+ }
219+
220+ if fwk .GetRuntime () == nil {
221+ t .Fatal ("failed to create runtime" )
222+ }
223+ handler := fwk .GetRuntime ().GetHandler ()
224+ if handler == nil {
225+ t .Fatal ("handler is nil" )
226+ }
227+
228+ srv := httptest .NewServer (handler .(http.Handler ))
229+ defer srv .Close ()
230+
231+ // test http
232+ t .Run ("sending http" , func (t * testing.T ) {
233+ resp , err := http .Get (srv .URL + "/http" )
234+ if err != nil {
235+ t .Fatalf ("http.Get: %v" , err )
236+ }
237+
238+ defer resp .Body .Close ()
239+ body , err := ioutil .ReadAll (resp .Body )
240+ if err != nil {
241+ t .Fatalf ("ioutil.ReadAll: %v" , err )
242+ }
243+
244+ if got , want := string (body ), "Hello World!" ; got != want {
245+ t .Fatalf ("TestHTTPFunction: got %v; want %v" , got , want )
246+ }
247+ })
248+
249+ // test http to openfunction
250+ t .Run ("sending http to openfunction" , func (t * testing.T ) {
251+ resp , err := http .Get (srv .URL + "/ofn" )
252+ if err != nil {
253+ t .Fatalf ("http.Get: %v" , err )
254+ }
255+
256+ defer resp .Body .Close ()
257+ body , err := ioutil .ReadAll (resp .Body )
258+ if err != nil {
259+ t .Fatalf ("ioutil.ReadAll: %v" , err )
260+ }
261+
262+ if got , want := string (body ), "hello there" ; got != want {
263+ t .Fatalf ("TestHTTPFunction: got %v; want %v" , got , want )
264+ }
265+ })
266+
267+ // test cloudevent
268+ t .Run ("sending cloudevent" , func (t * testing.T ) {
269+ messageByte , err := json .Marshal (ceDemo .message )
270+ if err != nil {
271+ t .Fatalf ("failed to marshal message: %v" , err )
272+ }
273+
274+ req , err := http .NewRequest ("POST" , srv .URL + "/ce" , bytes .NewBuffer (messageByte ))
275+ if err != nil {
276+ t .Fatalf ("error creating HTTP request for test: %v" , err )
277+ }
278+ req .Header .Set ("Content-Type" , "application/json" )
279+ for k , v := range ceDemo .headers {
280+ req .Header .Set (k , v )
281+ }
282+ client := & http.Client {}
283+ resp , err := client .Do (req )
284+ if err != nil {
285+ t .Fatalf ("failed to do client.Do: %v" , err )
286+ }
287+
288+ if resp .StatusCode != http .StatusOK {
289+ t .Fatalf ("failed to test cloudevents function: response status = %v, want %v" , resp .StatusCode , http .StatusOK )
290+ }
291+ })
292+
293+ // test cloudevent to openfunction
294+ t .Run ("sending cloudevent to openfunction" , func (t * testing.T ) {
295+ messageByte , err := json .Marshal (ceDemo .message )
296+ if err != nil {
297+ t .Fatalf ("failed to marshal message: %v" , err )
298+ }
299+
300+ req , err := http .NewRequest ("POST" , srv .URL + "/ofn" , bytes .NewBuffer (messageByte ))
301+ if err != nil {
302+ t .Fatalf ("error creating HTTP request for test: %v" , err )
303+ }
304+ req .Header .Set ("Content-Type" , "application/json" )
305+ for k , v := range ceDemo .headers {
306+ req .Header .Set (k , v )
307+ }
308+ client := & http.Client {}
309+ resp , err := client .Do (req )
310+ if err != nil {
311+ t .Fatalf ("failed to do client.Do: %v" , err )
312+ }
313+
314+ if resp .StatusCode != http .StatusOK {
315+ t .Fatalf ("failed to test cloudevents function: response status = %v, want %v" , resp .StatusCode , http .StatusOK )
316+ }
317+ })
318+
319+ }
320+
169321func TestAsyncBindingsFunction (t * testing.T ) {
170322 env := `{
171323 "name": "function-demo",
0 commit comments