-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathGraphResponseHandler.ts
More file actions
146 lines (132 loc) · 5.01 KB
/
GraphResponseHandler.ts
File metadata and controls
146 lines (132 loc) · 5.01 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
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { assert } from "chai";
import { GraphResponseHandler } from "../../../src/GraphResponseHandler";
import { ResponseType } from "../../../src/ResponseType";
describe("GraphResponseHandler.ts", () => {
const htmlString = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing Document</title>
</head>
<body>
<h1 id="test">Testing</h1>
</body>
</html>`;
const status200 = {
status: 200,
statusText: "OK",
};
const status204 = {
status: 204,
statusText: "OK",
};
const status500 = {
status: 500,
statusText: "Internal Server Error",
};
const status202 = {
status: 202,
statusText: "OK",
};
const status200Text = {
status: 200,
stautsText: "OK",
headers: {
"Content-Type": "text/plain",
},
};
const status200Json = {
status: 200,
stautsText: "OK",
headers: {
"Content-Type": "application/json",
},
};
const status200Unknown = {
status: 200,
statusText: "OK",
headers: {
"Content-Type": "dummy/unknown",
},
};
/* tslint:disable: no-string-literal */
describe("convertResponse", () => {
it("Should return empty response for the NO CONTENT (204 response)", async () => {
const response = new Response(undefined, status204);
const responseValue = await GraphResponseHandler["convertResponse"](response);
assert.isUndefined(responseValue);
});
it("Should return internal server error (500 response)", async () => {
const response = new Response(undefined, status500);
const responseValue = await GraphResponseHandler["convertResponse"](response);
assert.isNull(responseValue);
});
it("Should return empty text value for empty response", async () => {
const response = new Response(undefined, status202);
const responseValue = await GraphResponseHandler["convertResponse"](response);
assert.isNull(responseValue);
});
it("Should return text data for text/plain content-type", async () => {
const data = "text data";
const response = new Response(data, status200Text);
const responseValue = await GraphResponseHandler["convertResponse"](response);
assert.equal(responseValue, data);
});
it("Should return json data for application/json content-type", async () => {
const data = {
test: "test",
};
const response = new Response(JSON.stringify(data), status200Json);
const responseValue = await GraphResponseHandler["convertResponse"](response);
assert.equal(responseValue.test, data.test);
});
it("Should return raw response incase of unknown content-type", async () => {
const data = "test data";
const response = new Response(data, status200Unknown);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const responseValue = await GraphResponseHandler["convertResponse"](response);
// TODO - Handle unknown responses
// assert.equal(responseValue, data);
});
it("Should return response value as text", async () => {
const response = new Response(htmlString, status200);
const responseValue = await GraphResponseHandler["convertResponse"](response, ResponseType.TEXT);
assert.isDefined(responseValue);
assert.equal(typeof responseValue, "string");
assert.equal(responseValue, htmlString);
});
it("Should return response value as json", async () => {
const json = { test: "test" };
const response = new Response(JSON.stringify(json), status200);
const responseValue = await GraphResponseHandler["convertResponse"](response, ResponseType.JSON);
assert.isDefined(responseValue);
assert.equal(responseValue.test, "test");
});
it("Should return response value as text for default response type", async () => {
const response = new Response(htmlString, status200);
const responseValue = await GraphResponseHandler["convertResponse"](response);
assert.isDefined(responseValue);
assert.equal(typeof responseValue, "string");
assert.equal(responseValue, htmlString);
});
});
describe("getResponse", () => {
it("Should return a raw response", async () => {
const response = new Response(htmlString, status200);
const responseValue = await GraphResponseHandler.getResponse(response, ResponseType.RAW);
assert.isDefined(responseValue);
assert.isTrue(responseValue instanceof Response);
});
it("Should return valid 200 OK response", async () => {
const response = new Response(htmlString, status200);
const responseValue = await GraphResponseHandler.getResponse(response, ResponseType.TEXT);
assert.isDefined(responseValue);
});
});
});