12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import { describe, it, expect, vi, beforeEach } from 'vitest'
- import { get } from '../src/utils'
- describe('get Function Header Tests', () => {
- const mockFetch = vi.fn();
- const mockResponse = new Response(null, { status: 200 });
- beforeEach(() => {
- mockFetch.mockReset();
- mockFetch.mockResolvedValue(mockResponse);
- });
- const defaultHeaders = {
- 'Content-Type': 'application/json',
- 'Accept': 'application/json',
- 'User-Agent': expect.stringMatching(/ollama-js\/.*/)
- };
- it('should use default headers when no headers provided', async () => {
- await get(mockFetch, 'http://example.com');
-
- expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
- headers: expect.objectContaining(defaultHeaders)
- });
- });
- it('should handle Headers instance', async () => {
- const customHeaders = new Headers({
- 'Authorization': 'Bearer token',
- 'X-Custom': 'value'
- });
- await get(mockFetch, 'http://example.com', { headers: customHeaders });
- expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
- headers: expect.objectContaining({
- ...defaultHeaders,
- 'authorization': 'Bearer token',
- 'x-custom': 'value'
- })
- });
- });
- it('should handle plain object headers', async () => {
- const customHeaders = {
- 'Authorization': 'Bearer token',
- 'X-Custom': 'value'
- };
- await get(mockFetch, 'http://example.com', { headers: customHeaders });
- expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
- headers: expect.objectContaining({
- ...defaultHeaders,
- 'Authorization': 'Bearer token',
- 'X-Custom': 'value'
- })
- });
- });
- it('should not allow custom headers to override default User-Agent', async () => {
- const customHeaders = {
- 'User-Agent': 'custom-agent'
- };
- await get(mockFetch, 'http://example.com', { headers: customHeaders });
- expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
- headers: expect.objectContaining({
- 'User-Agent': expect.stringMatching(/ollama-js\/.*/)
- })
- });
- });
- it('should handle empty headers object', async () => {
- await get(mockFetch, 'http://example.com', { headers: {} });
- expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
- headers: expect.objectContaining(defaultHeaders)
- });
- });
- });
|