fix: spoof ollama streaming responses for tool calls

This commit is contained in:
lingyuzeng
2026-03-22 20:15:30 +08:00
parent 7a718d8983
commit 649958677e
3 changed files with 103 additions and 14 deletions

View File

@@ -62,20 +62,36 @@ describe('Proxy Integration Test', () => {
});
});
it('rejects streaming requests cleanly', async () => {
it('spoofs streaming responses for stream=true requests', async () => {
const requestFixturePath = path.join(__dirname, 'fixtures', 'openclaw-like-request.json');
const responseFixturePath = path.join(__dirname, 'fixtures', 'ollama-xml-response.json');
const requestJson = JSON.parse(fs.readFileSync(requestFixturePath, 'utf8'));
const responseJson = JSON.parse(fs.readFileSync(responseFixturePath, 'utf8'));
requestJson.stream = true;
(global.fetch as any).mockResolvedValue({
ok: true,
text: async () => JSON.stringify(responseJson),
json: async () => responseJson
});
const response = await server.inject({
method: 'POST',
url: '/api/chat',
payload: requestJson
});
expect(response.statusCode).toBe(400);
const body = JSON.parse(response.payload);
expect(body.error).toContain('Streaming is not supported');
expect(global.fetch).not.toHaveBeenCalled();
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toContain('text/event-stream');
expect(global.fetch).toHaveBeenCalledTimes(1);
const fetchArgs = (global.fetch as any).mock.calls[0];
const upstreamBody = JSON.parse(fetchArgs[1].body);
expect(upstreamBody.stream).toBe(false);
expect(response.payload).toContain('"role":"assistant"');
expect(response.payload).toContain('"tool_calls"');
expect(response.payload).toContain('"finish_reason":"tool_calls"');
expect(response.payload).toContain('data: [DONE]');
});
});