Skip to content

Commit 63b5d69

Browse files
committed
chore: move downloadFile util test
1 parent 309be60 commit 63b5d69

File tree

2 files changed

+44
-33
lines changed

2 files changed

+44
-33
lines changed

test/index.spec.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import process from "process"
2626
import os from "os"
2727
import cpy from "cpy"
2828
import { dir as getTmpDir } from "tmp-promise"
29-
import { downloadFile } from "../packages/runtime/src/templates/handlerUtils"
3029
import { getExtendedApiRouteConfigs } from "../packages/runtime/src/helpers/functions"
3130
// @ts-expect-error - TODO: Convert runtime export to ES6
3231
import nextRuntimeFactory from "../packages/runtime/src"
@@ -1087,38 +1086,6 @@ describe('onPostBuild', () => {
10871086
})
10881087

10891088
describe('function helpers', () => {
1090-
it('downloadFile can download a file', async () => {
1091-
const url =
1092-
'https://raw.githubusercontent.com/netlify/next-runtime/c2668af24a78eb69b33222913f44c1900a3bce23/manifest.yml'
1093-
const tmpFile = join(os.tmpdir(), 'next-test', 'downloadfile.txt')
1094-
await ensureDir(path.dirname(tmpFile))
1095-
await downloadFile(url, tmpFile)
1096-
expect(existsSync(tmpFile)).toBeTruthy()
1097-
expect(readFileSync(tmpFile, 'utf8')).toMatchInlineSnapshot(`
1098-
"name: netlify-plugin-nextjs-experimental
1099-
"
1100-
`)
1101-
await unlink(tmpFile)
1102-
})
1103-
1104-
it('downloadFile throws on bad domain', async () => {
1105-
const url = 'https://nonexistentdomain.example'
1106-
const tmpFile = join(os.tmpdir(), 'next-test', 'downloadfile.txt')
1107-
await ensureDir(path.dirname(tmpFile))
1108-
await expect(downloadFile(url, tmpFile)).rejects.toThrowErrorMatchingInlineSnapshot(
1109-
`"getaddrinfo ENOTFOUND nonexistentdomain.example"`,
1110-
)
1111-
})
1112-
1113-
it('downloadFile throws on 404', async () => {
1114-
const url = 'https://example.com/nonexistentfile'
1115-
const tmpFile = join(os.tmpdir(), 'next-test', 'downloadfile.txt')
1116-
await ensureDir(path.dirname(tmpFile))
1117-
await expect(downloadFile(url, tmpFile)).rejects.toThrowError(
1118-
'Failed to download https://example.com/nonexistentfile: 404 Not Found',
1119-
)
1120-
})
1121-
11221089
describe('config', () => {
11231090
describe('dependency tracing', () => {
11241091
it('extracts a list of all dependencies', async () => {

test/templates/handlerUtils.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ import {
33
unlocalizeRoute,
44
localizeRoute,
55
localizeDataRoute,
6+
downloadFile,
67
} from '../../packages/runtime/src/templates/handlerUtils'
8+
import { join } from "pathe"
9+
import os from "os"
10+
import path from "path"
11+
import {
12+
unlink,
13+
existsSync,
14+
readFileSync,
15+
ensureDir,
16+
} from "fs-extra"
717

818
describe('normalizeRoute', () => {
919
it('removes a trailing slash from a route', () => {
@@ -85,3 +95,37 @@ describe('localizeDataRoute', () => {
8595
expect(localizeDataRoute('/foo.rsc', '/foo')).toEqual('/foo.rsc')
8696
})
8797
})
98+
99+
describe('downloadFile', () => {
100+
it('can download a file', async () => {
101+
const url =
102+
'https://raw.githubusercontent.com/netlify/next-runtime/c2668af24a78eb69b33222913f44c1900a3bce23/manifest.yml'
103+
const tmpFile = join(os.tmpdir(), 'next-test', 'downloadfile.txt')
104+
await ensureDir(path.dirname(tmpFile))
105+
await downloadFile(url, tmpFile)
106+
expect(existsSync(tmpFile)).toBeTruthy()
107+
expect(readFileSync(tmpFile, 'utf8')).toMatchInlineSnapshot(`
108+
"name: netlify-plugin-nextjs-experimental
109+
"
110+
`)
111+
await unlink(tmpFile)
112+
})
113+
114+
it('throws on bad domain', async () => {
115+
const url = 'https://nonexistentdomain.example'
116+
const tmpFile = join(os.tmpdir(), 'next-test', 'downloadfile.txt')
117+
await ensureDir(path.dirname(tmpFile))
118+
await expect(downloadFile(url, tmpFile)).rejects.toThrowErrorMatchingInlineSnapshot(
119+
`"getaddrinfo ENOTFOUND nonexistentdomain.example"`,
120+
)
121+
})
122+
123+
it('throws on 404', async () => {
124+
const url = 'https://example.com/nonexistentfile'
125+
const tmpFile = join(os.tmpdir(), 'next-test', 'downloadfile.txt')
126+
await ensureDir(path.dirname(tmpFile))
127+
await expect(downloadFile(url, tmpFile)).rejects.toThrowError(
128+
'Failed to download https://example.com/nonexistentfile: 404 Not Found',
129+
)
130+
})
131+
})

0 commit comments

Comments
 (0)