Skip to content

Commit

Permalink
add pipe support for wpt server (#3228)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed May 9, 2024
1 parent 60efbf0 commit 71c4d73
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 19 deletions.
3 changes: 3 additions & 0 deletions test/wpt/runner/runner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ export class WPTRunner extends EventEmitter {
}
})

worker.stdout.pipe(process.stdout)
worker.stderr.pipe(process.stderr)

const fileUrl = new URL(`/${this.#folderName}${test.slice(this.#folderPath.length)}`, 'http://wpt')
fileUrl.pathname = fileUrl.pathname.replace(/\.js$/, '.html')
fileUrl.search = variant
Expand Down
3 changes: 3 additions & 0 deletions test/wpt/server/constants.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const symbols = {
kContent: Symbol('content')
}
57 changes: 38 additions & 19 deletions test/wpt/server/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { createReadStream, readFileSync, existsSync } from 'node:fs'
import { setTimeout as sleep } from 'node:timers/promises'
import { route as networkPartitionRoute } from './routes/network-partition-key.mjs'
import { route as redirectRoute } from './routes/redirect.mjs'
import { Pipeline } from './util.mjs'
import { symbols } from './constants.mjs'

const tests = fileURLToPath(join(import.meta.url, '../../tests'))

Expand All @@ -31,6 +33,11 @@ const stash = new Stash()
const server = createServer(async (req, res) => {
const fullUrl = new URL(req.url, `http://localhost:${server.address().port}`)

if (fullUrl.searchParams.has('pipe')) {
const pipe = new Pipeline(fullUrl.searchParams.get('pipe'))
res = await pipe.call(req, res)
}

switch (fullUrl.pathname) {
case '/service-workers/cache-storage/resources/blank.html': {
res.setHeader('content-type', 'text/html')
Expand All @@ -55,7 +62,8 @@ const server = createServer(async (req, res) => {
case '/fetch/data-urls/resources/base64.json':
case '/fetch/data-urls/resources/data-urls.json':
case '/fetch/api/resources/empty.txt':
case '/fetch/api/resources/data.json': {
case '/fetch/api/resources/data.json':
case '/common/text-plain.txt': {
// If this specific resources requires custom headers
const customHeadersPath = join(tests, fullUrl.pathname + '.headers')
if (existsSync(customHeadersPath)) {
Expand All @@ -74,9 +82,11 @@ const server = createServer(async (req, res) => {
}

// https://github.com/web-platform-tests/wpt/blob/6ae3f702a332e8399fab778c831db6b7dca3f1c6/fetch/api/resources/data.json
return createReadStream(join(tests, fullUrl.pathname))
createReadStream(join(tests, fullUrl.pathname))
.on('end', () => res.end())
.pipe(res)

break
}
case '/fetch/api/resources/trickle.py': {
// Note: python's time.sleep(...) takes seconds, while setTimeout
Expand Down Expand Up @@ -133,7 +143,7 @@ const server = createServer(async (req, res) => {
}

res.end()
return
break
}
case '/fetch/api/resources/stash-take.py': {
// https://github.com/web-platform-tests/wpt/blob/6ae3f702a332e8399fab778c831db6b7dca3f1c6/fetch/api/resources/stash-take.py
Expand All @@ -144,7 +154,8 @@ const server = createServer(async (req, res) => {
const took = stash.take(key, fullUrl.pathname) ?? null

res.write(JSON.stringify(took))
return res.end()
res.end()
break
}
case '/fetch/api/resources/echo-content.py': {
res.setHeader('X-Request-Method', req.method)
Expand Down Expand Up @@ -268,15 +279,19 @@ const server = createServer(async (req, res) => {
break
}
case '/fetch/connection-pool/resources/network-partition-key.py': {
return networkPartitionRoute(req, res, fullUrl)
networkPartitionRoute(req, res, fullUrl)
break
}
case '/resources/top.txt': {
return createReadStream(join(tests, 'fetch/api/', fullUrl.pathname))
createReadStream(join(tests, 'fetch/api/', fullUrl.pathname))
.on('end', () => res.end())
.pipe(res)

break
}
case '/fetch/api/resources/redirect.py': {
return redirectRoute(req, res, fullUrl)
redirectRoute(req, res, fullUrl)
break
}
case '/fetch/api/resources/method.py': {
if (fullUrl.searchParams.has('cors')) {
Expand All @@ -299,7 +314,7 @@ const server = createServer(async (req, res) => {
}

res.end()
return
break
}
case '/fetch/api/resources/clean-stash.py': {
const token = fullUrl.searchParams.get('token')
Expand Down Expand Up @@ -334,7 +349,7 @@ const server = createServer(async (req, res) => {

if (req.headers.authorization) {
res.end(req.headers.authorization)
return
break
}

res.end('none')
Expand Down Expand Up @@ -363,48 +378,48 @@ const server = createServer(async (req, res) => {

if (user === 'user' && password === 'password') {
res.end('Authentication done')
return
break
}

const realm = fullUrl.searchParams.get('realm') ?? 'test'

res.statusCode = 401
res.setHeader('WWW-Authenticate', `Basic realm="${realm}"`)
res.end('Please login with credentials \'user\' and \'password\'')
return
break
}
case '/fetch/api/resources/redirect-empty-location.py': {
res.setHeader('location', '')
res.statusCode = 302
res.end('')
return
break
}
case '/service-workers/cache-storage/resources/fetch-status.py': {
const status = Number(fullUrl.searchParams.get('status'))

res.statusCode = status
res.end()
return
break
}
case '/service-workers/cache-storage/this-resource-should-not-exist':
case '/service-workers/cache-storage/this-does-not-exist-please-dont-create-it': {
res.statusCode = 404
res.end()
return
break
}
case '/service-workers/cache-storage/resources/vary.py': {
if (fullUrl.searchParams.has('clear-vary-value-override-cookie')) {
res.setHeader('cookie', '')
res.end('vary cookie cleared')
return
break
}

const setCookieVary = fullUrl.searchParams.get('set-vary-value-override-cookie') ?? ''

if (setCookieVary) {
res.setHeader('set-cookie', `vary-value-override=${setCookieVary}`)
res.end('vary cookie set')
return
break
}

const cookieVary = req.headers.cookie?.split(';').find((c) => c.includes('vary-value-override='))
Expand All @@ -420,7 +435,7 @@ const server = createServer(async (req, res) => {
}

res.end('vary response')
return
break
}
case '/eventsource/resources/message.py': {
const mime = fullUrl.searchParams.get('mime') ?? 'text/event-stream'
Expand All @@ -435,7 +450,7 @@ const server = createServer(async (req, res) => {
res.end()
}, sleep)

return
break
}
case '/eventsource/resources/last-event-id.py': {
const lastEventId = req.headers['Last-Event-ID'] ?? ''
Expand All @@ -451,13 +466,17 @@ const server = createServer(async (req, res) => {
res.end()
}

return
break
}
default: {
res.statusCode = 200
res.end(fullUrl.toString())
}
}

if (res[symbols.kContent]) {
res.write(res[symbols.kContent])
}
}).listen(0)

await once(server, 'listening')
Expand Down
Loading

0 comments on commit 71c4d73

Please sign in to comment.