- 移除未使用的YAML锚点定义 - 替换commands字段中的锚点引用为实际值 - 移除有问题的通知步骤 - 修复测试文件中的问题 - 添加新的测试用例和配置文件
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import { middleware } from './middleware';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
jest.mock('next/server', () => ({
|
||||
NextResponse: {
|
||||
next: jest.fn(() => ({
|
||||
headers: new Headers(),
|
||||
})),
|
||||
rewrite: jest.fn(() => ({
|
||||
headers: new Headers(),
|
||||
})),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('middleware', () => {
|
||||
let mockRequest: any;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
|
||||
mockRequest = {
|
||||
nextUrl: {
|
||||
pathname: '',
|
||||
clone: jest.fn(() => ({
|
||||
pathname: '',
|
||||
})),
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
it('should allow auth routes', () => {
|
||||
mockRequest.nextUrl.pathname = '/api/auth/signin';
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(NextResponse.next).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should allow admin routes', () => {
|
||||
mockRequest.nextUrl.pathname = '/api/admin/users';
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(NextResponse.next).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should allow content routes', () => {
|
||||
mockRequest.nextUrl.pathname = '/api/content/posts';
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(NextResponse.next).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should rewrite legacy API paths to v1', () => {
|
||||
mockRequest.nextUrl.pathname = '/api/config';
|
||||
mockRequest.nextUrl.clone.mockReturnValue({
|
||||
pathname: '/api/v1/config',
|
||||
});
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(NextResponse.rewrite).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should rewrite health API to v1', () => {
|
||||
mockRequest.nextUrl.pathname = '/api/health';
|
||||
mockRequest.nextUrl.clone.mockReturnValue({
|
||||
pathname: '/api/v1/health',
|
||||
});
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(NextResponse.rewrite).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not rewrite versioned API paths', () => {
|
||||
mockRequest.nextUrl.pathname = '/api/v1/users';
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(NextResponse.next).toHaveBeenCalled();
|
||||
expect(NextResponse.rewrite).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should set X-API-Version header for versioned routes', () => {
|
||||
mockRequest.nextUrl.pathname = '/api/v2/users';
|
||||
|
||||
const mockResponse = {
|
||||
headers: new Headers(),
|
||||
};
|
||||
(NextResponse.next as jest.Mock).mockReturnValue(mockResponse);
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(mockResponse.headers.get('X-API-Version')).toBe('v2');
|
||||
});
|
||||
|
||||
it('should handle docs routes', () => {
|
||||
mockRequest.nextUrl.pathname = '/api/docs';
|
||||
|
||||
const mockResponse = {
|
||||
headers: new Headers(),
|
||||
};
|
||||
(NextResponse.next as jest.Mock).mockReturnValue(mockResponse);
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(mockResponse.headers.get('X-API-Version')).toBe('none');
|
||||
});
|
||||
|
||||
it('should handle api-docs route', () => {
|
||||
mockRequest.nextUrl.pathname = '/api-docs';
|
||||
|
||||
const mockResponse = {
|
||||
headers: new Headers(),
|
||||
};
|
||||
(NextResponse.next as jest.Mock).mockReturnValue(mockResponse);
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(mockResponse.headers.get('X-API-Version')).toBe('none');
|
||||
});
|
||||
|
||||
it('should allow other API routes', () => {
|
||||
mockRequest.nextUrl.pathname = '/api/users';
|
||||
|
||||
middleware(mockRequest);
|
||||
|
||||
expect(NextResponse.next).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user