-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathendpoint.spec.ts
More file actions
470 lines (442 loc) · 15.9 KB
/
Copy pathendpoint.spec.ts
File metadata and controls
470 lines (442 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// @ts-expect-error ts-migrate(7016)
import capabilities130 from '../../fixtures/wms/capabilities-brgm-1-3-0.xml';
// @ts-expect-error ts-migrate(7016)
import capabilitiesStates from '../../fixtures/wms/capabilities-states-1-3-0.xml';
// @ts-expect-error ts-migrate(7016)
import exceptionReportWfs from '../../fixtures/wms/service-exception-report-wfs.xml';
// @ts-expect-error ts-migrate(7016)
import describeLayerResponse from '../../fixtures/wms/describelayer-response.xml';
import WmsEndpoint from './endpoint.js';
import { useCache } from '../shared/cache.js';
import { EndpointError, ServiceExceptionError } from '../shared/errors.js';
jest.mock('../shared/cache', () => ({
useCache: jest.fn((factory) => factory()),
}));
describe('WmsEndpoint', () => {
let endpoint: WmsEndpoint;
beforeEach(() => {
jest.clearAllMocks();
globalThis.fetchPreHandler = () => {};
globalThis.fetchResponseFactory = () => capabilities130;
endpoint = new WmsEndpoint(
'https://my.test.service/ogc/wms?service=wms&request=GetMap&aa=bb'
);
});
it('makes a getcapabilities request', async () => {
await endpoint.isReady();
expect(globalThis.fetch).toHaveBeenCalledWith(
'https://my.test.service/ogc/wms?aa=bb&SERVICE=WMS&REQUEST=GetCapabilities',
{ method: 'GET' }
);
});
describe('caching', () => {
beforeEach(async () => {
await endpoint.isReady();
});
it('uses cache once', () => {
expect(useCache).toHaveBeenCalledTimes(1);
});
it('stores the parsed capabilities in cache', async () => {
await expect(
(useCache as any).mock.results[0].value
).resolves.toMatchObject({
info: {
title: 'GéoServices : géologie, hydrogéologie et gravimétrie',
},
});
});
});
describe('#isReady', () => {
it('resolves with the endpoint object', async () => {
await expect(endpoint.isReady()).resolves.toEqual(endpoint);
});
describe('CORS error handling', () => {
beforeEach(() => {
globalThis.fetchPreHandler = (_url, options) => {
if (options?.method === 'HEAD') return 'ok!';
throw new Error('CORS problem');
};
endpoint = new WmsEndpoint('https://my.test.service/ogc/wms');
});
it('rejects with a relevant error', async () => {
const error = (await endpoint
.isReady()
.catch((e) => e)) as EndpointError;
expect(error).toBeInstanceOf(EndpointError);
expect(error.message).toBe(
'The document could not be fetched due to CORS limitations'
);
expect(error.httpStatus).toBe(0);
expect(error.isCrossOriginRelated).toBe(true);
});
});
describe('endpoint error handling', () => {
beforeEach(() => {
globalThis.fetchPreHandler = () => {
throw new TypeError('other kind of problem');
};
endpoint = new WmsEndpoint('https://my.test.service/ogc/wms');
});
it('rejects with a relevant error', async () => {
const error = (await endpoint
.isReady()
.catch((e) => e)) as EndpointError;
expect(error).toBeInstanceOf(EndpointError);
expect(error.message).toBe(
'Fetching the document failed either due to network errors or unreachable host, error is: other kind of problem'
);
expect(error.httpStatus).toBe(0);
expect(error.isCrossOriginRelated).toBe(false);
});
});
describe('http error handling', () => {
beforeEach(() => {
globalThis.fetchPreHandler = () => ({
ok: false,
text: () => Promise.resolve('something broke in the server'),
status: 500,
statusText: 'Internal Server Error',
clone: function () {
return this;
},
});
endpoint = new WmsEndpoint('https://my.test.service/ogc/wms');
});
it('rejects with a relevant error', async () => {
const error = (await endpoint
.isReady()
.catch((e) => e)) as EndpointError;
expect(error).toBeInstanceOf(EndpointError);
expect(error.message).toBe(
'Received an error with code 500: something broke in the server'
);
expect(error.httpStatus).toBe(500);
expect(error.isCrossOriginRelated).toBe(false);
});
});
describe('service exception handling', () => {
beforeEach(() => {
globalThis.fetchResponseFactory = () => exceptionReportWfs;
endpoint = new WmsEndpoint('https://my.test.service/ogc/wms');
});
it('rejects when the endpoint returns an exception report', async () => {
const error = (await endpoint
.isReady()
.catch((e) => e)) as ServiceExceptionError;
expect(error).toBeInstanceOf(ServiceExceptionError);
expect(error.message).toBe(
'msWMSGetCapabilities(): WMS server error. WMS request not enabled. Check wms/ows_enable_request settings.'
);
expect(error.requestUrl).toBe(
'https://my.test.service/ogc/wms?SERVICE=WMS&REQUEST=GetCapabilities'
);
expect(error.code).toBe('');
expect(error.locator).toBe('');
});
});
});
describe('#getVersion', () => {
it('returns the correct version', async () => {
await endpoint.isReady();
expect(endpoint.getVersion()).toBe('1.3.0');
});
});
describe('#getLayers', () => {
it('returns a summary of layers', async () => {
await endpoint.isReady();
expect(endpoint.getLayers()).toEqual([
{
abstract:
"Ensemble des services d'accès aux données sur la géologie, l'hydrogéologie et la gravimétrie, diffusées par le BRGM",
children: [
{
abstract: 'Cartes géologiques',
children: [
{
abstract:
'BD Scan-Million-Géol est la base de données géoréférencées de la carte géologique image à 1/1 000 000',
name: 'SCAN_F_GEOL1M',
title: 'Carte géologique image de la France au million',
},
{
abstract:
'BD Scan-Géol-250 est la base de données géoréférencées des cartes géologiques image à 1/250 000. Utilisation scientifique, technique, pédagogique',
name: 'SCAN_F_GEOL250',
title: 'Carte géologique image de la France au 1/250000',
},
{
abstract:
"BD Scan-Géol-50 est la base de données géoréférencées des cartes géologiques 'papier' à 1/50 000",
name: 'SCAN_D_GEOL50',
title: 'Carte géologique image de la France au 1/50 000e',
children: [
{
abstract: '',
name: 'INHERIT_SCALE',
title: 'Inherited scale denominators',
},
],
},
{
abstract: '',
name: 'INHERIT_BBOX',
title: 'Inherited bounding boxes',
},
],
name: 'GEOLOGIE',
title: 'Cartes géologiques',
},
],
name: 'GEOSERVICES_GEOLOGIE',
title: 'GéoServices : géologie, hydrogéologie et gravimétrie',
},
]);
});
});
describe('#getFlattenedLayers', () => {
it('returns a list of layers', async () => {
await endpoint.isReady();
expect(endpoint.getFlattenedLayers()).toEqual([
{
abstract:
"Ensemble des services d'accès aux données sur la géologie, l'hydrogéologie et la gravimétrie, diffusées par le BRGM",
name: 'GEOSERVICES_GEOLOGIE',
title: 'GéoServices : géologie, hydrogéologie et gravimétrie',
},
{
abstract: 'Cartes géologiques',
name: 'GEOLOGIE',
title: 'Cartes géologiques',
},
{
abstract:
'BD Scan-Million-Géol est la base de données géoréférencées de la carte géologique image à 1/1 000 000',
name: 'SCAN_F_GEOL1M',
title: 'Carte géologique image de la France au million',
},
{
abstract:
'BD Scan-Géol-250 est la base de données géoréférencées des cartes géologiques image à 1/250 000. Utilisation scientifique, technique, pédagogique',
name: 'SCAN_F_GEOL250',
title: 'Carte géologique image de la France au 1/250000',
},
{
abstract:
"BD Scan-Géol-50 est la base de données géoréférencées des cartes géologiques 'papier' à 1/50 000",
name: 'SCAN_D_GEOL50',
title: 'Carte géologique image de la France au 1/50 000e',
},
{
abstract: '',
name: 'INHERIT_SCALE',
title: 'Inherited scale denominators',
},
{
abstract: '',
name: 'INHERIT_BBOX',
title: 'Inherited bounding boxes',
},
]);
});
});
describe('#getLayerByName', () => {
it('returns detailed info on a layer', async () => {
await endpoint.isReady();
expect(endpoint.getLayerByName('GEOLOGIE')).toEqual({
abstract: 'Cartes géologiques',
attribution: {
logoUrl: 'http://mapsref.brgm.fr/legendes/brgm_logo.png',
title: 'Brgm',
url: 'http://www.brgm.fr/',
},
availableCrs: [
'EPSG:4326',
'CRS:84',
'EPSG:3857',
'EPSG:4171',
'EPSG:2154',
],
boundingBoxes: {
'CRS:84': [-180, -90, 180, 90],
'EPSG:2154': [-1e15, -1e15, 1e15, 1e15],
'EPSG:3857': [-1e15, -1e15, 1e15, 1e15],
'EPSG:4171': [-180, -90, 180, 90],
'EPSG:4326': [-180, -90, 180, 90],
},
keywords: [],
name: 'GEOLOGIE',
queryable: false,
opaque: false,
styles: [
{
name: 'default',
title: 'default',
},
],
title: 'Cartes géologiques',
children: expect.any(Array),
});
});
});
describe('#getSingleLayerName', () => {
it('returns null (multiple feature types)', async () => {
await endpoint.isReady();
expect(endpoint.getSingleLayerName()).toBe(null);
});
describe('with a single feature type', () => {
beforeEach(() => {
globalThis.fetchResponseFactory = () => capabilitiesStates;
endpoint = new WmsEndpoint(
'https://my.test.service/ogc/wms?service=wfs&request=DescribeFeatureType'
);
});
it('returns the single feature type name', async () => {
await endpoint.isReady();
expect(endpoint.getSingleLayerName()).toBe('usa:states');
});
});
});
describe('#getServiceInfo', () => {
it('returns service info', async () => {
await endpoint.isReady();
expect(endpoint.getServiceInfo()).toEqual({
abstract:
"Ensemble des services d'accès aux données sur la géologie, l'hydrogéologie et la gravimétrie, diffusées par le BRGM",
constraints: 'None',
fees: 'no conditions apply',
name: 'WMS',
title: 'GéoServices : géologie, hydrogéologie et gravimétrie',
outputFormats: [
'image/png',
'image/gif',
'image/jpeg',
'image/ecw',
'image/tiff',
'image/png; mode=8bit',
'application/x-pdf',
'image/svg+xml',
],
infoFormats: ['text/plain', 'application/vnd.ogc.gml'],
exceptionFormats: ['XML', 'INIMAGE', 'BLANK'],
keywords: [
'Géologie',
'BRGM',
'INSPIRE:ViewService',
'infoMapAccessService',
'WMS 1.1.1',
'WMS 1.3.0',
'SLD 1.1.0',
],
provider: {
contact: {
name: 'Support BRGM',
organization: 'BRGM',
position: 'pointOfContact',
phone: '+33(0)2 38 64 34 34',
fax: '+33(0)2 38 64 35 18',
address: {
deliveryPoint: '3, Avenue Claude Guillemin, BP36009',
city: 'Orléans',
administrativeArea: 'Centre',
postalCode: '45060',
country: 'France',
},
email: 'contact-brgm@brgm.fr',
},
},
});
});
});
describe('#generateGetMapUrl', () => {
it('generates a correct URL', async () => {
await endpoint.isReady();
expect(
endpoint.getMapUrl(['layer1', 'layer2'], {
widthPx: 100,
heightPx: 200,
crs: 'EPSG:4326',
extent: [10, 20, 100, 200],
outputFormat: 'image/png',
})
).toBe(
'http://geoservices.brgm.fr/geologie?language=fre&SERVICE=WMS&REQUEST=GetMap&VERSION=1.3.0&LAYERS=layer1%2Clayer2&STYLES=&WIDTH=100&HEIGHT=200&FORMAT=image%2Fpng&CRS=EPSG%3A4326&BBOX=10%2C20%2C100%2C200'
);
});
});
describe('#getCapabilitiesUrl', () => {
it.skip('returns the URL used for the request before the capabilities are retrieved', async () => {
expect(endpoint.getCapabilitiesUrl()).toBe(
'https://my.test.service/ogc/wms?aa=bb&SERVICE=WMS&REQUEST=GetCapabilities'
);
await endpoint.isReady();
});
it('returns the self-reported URL after the capabilities are retrieved', async () => {
await endpoint.isReady();
expect(endpoint.getCapabilitiesUrl()).toBe(
'http://geoservices.brgm.fr/geologie?language=fre&SERVICE=WMS&REQUEST=GetCapabilities'
);
});
});
describe('#getOperationUrl', () => {
it.skip('returns NULL before the document is loaded', async () => {
expect(endpoint.getOperationUrl('GetMap')).toBeNull();
await endpoint.isReady();
});
it('returns undefined for a non-existant operation', async () => {
await endpoint.isReady();
expect(endpoint.getOperationUrl('foo')).toBeUndefined();
});
it('returns the correct URL for an existant operation', async () => {
await endpoint.isReady();
expect(endpoint.getOperationUrl('GetMap')).toBe(
'http://geoservices.brgm.fr/geologie?language=fre&'
);
});
});
describe('#describeLayer', () => {
beforeEach(() => {
globalThis.fetchResponseFactory = (url) => {
if (url.indexOf('DescribeLayer') > -1) return describeLayerResponse;
return capabilities130;
};
endpoint = new WmsEndpoint(
'https://my.test.service/ogc/wms?service=wms&request=GetMap&aa=bb'
);
});
it('returns the layer description for a vector layer', async () => {
await endpoint.isReady();
const result = await endpoint.describeLayer(
'my_workspace:my_vector_layer'
);
expect(result).toEqual({
layerName: 'my_workspace:my_vector_layer',
owsType: 'WFS',
owsUrl: 'https://my-server.com/wfs?',
typeName: 'my_workspace:my_vector_layer',
});
});
it('returns the layer description for a raster layer', async () => {
await endpoint.isReady();
const result = await endpoint.describeLayer(
'my_workspace:my_raster_layer'
);
expect(result).toEqual({
layerName: 'my_workspace:my_raster_layer',
owsType: 'WCS',
owsUrl: 'https://my-server.com/wcs?',
typeName: 'my_workspace:my_raster_layer',
});
});
it('returns null when the layer is not found in the response', async () => {
await endpoint.isReady();
const result = await endpoint.describeLayer('nonexistent:layer');
expect(result).toBeNull();
});
it('returns null when DescribeLayer is not advertised', async () => {
globalThis.fetchResponseFactory = () => capabilitiesStates;
endpoint = new WmsEndpoint('https://my.test.service/ogc/wms');
await endpoint.isReady();
expect(endpoint.describeLayer('usa:states')).toBeNull();
});
});
});