Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hyperactive

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hyperactive

Creates mocha tests for all hypermedia of your API

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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.

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 :)

FAQs

Package last updated on 12 Aug 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc