diff --git a/test/functions/hello/index.ts b/test/functions/hello/index.ts new file mode 100644 index 0000000..68e5c0f --- /dev/null +++ b/test/functions/hello/index.ts @@ -0,0 +1,38 @@ +// Follow this setup guide to integrate the Deno language server with your editor: +// https://deno.land/manual/getting_started/setup_your_environment +// This enables autocomplete, go to definition, etc. + +console.log("Hello from Functions!"); + +Deno.serve(async (req) => { + const { name } = await req.json(); + const data = { + message: `Hello ${name}!`, + }; + + if (name === "") { + return new Response(JSON.stringify({ error: "Body request is invalid" }), { + status: 400, + headers: { "Content-Type": "application/json" }, + }); + } + if (name === "error") { + return Error("An error occurred"); + } + + return new Response(JSON.stringify(data), { + headers: { "Content-Type": "application/json" }, + }); +}); + +/* To invoke locally: + + 1. Run `supabase start` (see: https://supabase.com/docs/reference/cli/supabase-start) + 2. Make an HTTP request: + + curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/hello' \ + --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \ + --header 'Content-Type: application/json' \ + --data '{"name":"Functions"}' + +*/ diff --git a/test/integration/functions_test.go b/test/integration/functions_test.go new file mode 100644 index 0000000..c0d85db --- /dev/null +++ b/test/integration/functions_test.go @@ -0,0 +1,41 @@ +package functions_test + +import ( + "fmt" + "testing" + + "github.com/supabase-community/functions-go" +) + +const ( + rawUrl = "https://your-supabase-url.co/functions/v1" + token = "supbase-service-key" +) + +// TestHello tests the normal function +func TestHello(t *testing.T) { + client := functions.NewClient(rawUrl, token, nil) + type Body struct { + Name string `json:"name"` + } + b := Body{Name: "world"} + resp, err := client.Invoke("hello", b) + if err != nil { + t.Fatalf("Invoke failed: %s", err) + } + fmt.Println(resp) +} + +// TestErrorHandling tests the error handling of the functions client +func TestErrorHandling(t *testing.T) { + client := functions.NewClient(rawUrl, token, map[string]string{"custom-header": "custom-header"}) + type Body struct { + Name string `json:"name"` + } + b := Body{Name: "error"} + resp, err := client.Invoke("hello", b) + if err != nil { + t.Fatalf("Invoke failed: %s", err) + } + fmt.Println(resp) +}