Skip to content

Commit 48c0c17

Browse files
Merge branch 'dev' into Pipeline_Scripts
2 parents 9886feb + 01c28e7 commit 48c0c17

File tree

6 files changed

+109
-25
lines changed

6 files changed

+109
-25
lines changed

samples/browser/src/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
</head>
1313

1414
<body style="background: #f3f3f3;">
15-
<div class="fileUploadParent">
15+
<!-- <div class="fileUploadParent">
1616
<img id="profileImg" width="100px" src="" alt="profileImg" />
1717
<input id="uploadProfile" type="file" class="fileUpload" onchange="request.updateProfilePicture()" />
1818
<div class="imageInput">
1919
<img width="20px" src="./upload-user.svg" />
2020
</div>
21-
</div>
21+
</div> -->
2222
<div display="inline-block">
2323
<h2>
2424
<span>Hello </span>
25-
<span id="displayName" style="display: inline-block; vertical-align: middle;"></span>
26-
<span> !</span>
25+
<!-- <span id="displayName" style="display: inline-block; vertical-align: middle;"></span> -->
26+
<span>!</span>
2727
</h2>
2828
</div>
2929
<div class="main">

samples/browser/src/main.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ const init = async () => {
2323

2424
bindEvents();
2525

26-
let displayName = await request.getDisplayName();
27-
ui.setDisplayName(displayName);
26+
// let displayName = await request.getDisplayName();
27+
// ui.setDisplayName(displayName);
2828

29-
let profileImg = await request.getProfilePicture();
30-
ui.setProfilePicture(profileImg);
29+
// let profileImg = await request.getProfilePicture();
30+
// ui.setProfilePicture(profileImg);
3131
};
3232

3333
const bindEvents = () => {

samples/browser/src/request.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ let request = {
1313

1414
getProfilePicture: async () => {
1515
try {
16-
let response = await client
17-
.api("/me/photo/$value")
18-
.responseType(MicrosoftGraph.ResponseType.BLOB)
19-
.get();
16+
let response = await client.api("/me/photo/$value").get();
2017
return response;
2118
} catch (error) {
2219
console.error(error);

spec/core/GraphResponseHandler.ts

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { assert } from "chai";
99

10-
import { GraphResponseHandler } from "../../src/GraphResponseHandler";
10+
import { DocumentType, GraphResponseHandler } from "../../src/GraphResponseHandler";
1111
import { ResponseType } from "../../src/ResponseType";
1212

1313
describe("GraphResponseHandler.ts", () => {
@@ -33,11 +33,43 @@ describe("GraphResponseHandler.ts", () => {
3333
status: 500,
3434
statusText: "Internal Server Error",
3535
};
36+
const status202 = {
37+
status: 202,
38+
statusText: "OK",
39+
};
40+
const status200Text = {
41+
status: 200,
42+
stautsText: "OK",
43+
headers: {
44+
"Content-Type": "text/plain",
45+
},
46+
};
47+
const status200Json = {
48+
status: 200,
49+
stautsText: "OK",
50+
headers: {
51+
"Content-Type": "application/json",
52+
},
53+
};
54+
const status200Image = {
55+
status: 200,
56+
stautsText: "OK",
57+
headers: {
58+
"Content-Type": "image/jpeg",
59+
},
60+
};
61+
const status200Unknown = {
62+
status: 200,
63+
statusText: "OK",
64+
headers: {
65+
"Content-Type": "dummy/unknown",
66+
},
67+
};
3668
/* tslint:disable: no-string-literal */
3769
describe("parseDocumentResponse", () => {
3870
it("Should return the html string", async () => {
3971
const response = new Response(htmlString, status200);
40-
const dom = await GraphResponseHandler["parseDocumentResponse"](response, GraphResponseHandler["DocumentTypes"]["TEXT_HTML"]);
72+
const dom = await GraphResponseHandler["parseDocumentResponse"](response, DocumentType.TEXT_HTML);
4173
assert.isDefined(dom);
4274
assert.equal(typeof dom, "string");
4375
});
@@ -50,6 +82,35 @@ describe("GraphResponseHandler.ts", () => {
5082
assert.isUndefined(responseValue);
5183
});
5284

85+
it("Should return empty text value for empty response", async () => {
86+
const response = new Response(undefined, status202);
87+
const responseValue = await GraphResponseHandler["convertResponse"](response);
88+
assert.isUndefined(responseValue);
89+
});
90+
91+
it("Should return text data for text/plain content-type", async () => {
92+
const data = "text data";
93+
const response = new Response(data, status200Text);
94+
const responseValue = await GraphResponseHandler["convertResponse"](response);
95+
assert.equal(responseValue, data);
96+
});
97+
98+
it("Should return json data for application/json content-type", async () => {
99+
const data = {
100+
test: "test",
101+
};
102+
const response = new Response(JSON.stringify(data), status200Json);
103+
const responseValue = await GraphResponseHandler["convertResponse"](response);
104+
assert.equal(responseValue.test, data.test);
105+
});
106+
107+
it("Should return raw response incase of unknown content-type", async () => {
108+
const data = "test data";
109+
const response = new Response(data, status200Unknown);
110+
const responseValue = await GraphResponseHandler["convertResponse"](response);
111+
assert.equal(responseValue, data);
112+
});
113+
53114
it("Should return response value as text", async () => {
54115
const response = new Response(htmlString, status200);
55116
const responseValue = await GraphResponseHandler["convertResponse"](response, ResponseType.TEXT);

src/GraphErrorHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,11 @@ export class GraphErrorHandler {
106106
*/
107107
public static async getError(error: any = null, statusCode: number = -1, callback?: GraphRequestCallback): Promise<GraphError> {
108108
let gError: GraphError;
109-
if (error instanceof Response) {
109+
if (error && (error.constructor.name === "Response" || error.constructor.name === "Body")) {
110110
gError = await GraphErrorHandler.constructErrorFromRawResponse(error, statusCode);
111111
} else if (error && error.error) {
112112
gError = GraphErrorHandler.constructErrorFromResponse(error, statusCode);
113-
} else if (error instanceof Error) {
113+
} else if (error && error.constructor.name === "Error") {
114114
gError = GraphErrorHandler.constructError(error, statusCode);
115115
} else {
116116
gError = new GraphError(statusCode);

src/GraphResponseHandler.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,42 @@ import { ResponseType } from "./ResponseType";
2020
* @property {string} APPLICATION_XML - The application/xml content type
2121
* @property {string} APPLICATION_XHTML - The application/xhml+xml content type
2222
*/
23-
enum DocumentType {
23+
export enum DocumentType {
2424
TEXT_HTML = "text/html",
2525
TEXT_XML = "text/xml",
2626
APPLICATION_XML = "application/xml",
2727
APPLICATION_XHTML = "application/xhtml+xml",
2828
}
2929

30+
/**
31+
* @enum
32+
* Enum for Content types
33+
* @property {string} TEXT_PLAIN - The text/plain content type
34+
* @property {string} APPLICATION_JSON - The application/json content type
35+
*/
36+
37+
enum ContentType {
38+
TEXT_PLAIN = "text/plain",
39+
APPLICATION_JSON = "application/json",
40+
}
41+
42+
/**
43+
* @enum
44+
* Enum for Content type regex
45+
* @property {string} DOCUMENT - The regex to match document content types
46+
* @property {string} IMAGE - The regex to match image content types
47+
*/
48+
enum ContentTypeRegexStr {
49+
DOCUMENT = "^(text\\/(html|xml))|(application\\/(xml|xhtml\\+xml))$",
50+
IMAGE = "^image\\/.+",
51+
}
52+
3053
/**
3154
* @class
3255
* Class for GraphResponseHandler
3356
*/
3457

3558
export class GraphResponseHandler {
36-
/**
37-
* @private
38-
* @static
39-
* A member holding array of document types
40-
*/
41-
private static DocumentTypes: string[] = ["text/html", "text/xml", "application/xml", "application/xhtml+xml"];
42-
4359
/**
4460
* @private
4561
* @static
@@ -110,7 +126,17 @@ export class GraphResponseHandler {
110126
const contentType = clonedRawResponse.headers.get("Content-type");
111127
if (contentType !== null) {
112128
const mimeType = contentType.split(";")[0];
113-
responseValue = GraphResponseHandler.DocumentTypes.includes(mimeType) ? await GraphResponseHandler.parseDocumentResponse(clonedRawResponse, mimeType as DocumentType) : await clonedRawResponse.json();
129+
if (new RegExp(ContentTypeRegexStr.DOCUMENT).test(mimeType)) {
130+
responseValue = await GraphResponseHandler.parseDocumentResponse(clonedRawResponse, mimeType as DocumentType);
131+
} else if (new RegExp(ContentTypeRegexStr.IMAGE).test(mimeType)) {
132+
responseValue = clonedRawResponse.blob();
133+
} else if (mimeType === ContentType.TEXT_PLAIN) {
134+
responseValue = await clonedRawResponse.text();
135+
} else if (mimeType === ContentType.APPLICATION_JSON) {
136+
responseValue = await clonedRawResponse.json();
137+
} else {
138+
responseValue = Promise.resolve(clonedRawResponse.body);
139+
}
114140
} else {
115141
/**
116142
* RFC specification {@link https://tools.ietf.org/html/rfc7231#section-3.1.1.5} says:

0 commit comments

Comments
 (0)