forked from cap-js-community/odata-v2-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocols.test.js
130 lines (101 loc) · 5.16 KB
/
protocols.test.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
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
"use strict";
const cds = require("@sap/cds");
const supertest = require("supertest");
const util = require("./_env/util/request");
const fs = require("fs");
cds.test(__dirname + "/_env");
let request;
const expectGET = async (request, path) => {
await expectGETService(request, path);
await expectGETServiceMetadata(request, path);
await expectGETServiceData(request, `${path}/Header`);
};
const expectGETService = async (request, path) => {
const response = await util.callRead(request, path, {
accept: "application/json",
});
expect(response.body).toBeDefined();
expect(response.body.d.EntitySets.sort()).toEqual(
["Header", "HeaderItem", "HeaderLine", "Header_texts", "HeaderItem_texts"].sort(),
);
};
const expectGETServiceMetadata = async (request, path) => {
const response = await util.callRead(request, `${path}/$metadata`, {
accept: "application/json",
});
expect(response.body).toBeDefined();
expect(response.statusCode).toBe(200);
};
const expectGETServiceData = async (request, path) => {
const response = await util.callRead(request, path, {
accept: "application/json",
});
expect(response.body).toBeDefined();
expect(response.body.d.results.length > 0).toEqual(true);
};
const expectRejectProtocol = async (request, path) => {
let response = await util.callRead(request, `${path}/$metadata`, {
accept: "application/json",
});
expect(response.body).toBeDefined();
expect(response.text).toEqual("Invalid service protocol. Only OData services supported");
response = await util.callRead(request, `${path}/Header`, {
accept: "application/json",
});
expect(response.body).toBeDefined();
expect(response.text).toEqual("Invalid service protocol. Only OData services supported");
};
describe("CDS protocols", () => {
beforeAll(async () => {
await global._init;
request = supertest(cds.app.server);
});
it("service with relative path", () => expectGET(request, "/odata/v2/relative"));
it("service with absolute path", () => expectGET(request, "/odata/v2/absolute"));
it("service with relative complex path", () => expectGETService(request, "/odata/v2/relative2/complex/path"));
it("service with absolute complex path", () => expectGET(request, "/odata/v2/absolute2/complex/path"));
it("service annotated with @odata", async () => expectGET(request, "/odata/v2/atodata"));
it("reject service annotated with @rest", async () => expectRejectProtocol(request, "/odata/v2/rest/atrest"));
it("service annotated with @protocol: 'odata'", async () => expectGET(request, "/odata/v2/atprotocolodata"));
it("service annotated with @protocol: 'rest'", async () =>
expectRejectProtocol(request, "/odata/v2/rest/atprotocolrest"));
it("service annotated with @protocol: ['odata']", async () => expectGET(request, "/odata/v2/atprotocollistodata"));
it("service annotated with @protocol: ['rest']", async () =>
expectRejectProtocol(request, "/odata/v2/rest/atprotocollistrest"));
it("service annotated with @protocol: [{ kind: 'odata', path: 'relative2' }]", async () =>
expectGET(request, "/odata/v2/relative2"));
it("service annotated with @protocol: [{ kind: 'odata', path: '/absolute2' }]", async () =>
expectGET(request, "/odata/v2/absolute2"));
it("service annotated with @protocol: [{ kind: 'odata', path: '/custom/odata/path' }]", async () =>
expectGET(request, "/odata/v2/custom/odata/path"));
it("service annotated with @protocol: [{ kind: 'odata-v4', path: '/custom2/odata/path' }]", async () =>
expectGET(request, "/odata/v2/custom2/odata/path"));
it("service annotated with @protocol: [{ kind: 'odata-v4', path: '/odata' }]", async () =>
expectGET(request, "/odata/v2/odata"));
it("service annotated with @protocol: [{ kind: 'odata-v4', path: 'odata' }]", async () =>
expectGET(request, "/odata/v2/odata"));
it("service annotated with @protocol: [{ kind: 'odata-v4', path: '/odata/v4/odata' }]", async () =>
expectGET(request, "/odata/v2/odata"));
it("service with absolute path on batch calls", async () => {
let response = await util.callRead(request, "/odata/v2/absolute/Header?$top=1");
expect(response.body).toBeDefined();
expect(response.body.d.results).toHaveLength(1);
const ID = response.body.d.results[0].ID;
let payload = fs.readFileSync(__dirname + "/_env/util/batch/Batch-GET.txt", "utf8");
payload = payload.replace(/\r\n/g, "\n");
payload = payload.replace(/{{ID}}/g, ID);
response = await util.callMultipart(request, "/odata/v2/absolute/$batch", payload);
expect(response.statusCode).toEqual(202);
const responses = util.splitMultipartResponse(response.body);
expect(responses.length).toEqual(3);
expect(responses.filter((response) => response.statusCode === 200).length).toEqual(3);
const [first, second, third] = responses;
expect(first.body.d.results.length).toEqual(7);
expect(first.contentTransferEncoding).toEqual("binary");
expect(second.body.d.results.length).toEqual(7);
expect(second.contentTransferEncoding).toEqual("binary");
expect(third.body.d.hasOwnProperty("results")).toEqual(false);
expect(third.body.d.ID).toEqual(ID);
expect(third.contentTransferEncoding).toEqual("binary");
});
});