-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
You now have the ability to add pass a postBody function to the parser which will get called once the body stream has completed
- Loading branch information
Showing
3 changed files
with
105 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { checkSurrogate, testResponse, urlHandler } from "./helpers"; | ||
import { esi, ESIConfig } from "../src"; | ||
import { AddressInfo } from "net"; | ||
import http from "http"; | ||
|
||
const esiHead = { | ||
"Content-Type": "text/html", | ||
"Surrogate-Control": `content="ESI/1.0"`, | ||
}; | ||
|
||
let parser: esi; | ||
let config: ESIConfig; | ||
const makeRequest = async function (request: string, details?: RequestInit) { | ||
const reqUrl = new URL(request, `http://localhost:${port}`).toString(); | ||
const req = new Request(reqUrl, details); | ||
return parser.parse(req); | ||
}; | ||
|
||
const routeHandler = new urlHandler(); | ||
// @ts-ignore | ||
const server = http.createServer(routeHandler.route); | ||
let port = 0; | ||
const testingDetails = { | ||
port: 0, | ||
hostname: "localhost", | ||
proto: "http:", | ||
url: `http://localhost:0`, | ||
}; | ||
|
||
beforeAll(() => { | ||
// Setup a basic HTTP server to handle traffic | ||
server.listen(0); | ||
const address = server.address() as AddressInfo; | ||
port = address.port; | ||
testingDetails.port = address.port; | ||
testingDetails.url = `http://localhost:${port}`; | ||
}); | ||
|
||
afterAll((done) => { | ||
server.close(() => { | ||
done(); | ||
}); | ||
}); | ||
|
||
test("TEST 1: postBody Function", async () => { | ||
const url = `/post-body/test-1`; | ||
let count = 0; | ||
const postBody = function () { | ||
expect(count).toEqual(3); | ||
return; | ||
}; | ||
parser = new esi(config, undefined, undefined, postBody); | ||
|
||
const printFragment = function ( | ||
req: http.IncomingMessage, | ||
res: testResponse, | ||
) { | ||
count++; | ||
const url = new URL(req.url as string, `http://localhost:${port}`); | ||
const query = url.searchParams.get("a") ? url.searchParams.get("a") : ""; | ||
res.end(`FRAGMENT: ${query}\n`); | ||
}; | ||
routeHandler.add(url, function (req, res) { | ||
res.writeHead(200, esiHead); | ||
res.say("1"); | ||
res.write(`<esi:include src="${url}/fragment_1" />`); | ||
res.say("2"); | ||
res.write(`<esi:include src="${url}/fragment_1?a=2" />`); | ||
res.write("3"); | ||
res.end( | ||
`<esi:include src="http://localhost:${port}${url}/fragment_1?a=3" />`, | ||
); | ||
}); | ||
routeHandler.add(`${url}/fragment_1`, printFragment); | ||
routeHandler.add(`${url}/fragment_1?a=2`, printFragment); | ||
routeHandler.add(`${url}/fragment_1?a=3`, printFragment); | ||
const res = await makeRequest(url); | ||
expect(res.ok).toBeTruthy(); | ||
expect(checkSurrogate(res)).toBeTruthy(); | ||
expect(await res.text()).toEqual( | ||
`1\nFRAGMENT: \n2\nFRAGMENT: 2\n3FRAGMENT: 3\n`, | ||
); | ||
}); |