Skip to content

Commit 0c52fe6

Browse files
authored
Tests for API (#122)
* check HTTP code status and show alert with error * tests for API errors
1 parent 64335b0 commit 0c52fe6

File tree

5 files changed

+46
-26
lines changed

5 files changed

+46
-26
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
}
2323
}
2424
</script>
25-
<script src="./src/apis.js"></script>
25+
<script type="module" src="./src/apis.js"></script>
2626
<script type="module" src="./src/index.js"></script>
2727
<div id="errorBox" style="position:absolute; top:10px; display:block; z-index:100; background-color: #ffffff; white-space:pre-line"></div>
2828
</body>

src/apis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const osmApiUrl = new URLSearchParams(location.search).get('osmApiUrl') || 'https://api.openstreetmap.org/api/0.6';
2-
const apis = {
2+
export const apis = {
33
bounding: {
44
api: osmApiUrl + '/map?bbox=',
55
url: (left, bottom, right, top) => {

src/building.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {apis} from './apis.js';
12
import {BuildingShapeUtils} from './extras/BuildingShapeUtils.js';
23
import {BuildingPart} from './buildingpart.js';
34
import {MultiBuildingPart} from './multibuildingpart.js';
@@ -209,24 +210,39 @@ class Building {
209210
static async getWayData(id) {
210211
let restPath = apis.getWay.url(id);
211212
let response = await fetch(restPath);
212-
let text = await response.text();
213-
return text;
213+
if (response.status === 404) {
214+
throw `The way ${id} was not found on the server.\nURL: ${restPath}`;
215+
} else if (response.status === 410) {
216+
throw `The way ${id} was deleted.\nURL: ${restPath}`;
217+
} else if (response.status !== 200) {
218+
throw `HTTP ${response.status}.\nURL: ${restPath}`;
219+
}
220+
return await response.text();
214221
}
215222

216223
static async getRelationData(id) {
217224
let restPath = apis.getRelation.url(id);
218225
let response = await fetch(restPath);
219-
let text = await response.text();
220-
return text;
226+
if (response.status === 404) {
227+
throw `The relation ${id} was not found on the server.\nURL: ${restPath}`;
228+
} else if (response.status === 410) {
229+
throw `The relation ${id} was deleted.\nURL: ${restPath}`;
230+
} else if (response.status !== 200) {
231+
throw `HTTP ${response.status}.\nURL: ${restPath}`;
232+
}
233+
return await response.text();
221234
}
222235

223236
/**
224-
* Fetch way data from OSM
237+
* Fetch map data data from OSM
225238
*/
226239
static async getInnerData(left, bottom, right, top) {
227-
let response = await fetch(apis.bounding.url(left, bottom, right, top));
228-
let res = await response.text();
229-
return res;
240+
let url = apis.bounding.url(left, bottom, right, top);
241+
let response = await fetch(url);
242+
if (response.status !== 200) {
243+
throw `HTTP ${response.status}.\nURL: ${url}`;
244+
}
245+
return await response.text();
230246
}
231247

232248
/**

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function init() {
7979
createFolders(folder, part.options);
8080
}
8181
}
82+
}).catch(err => {
83+
window.printError(err);
84+
alert(err);
8285
});
8386
camera = new PerspectiveCamera(
8487
50,

test/building.test.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,10 @@ expect.extend({toBeDeepCloseTo});
66

77
import { Shape, Mesh } from 'three';
88
import { TextEncoder } from 'node:util';
9+
import {expect, test, beforeEach, describe} from '@jest/globals';
910
global.TextEncoder = TextEncoder;
1011

11-
let apis = {
12-
bounding: {
13-
api:'https://api.openstreetmap.org/api/0.6/map?bbox=',
14-
url: (left, bottom, right, top) => {
15-
return apis.bounding.api + left + ',' + bottom + ',' + right + ',' + top;
16-
},
17-
},
18-
getRelation: {
19-
api:'https://api.openstreetmap.org/api/0.6/relation/',
20-
parameters:'/full',
21-
url: (relationId) => {
22-
return apis.getRelation.api + relationId + apis.getRelation.parameters;
23-
},
24-
},
25-
};
12+
import {apis} from '../src/apis.js';
2613
global.apis = apis;
2714

2815
import { Building } from '../src/building.js';
@@ -55,10 +42,24 @@ const data = `
5542
</osm>`;
5643

5744
beforeEach(() => {
58-
fetch.resetMocks();
45+
fetchMock.resetMocks();
5946
errors = [];
6047
});
6148

49+
describe.each([
50+
[['way', -1], ['', { status: 404 }], /^The way -1 was not found on the server.\nURL: /],
51+
[['way', -1], ['', { status: 410 }], /^The way -1 was deleted.\nURL: /],
52+
[['way', -1], ['', { status: 509 }], /^HTTP 509.\nURL: /],
53+
[['relation', -1], ['', { status: 404 }], /^The relation -1 was not found on the server.\nURL: /],
54+
[['relation', -1], ['', { status: 410 }], /^The relation -1 was deleted.\nURL: /],
55+
[['relation', -1], ['', { status: 509 }], /^HTTP 509.\nURL: /],
56+
])('Test API error handling', (args, response, matcher) => {
57+
test(`Test API error for ${args[0]} ${args[1]} with HTTP ${response[1].status}`, async() => {
58+
fetch.mockResponses(response);
59+
await expect(Building.downloadDataAroundBuilding(...args)).rejects.toMatch(matcher);
60+
});
61+
});
62+
6263
test('Test Constructor', async() => {
6364
const bldg = new Building('31361386', data);
6465
expect(bldg.home).toBeDeepCloseTo([11.015512, 49.5833659], 10);

0 commit comments

Comments
 (0)