Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(nodejs-client): override default headers case-insensitively (#44090)

D419100442c753fdfc
diff --git a/sdks/nodejs-client/src/http/client.ts b/sdks/nodejs-client/src/http/client.ts
index e22250808aa..9f87dec3239 100644
--- a/sdks/nodejs-client/src/http/client.ts
+++ b/sdks/nodejs-client/src/http/client.ts
@@ -503,19 +503,20 @@ export class HttpClient {
let attempt = 0
while (true) {
const preparedBody = prepareRequestBody(method, data)
- const requestHeaders: Headers = {
+ const requestHeaders = new globalThis.Headers({
Authorization: `Bearer ${apiKey}`,
...preparedBody.headers,
- ...headers,
+ })
+ for (const [name, value] of Object.entries(headers ?? {})) {
+ requestHeaders.set(name, value)
}
if (
typeof process !== 'undefined' &&
!!process.versions?.node &&
- !requestHeaders['User-Agent'] &&
- !requestHeaders['user-agent']
+ !requestHeaders.has('User-Agent')
) {
- requestHeaders['User-Agent'] = DEFAULT_USER_AGENT
+ requestHeaders.set('User-Agent', DEFAULT_USER_AGENT)
}
const timeoutContext = createTimeoutContext(timeout * 1000)
diff --git a/sdks/nodejs-client/src/index.test.ts b/sdks/nodejs-client/src/index.test.ts
index a7e464409cc..285f34698d6 100644
--- a/sdks/nodejs-client/src/index.test.ts
+++ b/sdks/nodejs-client/src/index.test.ts
@@ -68,9 +68,9 @@ describe('Send Requests', () => {
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit]
expect(url).toBe(`${BASE_URL}${endpoint}`)
expect(init.method).toBe(method)
- expect(init.headers).toMatchObject({
- Authorization: 'Bearer test',
- 'User-Agent': 'dify-client-node',
+ expect(Object.fromEntries(new Headers(init.headers).entries())).toMatchObject({
+ authorization: 'Bearer test',
+ 'user-agent': 'dify-client-node',
})
})
@@ -84,8 +84,8 @@ describe('Send Requests', () => {
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit]
expect(url).toBe(`${BASE_URL}${routes.getMeta.url()}?user=end-user`)
expect(init.method).toBe(routes.getMeta.method)
- expect(init.headers).toMatchObject({
- Authorization: 'Bearer test',
+ expect(Object.fromEntries(new Headers(init.headers).entries())).toMatchObject({
+ authorization: 'Bearer test',
})
})
})
@@ -131,8 +131,8 @@ describe('File uploads', () => {
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit]
expect(url).toBe(`${BASE_URL}${routes.fileUpload.url()}`)
expect(init.method).toBe(routes.fileUpload.method)
- expect(init.headers).toMatchObject({
- Authorization: 'Bearer test',
+ expect(Object.fromEntries(new Headers(init.headers).entries())).toMatchObject({
+ authorization: 'Bearer test',
'content-type': 'multipart/form-data; boundary=test',
})
expect(init.body).not.toBe(form)
@@ -156,9 +156,9 @@ describe('Workflow client', () => {
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit]
expect(url).toBe(`${BASE_URL}${routes.stopWorkflow.url('task-1')}`)
expect(init.method).toBe(routes.stopWorkflow.method)
- expect(init.headers).toMatchObject({
- Authorization: 'Bearer test',
- 'Content-Type': 'application/json',
+ expect(Object.fromEntries(new Headers(init.headers).entries())).toMatchObject({
+ authorization: 'Bearer test',
+ 'content-type': 'application/json',
})
expect(init.body).toBe(JSON.stringify({ user: 'end-user' }))
})
@@ -205,8 +205,8 @@ describe('Chat client', () => {
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit]
expect(url).toBe(`${BASE_URL}${routes.getSuggested.url('msg-1')}?user=end-user`)
expect(init.method).toBe(routes.getSuggested.method)
- expect(init.headers).toMatchObject({
- Authorization: 'Bearer test',
+ expect(Object.fromEntries(new Headers(init.headers).entries())).toMatchObject({
+ authorization: 'Bearer test',
})
})
diff --git a/sdks/nodejs-client/tests/http.integration.test.ts b/sdks/nodejs-client/tests/http.integration.test.ts
index 188f908e4e8..2d187c22b18 100644
--- a/sdks/nodejs-client/tests/http.integration.test.ts
+++ b/sdks/nodejs-client/tests/http.integration.test.ts
@@ -105,6 +105,31 @@ describe('HttpClient integration', () => {
expect(requests.at(-1)?.body.toString('utf8')).toBe(JSON.stringify({ user: 'u' }))
})
+ it.each(['Content-Type', 'content-type', 'CoNtEnT-TyPe'])(
+ 'overrides the JSON content type with %s',
+ async (headerName) => {
+ await client.request({
+ method: 'POST',
+ path: '/json',
+ data: { user: 'u' },
+ headers: { [headerName]: 'application/custom+json' },
+ })
+
+ expect(requests.at(-1)?.headers['content-type']).toBe('application/custom+json')
+ expect(requests.at(-1)?.body.toString('utf8')).toBe(JSON.stringify({ user: 'u' }))
+ },
+ )
+
+ it('overrides authorization without combining differently cased defaults', async () => {
+ await client.request({
+ method: 'GET',
+ path: '/json',
+ headers: { authorization: 'Bearer override-key' },
+ })
+
+ expect(requests.at(-1)?.headers.authorization).toBe('Bearer override-key')
+ })
+
it('supports streaming request bodies with duplex fetch', async () => {
const response = await client.request<{ received: string }>({
method: 'POST',