Skip to content

feat: add integration test #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions test/functions/hello/index.ts
Original file line number Diff line number Diff line change
@@ -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"}'

*/
41 changes: 41 additions & 0 deletions test/integration/functions_test.go
Original file line number Diff line number Diff line change
@@ -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)
}