Skip to content

Commit

Permalink
Provide process.platform fallback for default User-Agent
Browse files Browse the repository at this point in the history
Fixes #2294.
  • Loading branch information
Zirro authored and domenic committed Aug 14, 2018
1 parent c854cc4 commit 0c100a3
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -154,7 +154,7 @@ The three options to the `ResourceLoader` constructor are:

- `proxy` is the address of a HTTP proxy to be used.
- `strictSSL` can be set to false to disable the requirement that SSL certificates be valid.
- `userAgent` affects the `User-Agent` header sent, and thus the resulting value for `navigator.userAgent`. It defaults to <code>\`Mozilla/5.0 (${process.platform}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/${jsdomVersion}\`</code>.
- `userAgent` affects the `User-Agent` header sent, and thus the resulting value for `navigator.userAgent`. It defaults to <code>\`Mozilla/5.0 (${process.platform || "unknown OS"}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/${jsdomVersion}\`</code>.

You can further customize resource fetching by subclassing `ResourceLoader` and overriding the `fetch()` method. For example, here is a version that only returns results for requests to a trusted origin:

Expand Down
3 changes: 2 additions & 1 deletion lib/jsdom/browser/resources/resource-loader.js
Expand Up @@ -11,7 +11,8 @@ module.exports = class ResourceLoader {
constructor({
strictSSL = true,
proxy = undefined,
userAgent = `Mozilla/5.0 (${process.platform}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/${packageVersion}`
userAgent = `Mozilla/5.0 (${process.platform || "unknown OS"}) AppleWebKit/537.36 ` +
`(KHTML, like Gecko) jsdom/${packageVersion}`
} = {}) {
this._strictSSL = strictSSL;
this._proxy = proxy;
Expand Down
2 changes: 1 addition & 1 deletion test/api/from-url.js
Expand Up @@ -81,7 +81,7 @@ describe("API: JSDOM.fromURL()", { skipIfBrowser: true }, () => {

describe("user agent", () => {
it("should use the default user agent as the User-Agent header when none is given", () => {
const expected = `Mozilla/5.0 (${process.platform}) AppleWebKit/537.36 ` +
const expected = `Mozilla/5.0 (${process.platform || "unknown OS"}) AppleWebKit/537.36 ` +
`(KHTML, like Gecko) jsdom/${packageVersion}`;

let recordedHeader;
Expand Down
2 changes: 1 addition & 1 deletion test/api/resources.js
Expand Up @@ -552,7 +552,7 @@ describe("API: resource loading configuration", { skipIfBrowser: true }, () => {

for (const resources of [undefined, "usable"]) {
describe(`User agent (resources set to ${resources})`, () => {
const expected = `Mozilla/5.0 (${process.platform}) AppleWebKit/537.36 ` +
const expected = `Mozilla/5.0 (${process.platform || "unknown OS"}) AppleWebKit/537.36 ` +
`(KHTML, like Gecko) jsdom/${packageVersion}`;

it("should have a default user agent following the correct pattern", () => {
Expand Down

0 comments on commit 0c100a3

Please sign in to comment.