I am doing unit testing using Feature file and need to pass value in path of the Resource
I found an example cucumber-js-rest-api that leverages cucumber.js and request NPM to do somewhat you're looking.
For instance, if you have a feature with the following definition that pulls 119 issue from Github API:
Feature: Access an issue details As a GitHub API client I want to see the details of an issue Scenario: Get an issue When I GET the issue 119 in repository cucumber-js owned by cucumber Then the http status should be 200 And I should see "No zombie.Browser() object in zombie anymore." in the body And the title should equal "Update README.md to correct error in example for zombie initialization"
Then the steps to pull the ids from text are explained in the steps file. In this case issue 119 and repository cucumber-js will be retrieved from text:
this.When(/^I GET the issue (\d+) in repository (.*) owned by (.*)$/, function(issue, repo, owner, callback) { this.get(this.issuePath(owner, repo, issue), callback) })
Now, the next question might be. How does this execute an API call? World.js file will bind request module to steps with a few utilities functions that make http requests to Github API.
this.get = function(path, callback) { var uri = this.uri(path) request.get({url: uri, headers: {'User-Agent': 'request'}}, function(error, response) { if (error) { return callback.fail(new Error('Error on GET request to ' + uri + ': ' + error.message)) } self.lastResponse = response callback() }) }
Same thing applies to repoPath and issuePath functions, which support other features:
this.repoPath = function(owner, repo) { return '/repos/' + owner + '/' + repo } this.issuePath = function(owner, repo, issue) { return this.repoPath(owner, repo) + '/issues/' + issue }
Hope this helps!