0

I tried to use a public end point(eg:api.openweathermap.org/data/2.5/weather?lat=35&lon=139) instead of the local host while configuring dredd and ran the command to run the tool.But I am not able to connect to the end point through dredd. It is throwing Error:getaddrINFO EAI_AGAIN . But when I tried to connect to the endpoint using post man .I am able to connect successfully

sachu
  • 1
  • 1

1 Answers1

1

There is no difference in calling a local or remote endpoint.

Some remote endpoints have some sort of authorization requirements.

This an example of Dredd calling external endpoint:

dredd.yml configuration file fragment

...
blueprint: doc/api.md
# endpoint: 'http://api-srv:5000'
endpoint: https://private-da275-notes69.apiary-mock.com

As you see, the only change is the endpoint on Dredd configuration file (created using Dredd init).

But, as I mention, sometimes you'll need to provide authorization through the header or query string parameter.

Dreed has hooks that allow you to change things before each transaction, for instance:

You'd like to add the apikey parameter in each URL before executing the request. This code can handle that.

hook.js

// Writing Dredd Hooks In Node.js
// Ref: http://dredd.org/en/latest/hooks-nodejs.html

var hooks = require('hooks');

hooks.beforeEach(function(transaction) {
  hooks.log('before each');
  // add query parameter to each transaction here
  let paramToAdd = 'api-key=23456';
  if (transaction.fullPath.indexOf('?') > -1)
    transaction.fullPath += '&' + paramToAdd;
  else
    transaction.fullPath += '?' + paramToAdd;

  hooks.log('before each fullpath: ' + transaction.fullPath);
});

Code at Github gist

Save this hook file anywhere in your project an than run Dredd passing the hook file.

dredd --hookfiles=./hoock.js

That's it, after execution the log will show the actual URL used in the request.

info: Configuration './dredd.yml' found, ignoring other arguments.
2018-06-25T16:57:13.243Z - info: Beginning Dredd testing...
2018-06-25T16:57:13.249Z - info: Found Hookfiles: 0=/api/scripts/dredd-hoock.js
2018-06-25T16:57:13.263Z - hook: before each
2018-06-25T16:57:13.264Z - hook: before each fullpath: /notes?api-key=23456
"/notes?api-key=23456"
2018-06-25T16:57:16.095Z - pass: GET (200) /notes duration: 2829ms
2018-06-25T16:57:16.096Z - hook: before each
2018-06-25T16:57:16.096Z - hook: before each fullpath: /notes?api-key=23456
"/notes?api-key=23456"
2018-06-25T16:57:16.788Z - pass: POST (201) /notes duration: 691ms
2018-06-25T16:57:16.788Z - hook: before each
2018-06-25T16:57:16.789Z - hook: before each fullpath: /notes/abcd1234?api-key=23456
"/notes/abcd1234?api-key=23456"
2018-06-25T16:57:17.113Z - pass: GET (200) /notes/abcd1234 duration: 323ms
2018-06-25T16:57:17.114Z - hook: before each
2018-06-25T16:57:17.114Z - hook: before each fullpath: /notes/abcd1234?api-key=23456
"/notes/abcd1234?api-key=23456"
2018-06-25T16:57:17.353Z - pass: DELETE (204) /notes/abcd1234 duration: 238ms
2018-06-25T16:57:17.354Z - hook: before each
2018-06-25T16:57:17.354Z - hook: before each fullpath: /notes/abcd1234?api-key=23456
"/notes/abcd1234?api-key=23456"
2018-06-25T16:57:17.614Z - pass: PUT (200) /notes/abcd1234 duration: 259ms
2018-06-25T16:57:17.615Z - complete: 5 passing, 0 failing, 0 errors, 0 skipped, 5 total
2018-06-25T16:57:17.616Z - complete: Tests took 4372ms