From 703c55bf786022bc6a5e882318d0706f4c1eea3a Mon Sep 17 00:00:00 2001 From: PedroHenrique0713 Date: Sat, 11 Jul 2026 21:50:02 -0300 Subject: [PATCH] fix(functions): match response Content-Type case-insensitively --- .../core/functions-js/src/FunctionsClient.ts | 7 +++- .../test/spec/response-content-type.spec.ts | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 packages/core/functions-js/test/spec/response-content-type.spec.ts diff --git a/packages/core/functions-js/src/FunctionsClient.ts b/packages/core/functions-js/src/FunctionsClient.ts index ce825be4c8e..157df372caf 100644 --- a/packages/core/functions-js/src/FunctionsClient.ts +++ b/packages/core/functions-js/src/FunctionsClient.ts @@ -298,7 +298,12 @@ export class FunctionsClient { throw new FunctionsHttpError(response) } - let responseType = (response.headers.get('Content-Type') ?? 'text/plain').split(';')[0].trim() + // HTTP media types are case-insensitive (RFC 9110), so normalize before + // matching — otherwise an "Application/JSON" response falls through to text. + let responseType = (response.headers.get('Content-Type') ?? 'text/plain') + .split(';')[0] + .trim() + .toLowerCase() let data: any if (responseType === 'application/json') { data = await response.json() diff --git a/packages/core/functions-js/test/spec/response-content-type.spec.ts b/packages/core/functions-js/test/spec/response-content-type.spec.ts new file mode 100644 index 00000000000..2be399de5b7 --- /dev/null +++ b/packages/core/functions-js/test/spec/response-content-type.spec.ts @@ -0,0 +1,42 @@ +import 'jest' + +import { FunctionsClient } from '../../src/index' + +/** + * Unit tests (no relay) for response body parsing based on the response + * Content-Type header. HTTP media types are case-insensitive (RFC 9110), so the + * client must match them regardless of casing. + */ +describe('response Content-Type parsing', () => { + const makeClient = (contentType: string, body: string) => { + const customFetch = jest.fn( + async () => + new Response(body, { + status: 200, + headers: { 'Content-Type': contentType }, + }) + ) as any + return new FunctionsClient('http://localhost', { customFetch }) + } + + test('parses JSON when Content-Type is lowercase', async () => { + const client = makeClient('application/json', JSON.stringify({ foo: 'bar' })) + const { data, error } = await client.invoke('fn', {}) + expect(error).toBeNull() + expect(data).toEqual({ foo: 'bar' }) + }) + + test('parses JSON when Content-Type has mixed casing', async () => { + const client = makeClient('Application/JSON; charset=utf-8', JSON.stringify({ foo: 'bar' })) + const { data, error } = await client.invoke('fn', {}) + expect(error).toBeNull() + expect(data).toEqual({ foo: 'bar' }) + }) + + test('parses JSON when Content-Type is uppercase', async () => { + const client = makeClient('APPLICATION/JSON', JSON.stringify({ foo: 'bar' })) + const { data, error } = await client.invoke('fn', {}) + expect(error).toBeNull() + expect(data).toEqual({ foo: 'bar' }) + }) +})