-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
75 lines (57 loc) · 2.11 KB
/
Copy pathmain.js
File metadata and controls
75 lines (57 loc) · 2.11 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
import { group, check } from 'k6';
import json from "k6/x/json";
export const options = {
iterations: 1,
vus: 1,
};
export default function () {
group("JSON Marshal", function () {
const sourceObj = {
"name": "Xacobe",
"age": 30,
"city": "Arteixo",
};
const marshalResult = json.marshal(sourceObj); // JSON string
console.log("Marshal result:", marshalResult);
check(marshalResult, {
"Marshal should return a string": (r) => typeof r === "string",
"The marshalled object should contain the sourceObj properties": (r) => {
const obj = JSON.parse(r);
return obj.name === sourceObj.name && obj.age === sourceObj.age && obj.city === sourceObj.city;
},
});
});
group("JSON Unmarshal", function () {
const sourceJson = `{"name":"Uxia","age":32,"city":"Santiago de Compostela"}`;
const unmarshalResult = json.unmarshal(sourceJson); // JSON Object
console.log("Unmarshal result:", unmarshalResult);
check(unmarshalResult, {
"Unmarshal should return an object": (r) => typeof r === "object",
"The unmarshalled object should contain the sourceJson properties": (r) => {
return r.name === "Uxia" && r.age === 32 && r.city === "Santiago de Compostela";
},
});
});
group("Unmarshal failures", function () {
const invalidJSON = `{"name": "Brais", "age": 30`; // Missing closing bracket
const unmarshalResult = json.unmarshal(invalidJSON); // null
console.log("Unmarshal Failure result:", unmarshalResult);
check(unmarshalResult, {
"Unmarshal with invalid JSON should return null": (r) => r === null,
});
});
group("Marshal failures", function () {
const sourceObj = {
"name": "John",
"age": 30,
"city": "New York"
}
//Add circular reference to create invalid JSON
sourceObj.circularReference = sourceObj;
const marshalResult = json.marshal(sourceObj); // empty string
console.log("Marshal Failure result:", marshalResult);
check(marshalResult, {
"Marshal with circular reference should return empty string": (r) => r === "",
});
});
}