How can I call APIs with different basepaths in an Apickli test?

Is there a pattern or technique to call another API from a Scenario?

For example, I want to get an oauth token from another API proxy and use it in a scenario.

Here's what I want to do:

Scenario: Verify OAuth token works
        Given I have a valid token request
        When I POST to OAUTH-BASEPATH/token
        And I store the value of body path $.access_token as access token
        And I set bearer token
        When I GET /validate
        Then response code should be 200

The OAUTH-BASEPATH is different than the basepath used in my config for the GET /validate.

2 3 1,286
3 REPLIES 3

For this particular scenario to work, just provide the domain name (without basepath) in Apickli constructor and refer to the full basepath in the feature file.

Another way of implementing this (just FYI) is to invent a new Given statement like

Given I have a valid access token

and implement a custom step function using apickli as a utility:

defineSupportCode(function({Given, When, Then}) {
	Given(/^I have a valid access token$/, function(callback) {
	        this.apickli.post(OAUTH-BASEPATH + "/token", function(err, response) {
			...
			callback();
		});

	});
});

Custom step function is the one I use for all my implementations.

Thanks @Ozan Seymen I followed that approach and generalized it to call out to a completely different domain and path, so I could get a token outside of the constructor. Changing the basepath caused problems for my CI/CD builds for features and replacements.

So now my test is:

Scenario: Verify OAuth token works
        Given I have a valid access token
        When I GET /status
        Then response code should be 200

Here's what I did in my oauth.js step definition:

var calloutApickli = require('apickli');

module.exports = function () { 
    this.Given(/^I have a valid access token$/, function (callback) {
        var domain = this.apickli.scenarioVariables["tokenDomain"];
        var basepath = this.apickli.scenarioVariables["tokenBasepath"];
        var clientId = this.apickli.scenarioVariables["clientId"];
        var clientSecret = this.apickli.scenarioVariables["clientSecret"];
        
        var callout = new calloutApickli.Apickli('https', domain + basepath);
        callout.addHttpBasicAuthorizationHeader(clientId,clientSecret);
        callout.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        callout.setRequestBody("grant_type=client_credentials");
        
        var self = this;
        callout.post("/token", function (error, response) {
            if (error) {
                callback(new Error(error));
            }
            var token = callout.getAccessTokenFromResponseBodyPath("$.access_token");
            self.apickli.setAccessToken(token);
            self.apickli.setBearerToken();
            callback();
        });
    });

Thanks, Kurt Googler Kanaskie! This was really helpful, as in some of our apis we need to make a callout to get login credentials and tokens