-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update user agent #83
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b4f7947
Update base-client.ts
WilliamBergamin 25676cb
Update base-client.ts
WilliamBergamin 5b304bc
Added user-agent and tests
WilliamBergamin d86fcb1
remove useless changes
WilliamBergamin 223fb5d
Add one more tests
WilliamBergamin 9c8eb36
created base client helpers file
WilliamBergamin 8bf7a0a
improve speed
WilliamBergamin 85686dc
Improve comment
WilliamBergamin fe61d18
remove useless changes
WilliamBergamin 2821009
Update src/base-client-helpers_test.ts
WilliamBergamin 6562fca
update test description
WilliamBergamin 513d32d
add module to function test description
WilliamBergamin 46ab0ba
Improve test names
WilliamBergamin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const API_VERSION_REGEX = /\/deno_slack_api@(.*)\//; | ||
|
||
export function getUserAgent() { | ||
const userAgents = []; | ||
userAgents.push(`Deno/${Deno.version.deno}`); | ||
userAgents.push(`OS/${Deno.build.os}`); | ||
userAgents.push( | ||
`deno-slack-api/${_internals.getModuleVersion()}`, | ||
); | ||
return userAgents.join(" "); | ||
} | ||
|
||
function getModuleVersion(): string | undefined { | ||
const url = _internals.getModuleUrl(); | ||
// Insure this module is sourced from https://deno.land/x/deno_slack_api | ||
if (url.host === "deno.land") { | ||
return url.pathname.match(API_VERSION_REGEX)?.at(1); | ||
} | ||
return undefined; | ||
} | ||
|
||
function getModuleUrl(): URL { | ||
return new URL(import.meta.url); | ||
} | ||
|
||
// Serialize an object into a string so as to be compatible with x-www-form-urlencoded payloads | ||
export function serializeData(data: Record<string, unknown>): URLSearchParams { | ||
const encodedData: Record<string, string> = {}; | ||
Object.entries(data).forEach(([key, value]) => { | ||
// Objects/arrays, numbers and booleans get stringified | ||
// Slack API accepts JSON-stringified-and-url-encoded payloads for objects/arrays | ||
// Inspired by https://github.com/slackapi/node-slack-sdk/blob/%40slack/web-api%406.7.2/packages/web-api/src/WebClient.ts#L452-L528 | ||
|
||
// Skip properties with undefined values. | ||
if (value === undefined) return; | ||
|
||
const serializedValue: string = typeof value !== "string" | ||
? JSON.stringify(value) | ||
: value; | ||
encodedData[key] = serializedValue; | ||
}); | ||
|
||
return new URLSearchParams(encodedData); | ||
} | ||
|
||
export const _internals = { getModuleVersion, getModuleUrl }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { | ||
_internals, | ||
getUserAgent, | ||
serializeData, | ||
} from "./base-client-helpers.ts"; | ||
import { assertSpyCalls, stub } from "./dev_deps.ts"; | ||
|
||
Deno.test(`base-client-helpers.${_internals.getModuleVersion.name}`, async (t) => { | ||
await t.step( | ||
"should return the version if the module is sourced from deno.land", | ||
() => { | ||
const getModuleUrlStub = stub(_internals, "getModuleUrl", () => { | ||
return new URL("https://deno.land/x/[email protected]/mod.ts)"); | ||
}); | ||
|
||
try { | ||
const moduleVersion = _internals.getModuleVersion(); | ||
|
||
assertSpyCalls(getModuleUrlStub, 1); | ||
assertEquals(moduleVersion, "2.1.0"); | ||
} finally { | ||
filmaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
getModuleUrlStub.restore(); | ||
} | ||
}, | ||
); | ||
|
||
await t.step( | ||
"should return undefined if the module is not sourced from deno.land", | ||
() => { | ||
const getModuleUrlStub = stub(_internals, "getModuleUrl", () => { | ||
return new URL("file:///hello/world.ts)"); | ||
}); | ||
try { | ||
const moduleVersion = _internals.getModuleVersion(); | ||
|
||
assertSpyCalls(getModuleUrlStub, 1); | ||
assertEquals(moduleVersion, undefined); | ||
} finally { | ||
getModuleUrlStub.restore(); | ||
} | ||
}, | ||
); | ||
|
||
await t.step( | ||
"should return undefined if the regex used to parse [email protected] fails", | ||
() => { | ||
const getModuleUrlStub = stub(_internals, "getModuleUrl", () => { | ||
return new URL("https://deno.land/x/[email protected]/mod.ts)"); | ||
}); | ||
try { | ||
const moduleVersion = _internals.getModuleVersion(); | ||
|
||
assertSpyCalls(getModuleUrlStub, 1); | ||
assertEquals(moduleVersion, undefined); | ||
} finally { | ||
getModuleUrlStub.restore(); | ||
} | ||
}, | ||
); | ||
}); | ||
|
||
Deno.test(`base-client-helpers.${getUserAgent.name}`, async (t) => { | ||
await t.step( | ||
"should return the user agent with deno version, OS name and undefined deno-slack-api version", | ||
() => { | ||
const expectedVersion = undefined; | ||
const getModuleUrlStub = stub(_internals, "getModuleVersion", () => { | ||
return expectedVersion; | ||
}); | ||
|
||
try { | ||
const userAgent = getUserAgent(); | ||
|
||
assertSpyCalls(getModuleUrlStub, 1); | ||
assertEquals( | ||
userAgent, | ||
`Deno/${Deno.version.deno} OS/${Deno.build.os} deno-slack-api/undefined`, | ||
); | ||
} finally { | ||
getModuleUrlStub.restore(); | ||
} | ||
}, | ||
); | ||
|
||
await t.step( | ||
"should return the user agent with deno version, OS name and deno-slack-api version", | ||
() => { | ||
const expectedVersion = "2.1.0"; | ||
const getModuleUrlStub = stub(_internals, "getModuleUrl", () => { | ||
return new URL( | ||
`https://deno.land/x/deno_slack_api@${expectedVersion}/mod.ts)`, | ||
); | ||
}); | ||
|
||
try { | ||
const userAgent = getUserAgent(); | ||
|
||
assertSpyCalls(getModuleUrlStub, 1); | ||
assertEquals( | ||
userAgent, | ||
`Deno/${Deno.version.deno} OS/${Deno.build.os} deno-slack-api/${expectedVersion}`, | ||
); | ||
} finally { | ||
getModuleUrlStub.restore(); | ||
} | ||
}, | ||
); | ||
}); | ||
|
||
Deno.test(`${serializeData.name} helper function`, async (t) => { | ||
await t.step( | ||
"should serialize string values as strings and return a URLSearchParams object", | ||
() => { | ||
assertEquals( | ||
serializeData({ "batman": "robin" }).toString(), | ||
"batman=robin", | ||
); | ||
}, | ||
); | ||
await t.step( | ||
"should serialize non-string values as JSON-encoded strings and return a URLSearchParams object", | ||
() => { | ||
assertEquals( | ||
serializeData({ "hockey": { "good": true, "awesome": "yes" } }) | ||
.toString(), | ||
"hockey=%7B%22good%22%3Atrue%2C%22awesome%22%3A%22yes%22%7D", | ||
); | ||
}, | ||
); | ||
await t.step( | ||
"should not serialize undefined values", | ||
() => { | ||
assertEquals( | ||
serializeData({ | ||
"hockey": { "good": true, "awesome": "yes" }, | ||
"baseball": undefined, | ||
}) | ||
.toString(), | ||
"hockey=%7B%22good%22%3Atrue%2C%22awesome%22%3A%22yes%22%7D", | ||
); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import { | |
SlackAPIOptions, | ||
} from "./types.ts"; | ||
import { createHttpError, HttpError } from "./deps.ts"; | ||
import { getUserAgent, serializeData } from "./base-client-helpers.ts"; | ||
|
||
export class BaseSlackAPIClient implements BaseSlackClient { | ||
#token?: string; | ||
|
@@ -42,6 +43,7 @@ export class BaseSlackAPIClient implements BaseSlackClient { | |
headers: { | ||
"Authorization": `Bearer ${token}`, | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"User-Agent": getUserAgent(), | ||
}, | ||
body, | ||
}); | ||
|
@@ -60,6 +62,7 @@ export class BaseSlackAPIClient implements BaseSlackClient { | |
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"User-Agent": getUserAgent(), | ||
}, | ||
body: JSON.stringify(data), | ||
}); | ||
|
@@ -87,23 +90,3 @@ export class BaseSlackAPIClient implements BaseSlackClient { | |
}; | ||
} | ||
} | ||
|
||
// Serialize an object into a string so as to be compatible with x-www-form-urlencoded payloads | ||
export function serializeData(data: Record<string, unknown>): URLSearchParams { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function was moved to |
||
const encodedData: Record<string, string> = {}; | ||
Object.entries(data).forEach(([key, value]) => { | ||
// Objects/arrays, numbers and booleans get stringified | ||
// Slack API accepts JSON-stringified-and-url-encoded payloads for objects/arrays | ||
// Inspired by https://github.com/slackapi/node-slack-sdk/blob/%40slack/web-api%406.7.2/packages/web-api/src/WebClient.ts#L452-L528 | ||
|
||
// Skip properties with undefined values. | ||
if (value === undefined) return; | ||
|
||
const serializedValue: string = typeof value !== "string" | ||
? JSON.stringify(value) | ||
: value; | ||
encodedData[key] = serializedValue; | ||
}); | ||
|
||
return new URLSearchParams(encodedData); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,7 @@ export { | |
afterEach, | ||
beforeAll, | ||
} from "https://deno.land/[email protected]/testing/bdd.ts"; | ||
export { | ||
assertSpyCalls, | ||
stub, | ||
} from "https://deno.land/[email protected]/testing/mock.ts"; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests where moves to
base-client-helpers_test.ts