forked from FIWARE/NGSI-LD_TestSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
83 lines (65 loc) · 2.34 KB
/
common.js
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
/**
* Common utility functions. Probably in the future this would need a bit of refactoring
*
* Copyright (c) 2018 FIWARE Foundation e.V.
*
*
*/
'use strict';
var endpoint = process.env.TEST_ENDPOINT;
var ngsild = 'ngsi-ld/v1';
// Just to test an old NGSIv2 endpoint
if (process.env.FAKE_LD == 'yes') {
ngsild = 'v2';
}
const testedResource = endpoint + '/' + ngsild;
// Regular expression for matching the link header pointing to the JSON-LD @context
const JSON_LD_CONTEXT_HEADER = /<.+>;\s+rel="http:\/\/www\.w3\.org\/ns\/json-ld#context";\s+type="application\/ld\+json"/;
const JSON = /application\/json(;.*)?/;
function assertCreated(response, id) {
expect(response).toHaveProperty('statusCode', 201);
expect(response.headers).toHaveProperty('location', '/' + ngsild + '/entities/' + id);
}
function assertResponse(response, mimeType) {
let mType = mimeType || JSON;
expect(response.response).toHaveProperty('statusCode', 200);
expect(response.response.headers['content-type']).toMatch(mType);
// response.response.headers['link'] =
// '<http://json-ld.org/contexts/person.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"';
if (mType == JSON) {
expect(response.response.headers['link']).toBeDefined();
let linkHeader = response.response.headers['link'];
expect(linkHeader).toMatch(JSON_LD_CONTEXT_HEADER);
}
}
function assertRetrieved(response, entity, mimeType) {
assertResponse(response, mimeType);
expect(response.body).toEqual(entity);
}
function assertRetrievedQuery(response, entity, mimeType) {
assertResponse(response, mimeType);
// Check first query result
expect(response.body[0]).toBeDefined();
expect(response.body[0]).toEqual(entity);
}
function assertNoResultsQuery(response, mimeType) {
assertResponse(response, mimeType);
// Check first query result
expect(response.body.length).toBe(0);
}
function serializeParams(query) {
let out = '';
Object.keys(query).forEach(function(key) {
out += key + '=' + encodeURIComponent(query[key]);
out += '&';
});
return out.substring(0, out.length - 1);
}
module.exports = {
testedResource: testedResource,
assertCreated: assertCreated,
assertRetrieved: assertRetrieved,
assertRetrievedQuery: assertRetrievedQuery,
assertNoResultsQuery: assertNoResultsQuery,
serializeParams: serializeParams
};