Edge Functions - inconsistent results when running .invoke locally using supabase serve
I have this simple edge function, im trying to test requests that are not POST. I keep getting mixed responses. Please help
message: Method not allowed
status: 405
headers: POST
or
message: undefined
status: 502
headers: null
Edge Function
Test
message: Method not allowed
status: 405
headers: POST
or
message: undefined
status: 502
headers: null
Edge Function
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}
export function createHandler(tasks = originalTasks) {
return async function (req: Request) {
if (req.method !== 'POST') {
console.log(`Method not allowed: ${req.method}`);
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
headers: { ...corsHeaders, 'Allow': 'POST' },
status: 405,
});
}
return new Response(JSON.stringify('hello'), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 200,
});
};
}const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}
export function createHandler(tasks = originalTasks) {
return async function (req: Request) {
if (req.method !== 'POST') {
console.log(`Method not allowed: ${req.method}`);
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
headers: { ...corsHeaders, 'Allow': 'POST' },
status: 405,
});
}
return new Response(JSON.stringify('hello'), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 200,
});
};
}Test
Deno.test('[Unit Test] Method Not POST', {sanitizeOps:false, sanitizeResources:false}, async () => {
// Switch to anon client
const anonClient = createClient<Database>(supabaseUrl, anonKey, options);
// Invoke the 'order' function without the first name
const { data, error } = await anonClient.functions.invoke('update-system-settings-handler', {
method: 'PUT',
headers: { Authorization: `Bearer ${anonKey}`},
body: { }
});
// log the error message
const errorMessage = (await error.context.json()).error;
console.log(`message: ${errorMessage}`);
console.log(`status: ${await error.context.status}`);
console.log(`headers: ${await error.context.headers.get('Allow')}`);
// Check for errors from the function invocation
if (data || !error || errorMessage != 'Method not allowed' || error.context.status != 405 || error.context.headers.get('Allow') != 'POST') {
assert(false);
}
});Deno.test('[Unit Test] Method Not POST', {sanitizeOps:false, sanitizeResources:false}, async () => {
// Switch to anon client
const anonClient = createClient<Database>(supabaseUrl, anonKey, options);
// Invoke the 'order' function without the first name
const { data, error } = await anonClient.functions.invoke('update-system-settings-handler', {
method: 'PUT',
headers: { Authorization: `Bearer ${anonKey}`},
body: { }
});
// log the error message
const errorMessage = (await error.context.json()).error;
console.log(`message: ${errorMessage}`);
console.log(`status: ${await error.context.status}`);
console.log(`headers: ${await error.context.headers.get('Allow')}`);
// Check for errors from the function invocation
if (data || !error || errorMessage != 'Method not allowed' || error.context.status != 405 || error.context.headers.get('Allow') != 'POST') {
assert(false);
}
});