Refactor integration tests and add feature-specific test suites

Moves Git, Git LFS, and authentication header forwarding tests from integration.test.js into dedicated feature test files under test/features/. Updates range-cache, security, and container-registry tests for consistency and minor improvements. Refactors src/index.js for code style and readability, especially around cache logic and error handling.
This commit is contained in:
xixu-me committed 2025-12-10 16:23:44 +08:00
1 parent 44b54580ad
commit ce93ea9861
8 files changed
+275 -282

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
import { SELF } from 'cloudflare:test';
import { describe, expect, it } from 'vitest';
describe('Authentication Header Forwarding', () => {
it('should forward Authorization header for Hugging Face requests', async () => {
// Test with a Hugging Face dataset file that would require authentication
const testUrl = 'https://example.com/hf/datasets/test/private-dataset/resolve/main/data.csv';
const authToken = 'Bearer hf_test_token_12345';
const response = await SELF.fetch(testUrl, {
method: 'HEAD',
headers: {
Authorization: authToken
}
});
// Should accept the request (not 400 bad request)
expect(response.status).not.toBe(400);
// Should attempt to proxy to HF with auth (status depends on actual HF response)
expect([200, 401, 403, 404]).toContain(response.status);
});
it('should forward Authorization header for GitHub API requests', async () => {
const testUrl = 'https://example.com/gh/test/private-repo/README.md';
const authToken = 'Bearer ghp_test_token_12345';
const response = await SELF.fetch(testUrl, {
method: 'HEAD',
headers: {
Authorization: authToken
}
});
// Should accept the request and forward the auth header
expect(response.status).not.toBe(400);
expect([200, 401, 403, 404]).toContain(response.status);
});
it('should forward Authorization header for PyPI authenticated requests', async () => {
const testUrl = 'https://example.com/pypi/simple/private-package/';
const authToken = 'Basic dGVzdDp0ZXN0MTIzNDU=';
const response = await SELF.fetch(testUrl, {
method: 'HEAD',
headers: {
Authorization: authToken
}
});
// Should accept the request
expect(response.status).not.toBe(400);
expect([200, 401, 403, 404]).toContain(response.status);
});
it('should work with gated Hugging Face models', async () => {
// Simulate a request to a gated model that requires authentication
const testUrl = 'https://example.com/hf/meta-llama/Llama-2-7b/resolve/main/config.json';
const authToken = 'Bearer hf_authenticated_token';
const response = await SELF.fetch(testUrl, {
headers: {
Authorization: authToken
}
});
// Should attempt to proxy with authentication
// The actual status depends on whether the token is valid and the model exists
expect(response.status).not.toBe(400);
});
});
+95
View File
@@ -0,0 +1,95 @@
import { SELF } from 'cloudflare:test';
import { describe, expect, it } from 'vitest';
describe('Git LFS Protocol Integration', () => {
it('should handle LFS info/lfs requests', async () => {
const testUrl = 'https://example.com/gh/microsoft/vscode.git/info/lfs';
const response = await SELF.fetch(testUrl, {
headers: {
'User-Agent': 'git-lfs/3.0.0 (GitHub; darwin amd64; go 1.17.2)'
}
});
expect([200, 301, 302, 404]).toContain(response.status);
});
it('should handle LFS batch API requests', async () => {
const testUrl = 'https://example.com/gh/microsoft/vscode.git/objects/batch';
const response = await SELF.fetch(testUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/vnd.git-lfs+json',
Accept: 'application/vnd.git-lfs+json',
'User-Agent': 'git-lfs/3.0.0'
},
body: JSON.stringify({
operation: 'download',
objects: [
{
oid: 'a1b2c3d4e5f6789012345678901234567890123456789012345678901234abcd',
size: 1024
}
]
})
});
expect([200, 301, 302, 400, 403, 404]).toContain(response.status);
});
it('should handle LFS object download requests', async () => {
const testUrl =
'https://example.com/gh/microsoft/vscode.git/objects/a1b2c3d4e5f6789012345678901234567890123456789012345678901234abcd';
const response = await SELF.fetch(testUrl, {
headers: {
'User-Agent': 'git-lfs/3.0.0',
Accept: 'application/octet-stream'
}
});
expect([200, 301, 302, 403, 404]).toContain(response.status);
});
it('should preserve LFS-specific headers', async () => {
const testUrl = 'https://example.com/gh/test/repo.git/objects/batch';
const response = await SELF.fetch(testUrl, {
method: 'POST',
headers: {
'User-Agent': 'git-lfs/3.0.0',
Accept: 'application/vnd.git-lfs+json',
'Content-Type': 'application/vnd.git-lfs+json'
},
body: '{}'
});
// Should not reject LFS-specific headers
expect(response.status).not.toBe(400);
});
it('should skip caching for LFS requests', async () => {
const testUrl = 'https://example.com/gh/test/repo.git/info/lfs';
// First request
const response1 = await SELF.fetch(testUrl, {
headers: {
'User-Agent': 'git-lfs/3.0.0'
}
});
// Second request - should not be cached
const response2 = await SELF.fetch(testUrl, {
headers: {
'User-Agent': 'git-lfs/3.0.0'
}
});
// Both requests should go to origin (no cache hit)
const metrics1 = response1.headers.get('X-Performance-Metrics');
const metrics2 = response2.headers.get('X-Performance-Metrics');
// Verify that neither indicates a cache hit
if (metrics1 && metrics2) {
expect(metrics1).not.toContain('cache_hit');
expect(metrics2).not.toContain('cache_hit');
}
});
});
+42
View File
@@ -0,0 +1,42 @@
import { SELF } from 'cloudflare:test';
import { describe, expect, it } from 'vitest';
describe('Git Protocol Integration', () => {
it('should handle Git info/refs requests', async () => {
const testUrl = 'https://example.com/gh/microsoft/vscode.git/info/refs?service=git-upload-pack';
const response = await SELF.fetch(testUrl, {
headers: {
'User-Agent': 'git/2.34.1'
}
});
expect([200, 301, 302, 404]).toContain(response.status);
});
it('should handle Git upload-pack requests', async () => {
const testUrl = 'https://example.com/gh/microsoft/vscode.git/git-upload-pack';
const response = await SELF.fetch(testUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-git-upload-pack-request',
'User-Agent': 'git/2.34.1'
},
body: '0000' // Minimal Git protocol data
});
expect([200, 301, 302, 400, 404]).toContain(response.status);
});
it('should preserve Git-specific headers', async () => {
const testUrl = 'https://example.com/gh/test/repo.git/info/refs';
const response = await SELF.fetch(testUrl, {
headers: {
'User-Agent': 'git/2.34.1',
'Git-Protocol': 'version=2'
}
});
// Should not reject Git-specific headers
expect(response.status).not.toBe(400);
});
});
+3 -13
View File
@@ -1,4 +1,5 @@
import { beforeAll, describe, expect, it } from 'vitest';
import { SELF } from 'cloudflare:test';
import { describe, expect, it } from 'vitest';
/**
* Tests for Range Request Caching Strategy
@@ -11,17 +12,6 @@ import { beforeAll, describe, expect, it } from 'vitest';
*/
describe('Range Request Caching Strategy', () => {
/** @type {any} */
let SELF;
beforeAll(async () => {
const { unstable_dev: unstableDev } = await import('wrangler');
const worker = await unstableDev('src/index.js', {
experimental: { disableExperimentalWarning: true }
});
SELF = worker;
});
describe('Cache Behavior for Range Requests', () => {
it('should not attempt to cache 206 responses', async () => {
const testUrl = 'https://example.com/gh/test/repo/sample.pdf';
@@ -44,7 +34,7 @@ describe('Range Request Caching Strategy', () => {
// Should not contain any cache put errors
const errorKeys = Object.keys(parsedMetrics).filter(
key => key.includes('error') || key.includes('fail')
key => (key.includes('error') || key.includes('fail')) && key !== 'client_error'
);
expect(errorKeys).toHaveLength(0);
}
+2 -2
View File
@@ -183,7 +183,7 @@ describe('Security Features', () => {
// If it doesn't throw, it should not be a server error
} catch (error) {
// Expected to throw TypeError for invalid header value
expect((/** @type {Error} */ (error)).message).toMatch(/[Ii]nvalid|[Hh]eader/);
expect(/** @type {Error} */ (error).message).toMatch(/[Ii]nvalid|[Hh]eader/);
}
});
});
@@ -200,7 +200,7 @@ describe('Security Features', () => {
responses.forEach((/** @type {Response} */ response) => {
expect(response.status).not.toBe(500);
});
});
}, 30000);
it('should timeout long-running requests', async () => {
// This test would need to be implemented based on actual timeout behavior