Skip to content

Test your API by crawling the hypermedia links

Notifications You must be signed in to change notification settings

vickvu/hyperactive

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hyperactive

Small utility used to actively test your API by crawling the hypermedia links

Logo

Build Status Dependency Status devDependency Status

npm install

How does it work?

hyperactive crawls your API responses, and creates mocha tests for each unique link it finds. Simply pass in some basic config and it will do the rest.

var hyperactive = require('hyperactive');

describe("My API", function() {
  it("should be discoverable", function() {
    hyperactive.crawl({
      url: "http://myApiEndpoint.com/route",
      options: {
        headers: {
          Accept: "application/json"
        }
      }
    });
  })
})

Note: hyperactive needs to run as part of a mocha test suite. If you want to run it in a different context, just make sure it and describe are defined in the global scope.

More options

- How do I configure extra HTTP options?

For SSL and basicAuth, just add the following to the config:

var hyperactive = require('hyperactive');

describe("My API", function() {
  it("should be discoverable", function() {
    hyperactive.crawl({
      url: "http://myApiEndpoint.com/route",
      options: {
        headers: {
          Accept: "application/json"
        },
        auth : {
          user: "myUsername",
          pass: "myPassword"
        },
        secureProtocol : "SSLv3_client_method",
        strictSSL : false
      }
    });
  })
})

Note: hyperactive uses unirest to send requests. The options hash can contain any valid Request option from unirest.

- How does it find hypermedia links?

By default, hyperactive looks for links according to the HAL spec:

{
  "resource": {
    "name": "my resource",
    "id": 1,
    "_links": {
      "link1": {
        "href": "http://myApiEndpoint.com/route1"
      },
      "link2": {
        "href": "http://myApiEndpoint.com/route2"
      }
    }
  }
}

If you have a different format for links, you can pass your own link finder. For example, if your API returns the following:

{
  "resource": {
    "name": "my resource",
    "id": 1
  },
  "links": [
    "http://myApiEndpoint.com/route1",
    "http://myApiEndpoint.com/route2"
  ]
}

then you can call hyperactive with:

function getLinks(responseBody) {
  return responseBody.links;
}

hyperactive.crawl({
  url: "http://myApiEndpoint.com/route",
  options: {
    headers: {
      Accept: "application/json"
    },
  },
  getLinks: getLinks
});

The getLinks function receives a Unirest response and should return an array of links for that response. It's up to you to get these links recursively if you have links nested at several levels inside the response.

- How do I validate the responses?

By default hyperactive just checks that each response returns a HTTP 200 (i.e. res.ok === true). You can also pass custom validation function. For example, if you have the following response:

{
  "success": true,
  "resource": {
    "name": "my resource",
    "id": 1,
    "_links": {
      "link1": {
        "href": "http://myApiEndpoint.com/route1"
      },
      "link2": {
        "href": "http://myApiEndpoint.com/route2"
      }
    }
  }
}

Then you can validate it with:

function validate(url, responseBody) {
  if(url.match(/some-url/)) {
    return responseBody.success;
  }
  return true;
}

hyperactive.crawl({
  url: "http://myApiEndpoint.com/route",
  options: {
    headers: {
      Accept: "application/json"
    },
  },
  validate: validate
});

- Will it crawl EVERYTHING?

By default, yes. If that's taking too long, you can also crawl a percentage of all links:

hyperactive.crawl({
  url: "http://myApiEndpoint.com/route",
  options: {
    headers: {
      Accept: "application/json"
    },
  }
  samplePercentage: 75
});

How can I contribute?

The usual process:

npm install
npm test

And if everything is passing, submit a pull request :)

About

Test your API by crawling the hypermedia links

Resources

Stars

Watchers

Forks

Packages

No packages published