Skip to content

Commit

Permalink
Support custom header (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan-sauce authored Sep 15, 2020
1 parent a42229e commit 48c9c68
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ If you want to tunnel your API request through a proxy please provide your proxy
Type: `string`<br>
Default: `null`

### headers

If you want to set request headers, as example {'User-Agent': 'node-saucelabs'}

Type: `object`<br>
Default: `{'User-Agent': 'saucelabs/<VERSION> (nodejs <PLATFORM>)'}`

## Usage

All accessible API commands with descriptions can be found [here](docs/interface.md).
Expand Down
4 changes: 4 additions & 0 deletions scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface SauceLabsOptions {
* If you want to tunnel your API request through a proxy please provide your proxy URL.
*/
proxy?: string;
/**
* If you want to set request headers, as example {'User-Agent': 'node-saucelabs'}
*/
headers?: object;
}`

exports.TC_SAUCE_CONNECT_OBJ = `
Expand Down
5 changes: 4 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { camelCase } from 'change-case'
import os from 'os'

import { version } from '../package.json'

export const DEFAULT_SAUCE_CONNECT_VERSION = '4.6.2'
Expand Down Expand Up @@ -69,7 +71,8 @@ export const DEFAULT_OPTIONS = {
key: process.env.SAUCE_ACCESS_KEY,
headless: false,
region: 'us',
proxy: process.env.HTTPS_PROXY || process.env.HTTP_PROXY
proxy: process.env.HTTPS_PROXY || process.env.HTTP_PROXY,
headers: { 'User-Agent': `saucelabs/v${version} (nodejs ${os.platform()})` },
}

export const ASSET_REGION_MAPPING = {
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class SauceLabs {
password: this._accessKey,
https: { rejectUnauthorized: getStrictSsl() },
followRedirect: true,
headers: this._options.headers
})

if (typeof this._options.proxy === 'string') {
Expand All @@ -52,7 +53,8 @@ export default class SauceLabs {
region: this._options.region,
headless: this._options.headless,
proxy: this._options.proxy,
webdriverEndpoint: this.webdriverEndpoint
webdriverEndpoint: this.webdriverEndpoint,
headers: this._options.headers
}, { get: ::this.get })
}

Expand Down
6 changes: 6 additions & 0 deletions tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should contain custom headers 1`] = `
Object {
"user-agent": "foo",
}
`;

exports[`startSauceConnect should not overwrite rest-url if given as a parameter 1`] = `
Array [
Array [
Expand Down
10 changes: 10 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ test('should not even try to upload if no files were selected', async () => {
expect(result.message).toBe('No files to upload selected')
})

test('should contain custom headers', async () => {
const api = new SauceLabs({ user: 'foo', key: 'bar', headers: { 'user-agent': 'foo' } })
got.mockReturnValue(Promise.resolve({
body: JSON.stringify({ foo: 'bar' })
}))
await api.updateTest('123', '{ "passed": false }')
const requestOptions = got.extend.mock.calls[0][0]
expect(requestOptions.headers).toMatchSnapshot()
})

describe('startSauceConnect', () => {
it('should start sauce connect with proper parsed args', async () => {
const logs = []
Expand Down

0 comments on commit 48c9c68

Please sign in to comment.