|
| 1 | +const LegacyResourceMapper = require('../../lib/legacy-resource-mapper') |
| 2 | +const chai = require('chai') |
| 3 | +const { expect } = chai |
| 4 | +chai.use(require('chai-as-promised')) |
| 5 | + |
| 6 | +const rootUrl = 'http://localhost/' |
| 7 | +const rootPath = '/var/www/folder/' |
| 8 | + |
| 9 | +const itMapsUrl = asserter(mapsUrl) |
| 10 | +const itMapsFile = asserter(mapsFile) |
| 11 | + |
| 12 | +describe('LegacyResourceMapper', () => { |
| 13 | + describe('A LegacyResourceMapper instance for a single-host setup', () => { |
| 14 | + const mapper = new LegacyResourceMapper({ rootUrl, rootPath }) |
| 15 | + |
| 16 | + // adapted PUT base cases from https://www.w3.org/DesignIssues/HTTPFilenameMapping.html |
| 17 | + |
| 18 | + itMapsUrl(mapper, 'a URL with an extension that matches the content type', |
| 19 | + { |
| 20 | + url: 'http://localhost/space/foo.html', |
| 21 | + contentType: 'text/html', |
| 22 | + createIfNotExists: true |
| 23 | + }, |
| 24 | + { |
| 25 | + path: `${rootPath}space/foo.html`, |
| 26 | + contentType: 'text/html' |
| 27 | + }) |
| 28 | + |
| 29 | + // Additional PUT cases |
| 30 | + |
| 31 | + itMapsUrl(mapper, 'a URL without content type', |
| 32 | + { |
| 33 | + url: 'http://localhost/space/foo.html', |
| 34 | + createIfNotExists: true |
| 35 | + }, |
| 36 | + { |
| 37 | + path: `${rootPath}space/foo.html`, |
| 38 | + contentType: 'text/html' |
| 39 | + }) |
| 40 | + |
| 41 | + itMapsUrl(mapper, 'a URL with an alternative extension that matches the content type', |
| 42 | + { |
| 43 | + url: 'http://localhost/space/foo.jpeg', |
| 44 | + contentType: 'image/jpeg', |
| 45 | + createIfNotExists: true |
| 46 | + }, |
| 47 | + { |
| 48 | + path: `${rootPath}space/foo.jpeg`, |
| 49 | + contentType: 'image/jpeg' |
| 50 | + }) |
| 51 | + |
| 52 | + itMapsUrl(mapper, 'a URL with an uppercase extension that matches the content type', |
| 53 | + { |
| 54 | + url: 'http://localhost/space/foo.JPG', |
| 55 | + contentType: 'image/jpeg', |
| 56 | + createIfNotExists: true |
| 57 | + }, |
| 58 | + { |
| 59 | + path: `${rootPath}space/foo.JPG`, |
| 60 | + contentType: 'image/jpeg' |
| 61 | + }) |
| 62 | + |
| 63 | + itMapsUrl(mapper, 'a URL with a mixed-case extension that matches the content type', |
| 64 | + { |
| 65 | + url: 'http://localhost/space/foo.jPeG', |
| 66 | + contentType: 'image/jpeg', |
| 67 | + createIfNotExists: true |
| 68 | + }, |
| 69 | + { |
| 70 | + path: `${rootPath}space/foo.jPeG`, |
| 71 | + contentType: 'image/jpeg' |
| 72 | + }) |
| 73 | + |
| 74 | + // GET/HEAD/POST/DELETE/PATCH base cases |
| 75 | + |
| 76 | + itMapsUrl(mapper, 'a URL of an existing file with extension', |
| 77 | + { |
| 78 | + url: 'http://localhost/space/foo.html' |
| 79 | + }, |
| 80 | + { |
| 81 | + path: `${rootPath}space/foo.html`, |
| 82 | + contentType: 'text/html' |
| 83 | + }) |
| 84 | + |
| 85 | + itMapsUrl(mapper, 'an extensionless URL of an existing file', |
| 86 | + { |
| 87 | + url: 'http://localhost/space/foo' |
| 88 | + }, |
| 89 | + { |
| 90 | + path: `${rootPath}space/foo`, |
| 91 | + contentType: 'text/turtle' |
| 92 | + }) |
| 93 | + |
| 94 | + itMapsUrl(mapper, 'a URL of an existing file with encoded characters', |
| 95 | + { |
| 96 | + url: 'http://localhost/space/foo%20bar%20bar.html' |
| 97 | + }, |
| 98 | + { |
| 99 | + path: `${rootPath}space/foo bar bar.html`, |
| 100 | + contentType: 'text/html' |
| 101 | + }) |
| 102 | + |
| 103 | + itMapsUrl(mapper, 'a URL of a new file with encoded characters', |
| 104 | + { |
| 105 | + url: 'http://localhost/space%2Ffoo%20bar%20bar.html', |
| 106 | + contentType: 'text/html', |
| 107 | + createIfNotExists: true |
| 108 | + }, |
| 109 | + { |
| 110 | + path: `${rootPath}space/foo bar bar.html`, |
| 111 | + contentType: 'text/html' |
| 112 | + }) |
| 113 | + |
| 114 | + // Security cases |
| 115 | + |
| 116 | + itMapsUrl(mapper, 'a URL with a /.. path segment', |
| 117 | + { |
| 118 | + url: 'http://localhost/space/../bar' |
| 119 | + }, |
| 120 | + new Error('Disallowed /.. segment in URL')) |
| 121 | + |
| 122 | + itMapsUrl(mapper, 'a URL with an encoded /.. path segment', |
| 123 | + { |
| 124 | + url: 'http://localhost/space%2F..%2Fbar' |
| 125 | + }, |
| 126 | + new Error('Disallowed /.. segment in URL')) |
| 127 | + |
| 128 | + // File to URL mapping |
| 129 | + |
| 130 | + itMapsFile(mapper, 'an HTML file', |
| 131 | + { path: `${rootPath}space/foo.html` }, |
| 132 | + { |
| 133 | + url: 'http://localhost/space/foo.html', |
| 134 | + contentType: 'text/html' |
| 135 | + }) |
| 136 | + |
| 137 | + itMapsFile(mapper, 'a Turtle file', |
| 138 | + { path: `${rootPath}space/foo.ttl` }, |
| 139 | + { |
| 140 | + url: 'http://localhost/space/foo.ttl', |
| 141 | + contentType: 'text/turtle' |
| 142 | + }) |
| 143 | + |
| 144 | + itMapsFile(mapper, 'a file with an uppercase extension', |
| 145 | + { path: `${rootPath}space/foo.HTML` }, |
| 146 | + { |
| 147 | + url: 'http://localhost/space/foo.HTML', |
| 148 | + contentType: 'text/html' |
| 149 | + }) |
| 150 | + |
| 151 | + itMapsFile(mapper, 'a file with a mixed-case extension', |
| 152 | + { path: `${rootPath}space/foo.HtMl` }, |
| 153 | + { |
| 154 | + url: 'http://localhost/space/foo.HtMl', |
| 155 | + contentType: 'text/html' |
| 156 | + }) |
| 157 | + |
| 158 | + itMapsFile(mapper, 'a file with disallowed IRI characters', |
| 159 | + { path: `${rootPath}space/foo bar bar.html` }, |
| 160 | + { |
| 161 | + url: 'http://localhost/space/foo%20bar%20bar.html', |
| 162 | + contentType: 'text/html' |
| 163 | + }) |
| 164 | + }) |
| 165 | + |
| 166 | + describe('A LegacyResourceMapper instance for a multi-host setup', () => { |
| 167 | + const mapper = new LegacyResourceMapper({ rootUrl, rootPath, includeHost: true }) |
| 168 | + |
| 169 | + itMapsUrl(mapper, 'a URL with a host', |
| 170 | + { |
| 171 | + url: 'http://example.org/space/foo.html', |
| 172 | + contentType: 'text/html', |
| 173 | + createIfNotExists: true |
| 174 | + }, |
| 175 | + { |
| 176 | + path: `${rootPath}example.org/space/foo.html`, |
| 177 | + contentType: 'text/html' |
| 178 | + }) |
| 179 | + |
| 180 | + itMapsUrl(mapper, 'a URL with a host with a port', |
| 181 | + { |
| 182 | + url: 'http://example.org:3000/space/foo.html', |
| 183 | + contentType: 'text/html', |
| 184 | + createIfNotExists: true |
| 185 | + }, |
| 186 | + { |
| 187 | + path: `${rootPath}example.org/space/foo.html`, |
| 188 | + contentType: 'text/html' |
| 189 | + }) |
| 190 | + |
| 191 | + itMapsFile(mapper, 'a file on a host', |
| 192 | + { |
| 193 | + path: `${rootPath}space/foo.html`, |
| 194 | + hostname: 'example.org' |
| 195 | + }, |
| 196 | + { |
| 197 | + url: 'http://example.org/space/foo.html', |
| 198 | + contentType: 'text/html' |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + describe('A LegacyResourceMapper instance for a multi-host setup with a subfolder root URL', () => { |
| 203 | + const rootUrl = 'http://localhost/foo/bar/' |
| 204 | + const mapper = new LegacyResourceMapper({ rootUrl, rootPath, includeHost: true }) |
| 205 | + |
| 206 | + itMapsFile(mapper, 'a file on a host', |
| 207 | + { |
| 208 | + path: `${rootPath}space/foo.html`, |
| 209 | + hostname: 'example.org' |
| 210 | + }, |
| 211 | + { |
| 212 | + url: 'http://example.org/foo/bar/space/foo.html', |
| 213 | + contentType: 'text/html' |
| 214 | + }) |
| 215 | + }) |
| 216 | +}) |
| 217 | + |
| 218 | +function asserter (assert) { |
| 219 | + const f = (...args) => assert(it, ...args) |
| 220 | + f.skip = (...args) => assert(it.skip, ...args) |
| 221 | + f.only = (...args) => assert(it.only, ...args) |
| 222 | + return f |
| 223 | +} |
| 224 | + |
| 225 | +function mapsUrl (it, mapper, label, options, expected) { |
| 226 | + // Set up positive test |
| 227 | + if (!(expected instanceof Error)) { |
| 228 | + it(`maps ${label}`, async () => { |
| 229 | + const actual = await mapper.mapUrlToFile(options) |
| 230 | + expect(actual).to.deep.equal(expected) |
| 231 | + }) |
| 232 | + // Set up error test |
| 233 | + } else { |
| 234 | + it(`does not map ${label}`, async () => { |
| 235 | + const actual = mapper.mapUrlToFile(options) |
| 236 | + await expect(actual).to.be.rejectedWith(expected.message) |
| 237 | + }) |
| 238 | + } |
| 239 | +} |
| 240 | + |
| 241 | +function mapsFile (it, mapper, label, options, expected) { |
| 242 | + it(`maps ${label}`, async () => { |
| 243 | + const actual = await mapper.mapFileToUrl(options) |
| 244 | + expect(actual).to.deep.equal(expected) |
| 245 | + }) |
| 246 | +} |
0 commit comments