smartproxy/test/test.route-redirects.ts

98 lines
2.7 KiB
TypeScript

import { tap, expect } from '@git.zone/tstest/tapbundle';
import { SmartProxy } from '../ts/proxies/smart-proxy/index.js';
import { createHttpToHttpsRedirect } from '../ts/proxies/smart-proxy/utils/route-helpers.js';
import type { IRouteConfig } from '../ts/proxies/smart-proxy/models/route-types.js';
// Test that HTTP to HTTPS redirects work correctly
tap.test('should handle HTTP to HTTPS redirects', async (tools) => {
// Create a simple HTTP to HTTPS redirect route
const redirectRoute = createHttpToHttpsRedirect(
'example.com',
443,
{
name: 'HTTP to HTTPS Redirect Test'
}
);
// Verify the route is configured correctly
expect(redirectRoute.action.type).toEqual('redirect');
expect(redirectRoute.action.redirect).toBeTruthy();
expect(redirectRoute.action.redirect?.to).toEqual('https://{domain}:443{path}');
expect(redirectRoute.action.redirect?.status).toEqual(301);
expect(redirectRoute.match.ports).toEqual(80);
expect(redirectRoute.match.domains).toEqual('example.com');
});
tap.test('should handle custom redirect configurations', async (tools) => {
// Create a custom redirect route
const customRedirect: IRouteConfig = {
name: 'custom-redirect',
match: {
ports: [8080],
domains: ['old.example.com']
},
action: {
type: 'redirect',
redirect: {
to: 'https://new.example.com{path}',
status: 302
}
}
};
// Verify the route structure
expect(customRedirect.action.redirect?.to).toEqual('https://new.example.com{path}');
expect(customRedirect.action.redirect?.status).toEqual(302);
});
tap.test('should support multiple redirect scenarios', async (tools) => {
const routes: IRouteConfig[] = [
// HTTP to HTTPS redirect
createHttpToHttpsRedirect(['example.com', 'www.example.com']),
// Custom redirect with different port
{
name: 'custom-port-redirect',
match: {
ports: 8080,
domains: 'api.example.com'
},
action: {
type: 'redirect',
redirect: {
to: 'https://{domain}:8443{path}',
status: 308
}
}
},
// Redirect to different domain entirely
{
name: 'domain-redirect',
match: {
ports: 80,
domains: 'old-domain.com'
},
action: {
type: 'redirect',
redirect: {
to: 'https://new-domain.com{path}',
status: 301
}
}
}
];
// Create SmartProxy with redirect routes
const proxy = new SmartProxy({
routes
});
// Verify all routes are redirect type
routes.forEach(route => {
expect(route.action.type).toEqual('redirect');
expect(route.action.redirect).toBeTruthy();
});
});
export default tap.start();