|
1 | 1 | import assert from 'node:assert/strict' |
| 2 | +import fs from 'node:fs/promises' |
| 3 | +import path from 'node:path' |
2 | 4 | import test from 'node:test' |
3 | 5 | import Fastify from 'fastify' |
4 | 6 | import { Reader } from 'styled-map-package' |
@@ -223,6 +225,106 @@ test('/style.json resolves style.json of fallback map when custom and online are |
223 | 225 | ) |
224 | 226 | }) |
225 | 227 |
|
| 228 | +test('custom map info endpoint not available when custom map path not provided', async (t) => { |
| 229 | + const server = setup(t) |
| 230 | + |
| 231 | + server.register(MapServerPlugin, { |
| 232 | + defaultOnlineStyleUrl: DEFAULT_ONLINE_STYLE_URL, |
| 233 | + fallbackMapPath: DEFAULT_FALLBACK_MAP_FILE_PATH, |
| 234 | + }) |
| 235 | + |
| 236 | + const address = await server.listen() |
| 237 | + |
| 238 | + mockAgent.enableNetConnect(new URL(address).host) |
| 239 | + |
| 240 | + const response = await server.inject({ |
| 241 | + method: 'GET', |
| 242 | + url: `/${CUSTOM_MAP_PREFIX}/info`, |
| 243 | + }) |
| 244 | + |
| 245 | + assert.equal(response.statusCode, 404) |
| 246 | +}) |
| 247 | + |
| 248 | +test('custom map info endpoint returns not found error when custom map does not exist', async (t) => { |
| 249 | + const server = setup(t) |
| 250 | + |
| 251 | + const nonExistentFile = |
| 252 | + path.parse(SAMPLE_SMP_FIXTURE_PATH).dir + '/does/not/exist.smp' |
| 253 | + |
| 254 | + server.register(MapServerPlugin, { |
| 255 | + customMapPath: nonExistentFile, |
| 256 | + defaultOnlineStyleUrl: DEFAULT_ONLINE_STYLE_URL, |
| 257 | + fallbackMapPath: DEFAULT_FALLBACK_MAP_FILE_PATH, |
| 258 | + }) |
| 259 | + |
| 260 | + const address = await server.listen() |
| 261 | + |
| 262 | + mockAgent.enableNetConnect(new URL(address).host) |
| 263 | + |
| 264 | + const response = await server.inject({ |
| 265 | + method: 'GET', |
| 266 | + url: `/${CUSTOM_MAP_PREFIX}/info`, |
| 267 | + }) |
| 268 | + |
| 269 | + assert.equal(response.statusCode, 404) |
| 270 | + assert.match(response.json().error, /Not Found/) |
| 271 | +}) |
| 272 | + |
| 273 | +test('custom map info endpoint returns server error when custom map is invalid', async (t) => { |
| 274 | + const server = setup(t) |
| 275 | + |
| 276 | + const invalidFile = new URL(import.meta.url).pathname |
| 277 | + |
| 278 | + server.register(MapServerPlugin, { |
| 279 | + customMapPath: invalidFile, |
| 280 | + defaultOnlineStyleUrl: DEFAULT_ONLINE_STYLE_URL, |
| 281 | + fallbackMapPath: DEFAULT_FALLBACK_MAP_FILE_PATH, |
| 282 | + }) |
| 283 | + |
| 284 | + const address = await server.listen() |
| 285 | + |
| 286 | + mockAgent.enableNetConnect(new URL(address).host) |
| 287 | + |
| 288 | + const response = await server.inject({ |
| 289 | + method: 'GET', |
| 290 | + url: `/${CUSTOM_MAP_PREFIX}/info`, |
| 291 | + }) |
| 292 | + |
| 293 | + assert.equal(response.statusCode, 500) |
| 294 | + assert.match(response.json().error, /Internal Server Error/) |
| 295 | +}) |
| 296 | + |
| 297 | +test('custom map info endpoint returns expected info when valid custom map is available', async (t) => { |
| 298 | + const server = setup(t) |
| 299 | + |
| 300 | + const smpStats = await fs.stat(SAMPLE_SMP_FIXTURE_PATH) |
| 301 | + |
| 302 | + server.register(MapServerPlugin, { |
| 303 | + customMapPath: SAMPLE_SMP_FIXTURE_PATH, |
| 304 | + defaultOnlineStyleUrl: DEFAULT_ONLINE_STYLE_URL, |
| 305 | + fallbackMapPath: DEFAULT_FALLBACK_MAP_FILE_PATH, |
| 306 | + }) |
| 307 | + |
| 308 | + const address = await server.listen() |
| 309 | + |
| 310 | + mockAgent.enableNetConnect(new URL(address).host) |
| 311 | + |
| 312 | + const response = await server.inject({ |
| 313 | + method: 'GET', |
| 314 | + url: `/${CUSTOM_MAP_PREFIX}/info`, |
| 315 | + }) |
| 316 | + |
| 317 | + assert.equal(response.statusCode, 200) |
| 318 | + |
| 319 | + const info = response.json() |
| 320 | + |
| 321 | + assert.deepEqual(info, { |
| 322 | + created: smpStats.ctime.toISOString(), |
| 323 | + size: smpStats.size, |
| 324 | + name: 'MapLibre', |
| 325 | + }) |
| 326 | +}) |
| 327 | + |
226 | 328 | /** |
227 | 329 | * @param {import('node:test').TestContext} t |
228 | 330 | */ |
|
0 commit comments