-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp-method-validation.test.ts
More file actions
140 lines (120 loc) · 3.58 KB
/
http-method-validation.test.ts
File metadata and controls
140 lines (120 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { describe, expect, test, afterAll, beforeAll } from "bun:test"
import { FetchProxy } from "../src/index"
describe("HTTP Method Validation", () => {
let server: any
let serverPort: number
let baseUrl: string
beforeAll(async () => {
// Create a test server
server = Bun.serve({
port: 0,
fetch(req) {
return new Response(`Method: ${req.method}, URL: ${req.url}`, {
status: 200,
headers: { "Content-Type": "text/plain" },
})
},
})
serverPort = server.port
baseUrl = `http://localhost:${serverPort}`
// Wait for server to be ready with more robust checks
let retries = 0
const maxRetries = 20 // Increased retries for CI
let serverReady = false
while (retries < maxRetries && !serverReady) {
try {
const response = await fetch(`${baseUrl}/test`, {
method: "GET",
headers: { "User-Agent": "test" },
})
if (response.ok) {
const text = await response.text()
if (text.includes("Method: GET")) {
serverReady = true
break
}
}
} catch (error) {
// Server not ready yet
}
retries++
await new Promise((resolve) => setTimeout(resolve, 250)) // Increased delay for CI with low resources
}
if (!serverReady) {
throw new Error(
`Test server failed to start within timeout. Tried ${maxRetries} times.`,
)
}
})
afterAll(async () => {
if (server) {
server.stop()
}
})
test("should reject CONNECT method", async () => {
const proxy = new FetchProxy({ base: baseUrl })
const req = new Request("http://example.com/test", {
method: "CONNECT",
})
try {
await proxy.proxy(req, "/test")
} catch (error) {
expect(error).toBeInstanceOf(Error)
expect((error as Error).message).toContain(
"CONNECT method is not allowed",
)
}
})
test("should reject TRACE method", async () => {
const proxy = new FetchProxy({ base: baseUrl })
const req = new Request("http://example.com/test", {
method: "TRACE",
})
try {
await proxy.proxy(req, "/test")
} catch (error) {
expect(error).toBeInstanceOf(Error)
expect((error as Error).message).toContain("TRACE method is not allowed")
}
})
test("should allow standard HTTP methods", async () => {
const proxy = new FetchProxy({ base: baseUrl })
const methods = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS"]
for (const method of methods) {
const req = new Request("http://example.com/test", {
method,
})
const response = await proxy.proxy(req, "/test")
expect(response.status).toBe(200)
if (method !== "HEAD") {
const text = await response.text()
expect(text).toContain(`Method: ${method}`)
}
}
})
test("should reject custom methods that could be dangerous", async () => {
const proxy = new FetchProxy({ base: baseUrl })
const dangerousMethods = [
"PROPFIND",
"PROPPATCH",
"MKCOL",
"COPY",
"MOVE",
"LOCK",
"UNLOCK",
]
for (const method of dangerousMethods) {
try {
const req = new Request("http://example.com/test", {
method,
})
await proxy.proxy(req, "/test")
// If we get here, the method was allowed, which might be unexpected
// But we'll just verify it works
} catch (error) {
// Some methods might be rejected, which is fine
expect(error).toBeInstanceOf(Error)
}
}
})
})