Skip to content

Commit 188e963

Browse files
committed
test: adding tests
Signed-off-by: Pawel Psztyc <jarrodek@gmail.com>
1 parent b8743c9 commit 188e963

19 files changed

+1310
-155
lines changed

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 3,
33
"name": "API console extension",
4-
"version": "0.1.0",
4+
"version": "0.1.3",
55
"description": "API Console extension to proxy HTTP requests to the documented API.",
66
"author": "Pawel Psztyc <pawel.psztyc@mulesoft.com>",
77
"content_scripts": [

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@advanced-rest-client/api-console-extension",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "API Console extension to proxy HTTP requests to the documented API.",
55
"type": "module",
66
"main": "index.js",

src/ApiConsoleAppProxy.js

+17-25
Original file line numberDiff line numberDiff line change
@@ -234,51 +234,40 @@ export class ApiConsoleAppProxy extends EventTarget {
234234
detail: {
235235
message: "No response has been recorded.",
236236
code: "no_response",
237+
error: true,
237238
},
238239
})
239240
);
240241
return;
241242
}
242243
const typedError = /** @type IApiConsoleProxyError */ (data);
243244
if (typedError.error) {
244-
const message =
245-
typeof typedError.message === "string"
246-
? typedError.message
247-
: "No response has been recorded.";
248-
const code =
249-
typeof typedError.code === "string" ? typedError.code : "unknown_error";
245+
const message = typeof typedError.message === "string" ? typedError.message : "No response has been recorded.";
246+
const code = typeof typedError.code === "string" ? typedError.code : "unknown_error";
247+
const state = typeof typedError.state === "string" ? typedError.state : undefined;
250248
this.eventTarget.dispatchEvent(
251249
new CustomEvent("oauth2-error", {
252250
bubbles: true,
253251
composed: true,
254252
detail: {
255253
message,
256254
code,
255+
state,
256+
error: true,
257257
},
258258
})
259259
);
260260
return;
261261
}
262262
const typedToken = /** @type ITokenInfo */ (data);
263-
const state =
264-
typeof typedToken.state === "string" ? typedToken.state : undefined;
265-
const accessToken =
266-
typedToken.accessToken && typeof typedToken.accessToken === "string"
267-
? typedToken.accessToken
268-
: undefined;
269-
const tokenType =
270-
typedToken.tokenType && typeof typedToken.tokenType === "string"
271-
? typedToken.tokenType
272-
: undefined;
273-
const expiresIn =
274-
typedToken.expiresIn &&
275-
(typeof typedToken.expiresIn === "number" ||
276-
typeof typedToken.expiresIn === "string")
277-
? Number(typedToken.expiresIn)
278-
: undefined;
279-
const scope = Array.isArray(typedToken.scope)
280-
? typedToken.scope
281-
: undefined;
263+
const state = typeof typedToken.state === "string" ? typedToken.state : undefined;
264+
const accessToken = typedToken.accessToken && typeof typedToken.accessToken === "string" ? typedToken.accessToken : undefined;
265+
const refreshToken = typedToken.refreshToken && typeof typedToken.refreshToken === "string" ? typedToken.refreshToken : undefined;
266+
const tokenType = typedToken.tokenType && typeof typedToken.tokenType === "string" ? typedToken.tokenType : undefined;
267+
const expiresIn = typedToken.expiresIn && (typeof typedToken.expiresIn === "number" || typeof typedToken.expiresIn === "string") ? Number(typedToken.expiresIn): undefined;
268+
const expiresAt = typedToken.expiresAt && (typeof typedToken.expiresAt === "number" || typeof typedToken.expiresAt === "string") ? Number(typedToken.expiresAt): undefined;
269+
const expiresAssumed = typeof typedToken.expiresAssumed === "boolean" ? typedToken.expiresAssumed : undefined;
270+
const scope = Array.isArray(typedToken.scope) ? typedToken.scope : undefined;
282271
this.eventTarget.dispatchEvent(
283272
new CustomEvent("oauth2-token-response", {
284273
bubbles: true,
@@ -289,6 +278,9 @@ export class ApiConsoleAppProxy extends EventTarget {
289278
tokenType,
290279
expiresIn,
291280
scope,
281+
refreshToken,
282+
expiresAt,
283+
expiresAssumed,
292284
},
293285
})
294286
);

src/proxy/OAuth2Proxy.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export class OAuth2Proxy {
250250
id: id,
251251
tab: tab
252252
};
253+
this._addTabHandlers();
253254
} catch (e) {
254255
throw new AuthorizationError(
255256
e.message,
@@ -343,7 +344,7 @@ export class OAuth2Proxy {
343344
/** @type string */
344345
let raw;
345346
try {
346-
const raw = this._authDataFromUrl(url);
347+
raw = this._authDataFromUrl(url);
347348
if (!raw) {
348349
throw new Error('');
349350
}
@@ -437,6 +438,7 @@ export class OAuth2Proxy {
437438
let tokenInfo;
438439
try {
439440
tokenInfo = await this.exchangeCode(code);
441+
tokenInfo.state = state;
440442
} catch (e) {
441443
this._handleTokenCodeError(/** @type Error */(e));
442444
return;

src/proxy/OAuthUtils.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function sanityCheck(settings) {
5959
*/
6060
export function randomString() {
6161
const array = new Uint32Array(28);
62-
window.crypto.getRandomValues(array);
62+
globalThis.crypto.getRandomValues(array);
6363
return Array.from(array, (dec) => `0${dec.toString(16)}`.substr(-2)).join("");
6464
}
6565

@@ -93,7 +93,7 @@ export function camel(name) {
9393
export async function sha256(value) {
9494
const encoder = new TextEncoder();
9595
const data = encoder.encode(value);
96-
return window.crypto.subtle.digest("SHA-256", data);
96+
return globalThis.crypto.subtle.digest("SHA-256", data);
9797
}
9898

9999
/**
@@ -126,7 +126,7 @@ export async function generateCodeChallenge(verifier) {
126126
export function nonceGenerator(size = 20) {
127127
const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
128128
let array = new Uint8Array(size);
129-
window.crypto.getRandomValues(array);
129+
globalThis.crypto.getRandomValues(array);
130130
array = array.map(x => validChars.charCodeAt(x % validChars.length));
131131
return String.fromCharCode.apply(null, array);
132132
}

src/service.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ class ApiConsoleService {
5151
} catch (e) {
5252
this.sendResponse({
5353
'message': e.message || 'The request is invalid.',
54-
'code': 'invalid_request',
55-
'error': true
54+
'code': e.code || 'invalid_request',
55+
'error': true,
56+
'state': e.state,
5657
});
5758
}
5859
}

src/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export interface IApiConsoleProxyError {
108108
error: true;
109109
code?: string;
110110
message: string;
111+
state?: string;
111112
}
112113

113114
export interface IApiConsoleHttpResponseStats {

0 commit comments

Comments
 (0)