basic.integration.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { describe, it, expect, vi } from 'vitest';
  2. import { OpenAPIHono } from '@hono/zod-openapi';
  3. import { createApiClient } from '../../__test_utils__/api-client';
  4. // 简单的mock测试
  5. describe('Basic API Integration Tests', () => {
  6. it('应该创建测试服务器并响应请求', async () => {
  7. // 创建简单的测试应用
  8. const app = new OpenAPIHono();
  9. // 添加一个简单的测试路由
  10. app.get('/test', (c) => {
  11. return c.json({ message: 'Hello, test!' });
  12. });
  13. // 创建API客户端
  14. const apiClient = createApiClient(app);
  15. // 发送请求
  16. const response = await apiClient.get('/test');
  17. // 验证响应
  18. expect(response.status).toBe(200);
  19. expect(response.data).toEqual({ message: 'Hello, test!' });
  20. });
  21. it('应该处理404错误', async () => {
  22. const app = new OpenAPIHono();
  23. const apiClient = createApiClient(app);
  24. const response = await apiClient.get('/non-existent');
  25. expect(response.status).toBe(404);
  26. });
  27. it('应该支持POST请求', async () => {
  28. const app = new OpenAPIHono();
  29. app.post('/echo', async (c) => {
  30. const body = await c.req.json();
  31. return c.json({ echoed: body });
  32. });
  33. const apiClient = createApiClient(app);
  34. const testData = { name: 'test', value: 123 };
  35. const response = await apiClient.post('/echo', testData);
  36. expect(response.status).toBe(200);
  37. expect(response.data).toEqual({ echoed: testData });
  38. });
  39. });