Skip to content

Commit 231c769

Browse files
Merge pull request #3 from supabase-community/feat/add-integration-test
feat: add integration test
2 parents 9e87785 + e16ba0e commit 231c769

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

test/functions/hello/index.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Follow this setup guide to integrate the Deno language server with your editor:
2+
// https://deno.land/manual/getting_started/setup_your_environment
3+
// This enables autocomplete, go to definition, etc.
4+
5+
console.log("Hello from Functions!");
6+
7+
Deno.serve(async (req) => {
8+
const { name } = await req.json();
9+
const data = {
10+
message: `Hello ${name}!`,
11+
};
12+
13+
if (name === "") {
14+
return new Response(JSON.stringify({ error: "Body request is invalid" }), {
15+
status: 400,
16+
headers: { "Content-Type": "application/json" },
17+
});
18+
}
19+
if (name === "error") {
20+
return Error("An error occurred");
21+
}
22+
23+
return new Response(JSON.stringify(data), {
24+
headers: { "Content-Type": "application/json" },
25+
});
26+
});
27+
28+
/* To invoke locally:
29+
30+
1. Run `supabase start` (see: https://supabase.com/docs/reference/cli/supabase-start)
31+
2. Make an HTTP request:
32+
33+
curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/hello' \
34+
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \
35+
--header 'Content-Type: application/json' \
36+
--data '{"name":"Functions"}'
37+
38+
*/

test/integration/functions_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package functions_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/supabase-community/functions-go"
8+
)
9+
10+
const (
11+
rawUrl = "https://your-supabase-url.co/functions/v1"
12+
token = "supbase-service-key"
13+
)
14+
15+
// TestHello tests the normal function
16+
func TestHello(t *testing.T) {
17+
client := functions.NewClient(rawUrl, token, nil)
18+
type Body struct {
19+
Name string `json:"name"`
20+
}
21+
b := Body{Name: "world"}
22+
resp, err := client.Invoke("hello", b)
23+
if err != nil {
24+
t.Fatalf("Invoke failed: %s", err)
25+
}
26+
fmt.Println(resp)
27+
}
28+
29+
// TestErrorHandling tests the error handling of the functions client
30+
func TestErrorHandling(t *testing.T) {
31+
client := functions.NewClient(rawUrl, token, map[string]string{"custom-header": "custom-header"})
32+
type Body struct {
33+
Name string `json:"name"`
34+
}
35+
b := Body{Name: "error"}
36+
resp, err := client.Invoke("hello", b)
37+
if err != nil {
38+
t.Fatalf("Invoke failed: %s", err)
39+
}
40+
fmt.Println(resp)
41+
}

0 commit comments

Comments
 (0)