This commit is contained in:
2025-05-29 10:13:41 +00:00
parent 200a735876
commit 6a08bbc558
14 changed files with 211 additions and 303 deletions

View File

@ -1,5 +1,5 @@
import { tap, expect } from '@git.zone/tstest/tapbundle';
import { SmartProxy } from '../ts/index.js';
import { SmartProxy, SocketHandlers } from '../ts/index.js';
import * as net from 'net';
// Test that HTTP-01 challenges are properly processed when the initial data arrives
@ -9,36 +9,28 @@ tap.test('should correctly handle HTTP-01 challenge requests with initial data c
const challengeResponse = 'mock-response-for-challenge';
const challengePath = `/.well-known/acme-challenge/${challengeToken}`;
// Create a handler function that responds to ACME challenges
const acmeHandler = async (context: any) => {
// Create a socket handler that responds to ACME challenges using httpServer
const acmeHandler = SocketHandlers.httpServer((req, res) => {
// Log request details for debugging
console.log(`Received request: ${context.method} ${context.path}`);
console.log(`Received request: ${req.method} ${req.url}`);
// Check if this is an ACME challenge request
if (context.path.startsWith('/.well-known/acme-challenge/')) {
const token = context.path.substring('/.well-known/acme-challenge/'.length);
if (req.url?.startsWith('/.well-known/acme-challenge/')) {
const token = req.url.substring('/.well-known/acme-challenge/'.length);
// If the token matches our test token, return the response
if (token === challengeToken) {
return {
status: 200,
headers: {
'Content-Type': 'text/plain'
},
body: challengeResponse
};
res.header('Content-Type', 'text/plain');
res.send(challengeResponse);
return;
}
}
// For any other requests, return 404
return {
status: 404,
headers: {
'Content-Type': 'text/plain'
},
body: 'Not found'
};
};
res.status(404);
res.header('Content-Type', 'text/plain');
res.send('Not found');
});
// Create a proxy with the ACME challenge route
const proxy = new SmartProxy({
@ -49,8 +41,8 @@ tap.test('should correctly handle HTTP-01 challenge requests with initial data c
path: '/.well-known/acme-challenge/*'
},
action: {
type: 'static',
handler: acmeHandler
type: 'socket-handler',
socketHandler: acmeHandler
}
}]
});
@ -98,27 +90,23 @@ tap.test('should correctly handle HTTP-01 challenge requests with initial data c
// Test that non-existent challenge tokens return 404
tap.test('should return 404 for non-existent challenge tokens', async (tapTest) => {
// Create a handler function that behaves like a real ACME handler
const acmeHandler = async (context: any) => {
if (context.path.startsWith('/.well-known/acme-challenge/')) {
const token = context.path.substring('/.well-known/acme-challenge/'.length);
// Create a socket handler that behaves like a real ACME handler
const acmeHandler = SocketHandlers.httpServer((req, res) => {
if (req.url?.startsWith('/.well-known/acme-challenge/')) {
const token = req.url.substring('/.well-known/acme-challenge/'.length);
// In this test, we only recognize one specific token
if (token === 'valid-token') {
return {
status: 200,
headers: { 'Content-Type': 'text/plain' },
body: 'valid-response'
};
res.header('Content-Type', 'text/plain');
res.send('valid-response');
return;
}
}
// For all other paths or unrecognized tokens, return 404
return {
status: 404,
headers: { 'Content-Type': 'text/plain' },
body: 'Not found'
};
};
res.status(404);
res.header('Content-Type', 'text/plain');
res.send('Not found');
});
// Create a proxy with the ACME challenge route
const proxy = new SmartProxy({
@ -129,8 +117,8 @@ tap.test('should return 404 for non-existent challenge tokens', async (tapTest)
path: '/.well-known/acme-challenge/*'
},
action: {
type: 'static',
handler: acmeHandler
type: 'socket-handler',
socketHandler: acmeHandler
}
}]
});