vlucas / frisby

Frisby is a REST API testing framework built on Jest that makes testing API endpoints easy, fast, and fun.
http://frisbyjs.com
1.53k stars 201 forks source link

Cannot get right data by using sample basic authorization settings. #504

Closed Smilehwj closed 6 years ago

Smilehwj commented 6 years ago
frisby.globalSetup({
  request: {
    headers: {
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'application/json',
      'Authorization': 'Basic' + Buffer.from('apiUser:u3KTqbnIxKe09keL9HFi+LDToQG5opsYDc4d+aX6').toString('base64')
    }
  },
  timeout: (60 * 1000)
})

}

FetchError: request to https://....... failed, reason: self signed certificate

Smilehwj commented 6 years ago

I have tried using 'Authorization': 'Basic YXBpVXNlcjp1M0tUcWJuSXhLZTA5a2VMOUhGaStMRFRvUUc1b3BzWURjNGQrYVg2' to get data. Still failed, same error.

H1Gdev commented 6 years ago

@Smilehwj

In node.js cannot use self signed certificate by default. https host should have valid cert.

Or disable certificate validation. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; https://nodejs.org/api/cli.html#cli_node_tls_reject_unauthorized_value

In jest. https://jestjs.io/docs/en/configuration#testenvironment-string

custom_env.js

const JSDOMEnvironment = require('jest-environment-jsdom');

class CustomEnvironment extends JSDOMEnvironment {
  async setup() {
    await super.setup();
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  }
}

module.exports = CustomEnvironment;

$ npm test -- --env=path/to/custom_env.js

Smilehwj commented 6 years ago

@Smilehwj

In node.js cannot use self signed certificate by default. https host should have valid cert.

Or disable certificate validation. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; https://nodejs.org/api/cli.html#cli_node_tls_reject_unauthorized_value

In jest. https://jestjs.io/docs/en/configuration#testenvironment-string

custom_env.js

const JSDOMEnvironment = require('jest-environment-jsdom');

class CustomEnvironment extends JSDOMEnvironment {
  async setup() {
    await super.setup();
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  }
}

module.exports = CustomEnvironment;

$ npm test -- --env=path/to/custom_env.js

That works for me. Thanks for your help.