This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from umaar/scan-node-modules-for-asset-folders
Scan components to find asset folders to serve as static resources
- Loading branch information
Showing
12 changed files
with
1,770 additions
and
189 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
25 changes: 0 additions & 25 deletions
25
lib/middleware/routes-static/routes-static.test.govuk-frontend.unit.spec.js
This file was deleted.
Oops, something went wrong.
26 changes: 14 additions & 12 deletions
26
lib/middleware/routes-static/routes-static.test.multiple-paths.unit.spec.js
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,27 +1,29 @@ | ||
const test = require('tape') | ||
const test = require('tap').test | ||
const express = require('express') | ||
const request = require('supertest') | ||
const path = require('path') | ||
|
||
const routesStatic = require('./routes-static') | ||
|
||
const callRoutesStatic = (assetsUrlPrefix = '', paths = [], servicePath) => { | ||
const callRoutesStatic = async ({assetsUrlPrefix = '', paths = []}) => { | ||
const app = express() | ||
app.use(routesStatic.init(assetsUrlPrefix, paths, servicePath)) | ||
app.use(await routesStatic.init({ | ||
assetsUrlPrefix, | ||
staticPaths: paths | ||
})) | ||
return app | ||
} | ||
|
||
const staticPath = path.resolve(__dirname, '..', 'spec', 'static', 'static-a') | ||
const staticPathB = path.resolve(__dirname, '..', 'spec', 'static', 'static-b') | ||
|
||
test('When a file that exists within more than one static route is requested', t => { | ||
const app = callRoutesStatic('', [staticPathB, staticPath], __dirname) | ||
test('When a file that exists within more than one static route is requested', async t => { | ||
const app = await callRoutesStatic({ | ||
assetsUrlPrefix: '', | ||
paths: [staticPathB, staticPath] | ||
}) | ||
|
||
request(app) | ||
.get('/static.txt') | ||
.end((err, res) => { | ||
t.equals(err, null, 'it should not invoke an error') | ||
t.equals(res.text, 'static-b static.txt', 'it should serve the correct file') | ||
t.end() | ||
}) | ||
const {error, text} = await request(app).get('/static.txt') | ||
t.equals(error, false, 'it should not invoke an error') | ||
t.equals(text, 'static-b static.txt', 'it should serve the correct file') | ||
}) |
21 changes: 8 additions & 13 deletions
21
lib/middleware/routes-static/routes-static.test.no-paths.unit.spec.js
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,23 +1,18 @@ | ||
const test = require('tape') | ||
const test = require('tap').test | ||
const express = require('express') | ||
const request = require('supertest') | ||
|
||
const routesStatic = require('./routes-static') | ||
|
||
const callRoutesStatic = (assetsUrlPrefix = '', paths = [], servicePath = __dirname) => { | ||
const callRoutesStatic = async ({assetsUrlPrefix = '', paths = []} = {}) => { | ||
const app = express() | ||
app.use(routesStatic.init(assetsUrlPrefix, paths, servicePath)) | ||
app.use(await routesStatic.init({assetsUrlPrefix, staticPaths: paths})) | ||
return app | ||
} | ||
|
||
test('When a file that does not exist within a static route is requested', t => { | ||
const app = callRoutesStatic() | ||
|
||
request(app) | ||
.get('/static.txt') | ||
.end((err, res) => { | ||
t.equals(err, null, 'it should not invoke an error') | ||
t.equals(res.status, 404, 'it should return 404') | ||
t.end() | ||
}) | ||
test('When a file that does not exist within a static route is requested', async t => { | ||
const app = await callRoutesStatic() | ||
const {error, status} = await request(app).get('/static.txt') | ||
t.ok(error, null, 'it should invoke an error') | ||
t.equals(status, 404, 'it should return 404') | ||
}) |
55 changes: 55 additions & 0 deletions
55
lib/middleware/routes-static/routes-static.test.node-module-asset-serving.unit.spec.js
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,55 @@ | ||
const test = require('tap').test | ||
const path = require('path') | ||
const express = require('express') | ||
const request = require('supertest') | ||
|
||
const routesStatic = require('./routes-static') | ||
|
||
const callRoutesStatic = async ({assetsUrlPrefix = '', paths = [], componentDirs}) => { | ||
const app = express() | ||
app.use(await routesStatic.init({assetsUrlPrefix, staticPaths: paths, componentDirs})) | ||
return app | ||
} | ||
|
||
const staticPath = path.resolve(__dirname, '..', 'spec') | ||
|
||
test('When a file from an assets folder is requested', async t => { | ||
const componentDirs = [{ | ||
sourcePath: path.resolve(staticPath, 'static', 'static-b') | ||
}] | ||
|
||
const app = await callRoutesStatic({componentDirs}) | ||
|
||
const {error, status, headers} = await request(app).get('/image-resource.png') | ||
t.equals(error, false, 'it should not invoke an error') | ||
t.equals(status, 200, 'it should return 200') | ||
t.equals(headers['content-type'], 'image/png', 'it should return the correct mime type') | ||
}) | ||
|
||
test('When a specified node module does not have an assets folder', async t => { | ||
const componentDirs = [{ | ||
sourcePath: path.resolve(staticPath, 'static', 'static-a') | ||
}] | ||
|
||
const app = await callRoutesStatic({componentDirs}) | ||
|
||
const {status: status1} = await request(app).get('/static.txt') | ||
t.equals(status1, 404, 'it should not serve assets at the root level') | ||
|
||
const {status: status2} = await request(app).get('/assets/static.txt') | ||
t.equals(status2, 404, 'it should not serve assets at any URL') | ||
}) | ||
|
||
test('When an asset folder is present within another asset folder', async t => { | ||
const componentDirs = [{ | ||
sourcePath: path.resolve(staticPath, 'static', 'static-b') | ||
}] | ||
|
||
const app = await callRoutesStatic({componentDirs}) | ||
|
||
const {status: statusNotFound} = await request(app).get('/image-resource-2.png') | ||
t.equals(statusNotFound, 404, 'it should not be found at the top level') | ||
|
||
const {status: statusFound} = await request(app).get('/assets/image-resource-2.png') | ||
t.equals(statusFound, 200, 'it should be found by specifying the nested asset folder') | ||
}) |
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,44 +1,35 @@ | ||
const test = require('tape') | ||
const test = require('tap').test | ||
const express = require('express') | ||
const request = require('supertest') | ||
const path = require('path') | ||
|
||
const routesStatic = require('./routes-static') | ||
|
||
const callRoutesStatic = (assetsUrlPrefix = '', paths = [], servicePath) => { | ||
const callRoutesStatic = async ({assetsUrlPrefix = '', paths = []}) => { | ||
const app = express() | ||
app.use(routesStatic.init(assetsUrlPrefix, paths, servicePath)) | ||
app.use(await routesStatic.init({assetsUrlPrefix, staticPaths: paths})) | ||
return app | ||
} | ||
|
||
const staticPath = path.resolve(__dirname, '..', 'spec', 'static', 'static-a') | ||
|
||
test('When routesStatic is required ', t => { | ||
t.equal(typeof routesStatic.init, 'function', 'it should export the init method') | ||
|
||
t.end() | ||
test('When a file that exists within a static route is requested', async t => { | ||
const app = await callRoutesStatic({ | ||
assetsUrlPrefix: '', | ||
paths: [staticPath] | ||
}) | ||
const {error, text, status} = await request(app).get('/static.txt') | ||
t.equals(error, false, 'it should not invoke an error') | ||
t.equals(text, 'static-a static.txt', 'it should serve the file') | ||
t.equals(status, 200, 'it should return a successful status') | ||
}) | ||
|
||
test('When a file that exists within a static route is requested', t => { | ||
const app = callRoutesStatic('', [staticPath], __dirname) | ||
|
||
request(app) | ||
.get('/static.txt') | ||
.end((err, res) => { | ||
t.equals(err, null, 'it should not invoke an error') | ||
t.equals(res.text, 'static-a static.txt', 'it should serve the file') | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('When a url prefix is passed to routes-static', t => { | ||
const app = callRoutesStatic('/url/prefix', [staticPath], __dirname) | ||
|
||
request(app) | ||
.get('/url/prefix/static.txt') | ||
.end((err, res) => { | ||
t.equals(err, null, 'it should not invoke an error') | ||
t.equals(res.text, 'static-a static.txt', 'it should use that prefix and serve the correct file') | ||
t.end() | ||
}) | ||
test('When a url prefix is passed to routes-static', async t => { | ||
const app = await callRoutesStatic({ | ||
assetsUrlPrefix: '/url/prefix', | ||
paths: [staticPath] | ||
}) | ||
const {error, text} = await request(app).get('/url/prefix/static.txt') | ||
t.equals(error, false, 'it should not invoke an error') | ||
t.equals(text, 'static-a static.txt', 'it should use that prefix and serve the correct file') | ||
}) |
Binary file added
BIN
+67 Bytes
lib/middleware/spec/static/static-b/assets/assets/image-resource-2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Oops, something went wrong.