-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,3 +119,4 @@ dist | |
.pnp.* | ||
|
||
tmp/ | ||
test/download |
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
Empty file.
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,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>hello world</h1> | ||
</body> | ||
</html> |
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 @@ | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
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 |
---|---|---|
@@ -1 +1 @@ | ||
jest.setTimeout(20000); | ||
jest.setTimeout(25000); |
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,79 @@ | ||
// Import Third-party Dependencies | ||
import { FastifyInstance } from "fastify"; | ||
|
||
// Import Node.js Dependencies | ||
import { createWriteStream, createReadStream, existsSync, promises as fs } from "fs"; | ||
import path from "path"; | ||
import { pipeline } from "stream/promises"; | ||
|
||
// Import Internal Dependencies | ||
import * as httpie from "../src/index"; | ||
import { createServer } from "./server/index"; | ||
|
||
// CONSTANTS | ||
const kGithubURL = new URL("https://github.com/"); | ||
const kFixturesPath = path.join(__dirname, "fixtures"); | ||
const kDownloadPath = path.join(__dirname, "download"); | ||
|
||
let httpServer: FastifyInstance; | ||
beforeAll(async() => { | ||
httpServer = await createServer("stream", 1338); | ||
await fs.mkdir(kDownloadPath, { recursive: true }); | ||
}); | ||
|
||
afterAll(async() => { | ||
await httpServer.close(); | ||
await fs.rm(kDownloadPath, { force: true, recursive: true }); | ||
}); | ||
|
||
describe("stream", () => { | ||
it("should fetch a .tar.gz of a given github repository", async() => { | ||
const fileDestination = path.join(kDownloadPath, "i18n-main.tar.gz"); | ||
const repositoryURL = new URL("NodeSecure/i18n/archive/main.tar.gz", kGithubURL); | ||
|
||
await httpie.stream("GET", repositoryURL, { | ||
headers: { | ||
"User-Agent": "httpie", | ||
"Accept-Encoding": "gzip, deflate" | ||
}, | ||
maxRedirections: 1 | ||
})(createWriteStream(fileDestination)); | ||
|
||
expect(existsSync(fileDestination)).toStrictEqual(true); | ||
}); | ||
|
||
it("should fetch the HTML home from the local fastify server", async() => { | ||
const fileDestination = path.join(kDownloadPath, "home.html"); | ||
|
||
await httpie.stream("GET", "/stream/home")(createWriteStream(fileDestination)); | ||
|
||
expect(existsSync(fileDestination)).toStrictEqual(true); | ||
const [contentA, contentB] = await Promise.all([ | ||
fs.readFile(path.join(kFixturesPath, "home.html"), "utf-8"), | ||
fs.readFile(path.join(kDownloadPath, "home.html"), "utf-8") | ||
]); | ||
|
||
expect(contentA).toStrictEqual(contentB); | ||
}); | ||
}); | ||
|
||
describe("pipeline", () => { | ||
it("should be able to pipeline (duplex stream)", async() => { | ||
const fixtureLocation = path.join(kFixturesPath, "lorem.txt"); | ||
const fileDestination = path.join(kDownloadPath, "lorem.txt"); | ||
|
||
await pipeline( | ||
createReadStream(fixtureLocation), | ||
httpie.pipeline("GET", "/stream/pipeline"), | ||
createWriteStream(fileDestination) | ||
); | ||
|
||
expect(existsSync(fileDestination)).toStrictEqual(true); | ||
const [contentA, contentB] = await Promise.all([ | ||
fs.readFile(fixtureLocation, "utf-8"), | ||
fs.readFile(fileDestination, "utf-8") | ||
]); | ||
|
||
expect(contentA.toUpperCase()).toStrictEqual(contentB); | ||
}); | ||
}); |