2
0

utils.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { describe, it, expect, vi, beforeEach } from 'vitest'
  2. import { get } from '../src/utils'
  3. describe('get Function Header Tests', () => {
  4. const mockFetch = vi.fn();
  5. const mockResponse = new Response(null, { status: 200 });
  6. beforeEach(() => {
  7. mockFetch.mockReset();
  8. mockFetch.mockResolvedValue(mockResponse);
  9. });
  10. const defaultHeaders = {
  11. 'Content-Type': 'application/json',
  12. 'Accept': 'application/json',
  13. 'User-Agent': expect.stringMatching(/ollama-js\/.*/)
  14. };
  15. it('should use default headers when no headers provided', async () => {
  16. await get(mockFetch, 'http://example.com');
  17. expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
  18. headers: expect.objectContaining(defaultHeaders)
  19. });
  20. });
  21. it('should handle Headers instance', async () => {
  22. const customHeaders = new Headers({
  23. 'Authorization': 'Bearer token',
  24. 'X-Custom': 'value'
  25. });
  26. await get(mockFetch, 'http://example.com', { headers: customHeaders });
  27. expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
  28. headers: expect.objectContaining({
  29. ...defaultHeaders,
  30. 'authorization': 'Bearer token',
  31. 'x-custom': 'value'
  32. })
  33. });
  34. });
  35. it('should handle plain object headers', async () => {
  36. const customHeaders = {
  37. 'Authorization': 'Bearer token',
  38. 'X-Custom': 'value'
  39. };
  40. await get(mockFetch, 'http://example.com', { headers: customHeaders });
  41. expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
  42. headers: expect.objectContaining({
  43. ...defaultHeaders,
  44. 'Authorization': 'Bearer token',
  45. 'X-Custom': 'value'
  46. })
  47. });
  48. });
  49. it('should not allow custom headers to override default User-Agent', async () => {
  50. const customHeaders = {
  51. 'User-Agent': 'custom-agent'
  52. };
  53. await get(mockFetch, 'http://example.com', { headers: customHeaders });
  54. expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
  55. headers: expect.objectContaining({
  56. 'User-Agent': expect.stringMatching(/ollama-js\/.*/)
  57. })
  58. });
  59. });
  60. it('should handle empty headers object', async () => {
  61. await get(mockFetch, 'http://example.com', { headers: {} });
  62. expect(mockFetch).toHaveBeenCalledWith('http://example.com', {
  63. headers: expect.objectContaining(defaultHeaders)
  64. });
  65. });
  66. });