test-server.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { OpenAPIHono } from '@hono/zod-openapi';
  2. import { Hono } from 'hono';
  3. import { DataSource } from 'typeorm';
  4. export interface TestServerOptions {
  5. setupAuth?: boolean;
  6. setupDatabase?: boolean;
  7. setupMiddlewares?: boolean;
  8. }
  9. /**
  10. * 创建测试服务器实例
  11. */
  12. export function createTestServer(
  13. app: OpenAPIHono | Hono,
  14. options: TestServerOptions = {}
  15. ) {
  16. const server = app;
  17. // 设置默认选项
  18. const {
  19. setupAuth = true
  20. } = options;
  21. return {
  22. get: (path: string, headers?: Record<string, string>) =>
  23. makeRequest('GET', path, undefined, headers),
  24. post: (path: string, body?: any, headers?: Record<string, string>) =>
  25. makeRequest('POST', path, body, headers),
  26. put: (path: string, body?: any, headers?: Record<string, string>) =>
  27. makeRequest('PUT', path, body, headers),
  28. delete: (path: string, headers?: Record<string, string>) =>
  29. makeRequest('DELETE', path, undefined, headers),
  30. patch: (path: string, body?: any, headers?: Record<string, string>) =>
  31. makeRequest('PATCH', path, body, headers)
  32. };
  33. async function makeRequest(
  34. method: string,
  35. path: string,
  36. body?: any,
  37. customHeaders?: Record<string, string>
  38. ) {
  39. const url = new URL(path, 'http://localhost:3000');
  40. const headers: Record<string, string> = {
  41. 'Content-Type': 'application/json',
  42. ...(setupAuth && { 'Authorization': 'Bearer test-token-123' }),
  43. ...customHeaders
  44. };
  45. const request = new Request(url.toString(), {
  46. method,
  47. headers,
  48. body: body ? JSON.stringify(body) : undefined,
  49. });
  50. try {
  51. const response = await server.fetch(request);
  52. const responseHeaders: Record<string, string> = {};
  53. response.headers.forEach((value: string, key: string) => {
  54. responseHeaders[key] = value;
  55. });
  56. let responseData: any;
  57. const contentType = response.headers.get('content-type');
  58. if (contentType?.includes('application/json')) {
  59. responseData = await response.json();
  60. } else {
  61. responseData = await response.text();
  62. }
  63. return {
  64. status: response.status,
  65. headers: responseHeaders,
  66. data: responseData,
  67. json: async () => responseData,
  68. text: async () => responseData
  69. };
  70. } catch (error) {
  71. throw new Error(`Request failed: ${error}`);
  72. }
  73. }
  74. }