github

kiranz / just-api

  • понедельник, 25 июня 2018 г. в 00:15:00
https://github.com/kiranz/just-api

JavaScript
Specification based API test framework for HTTP APIs (REST, GraphQL)



Just-API

npm package

Join the chat at https://gitter.im/just-api/Lobby

Just-API is a robust, specification based, codeless testing framework that tests REST, GraphQL (or any HTTP based) APIs. It runs on node.js. Just-API allows users to test APIs without writing code. It takes API test specification from YAML files and runs them either in serial mode or in parallel mode as instructed by the user. It also reports errors and test results in several formats including HTML and JSON.

In simple terms, how it works is that you provide request and response validation specification in an yaml file. Just-API builds the request, makes a call to server and validates response as per the specification. You can choose to validate any or all of response status code, headers, JSON data, JSON schema or provide your own custom validator function.

Find more about it here

Links

Features

Getting Started

Following is a simple example showing usage of Just-API.

To run just-api, you will need Node.js v7.10.0 or newer.

$ npm install just-api
$ mkdir specs
$ $EDITOR specs/starwars_service.yml # or open with your preferred editor

In your editor (make sure yaml is properly indented)

meta:
  name: Star Wars suite
configuration:
  scheme: https
  host: swapi.co
  base_path: /api
specs:
  - name: get "Luke Skywalker" info
    request:
      path: /people/1/
      method: get
    response:
      status_code: 200
      headers:
        - name: content-type
          value: !!js/regexp application/json     
      json_data:
        - path: $.name
          value: Luke Skywalker

Back in the terminal

$ ./node_modules/.bin/just-api

   ✓ get Luke Skywalker info (1516ms)

  Done: specs/suite.yml (Passed)

0 skipped, 0 failed, 1 passed (1 tests)
0 skipped, 0 failed, 1 passed (1 suites)
Duration: 1.6s

Testing GraphQL APIs

Following example tests a GraphQL API that returns location for a given ip address.

Create the yaml suite file and run just-api.

meta:
  name: GraphQL location service
configuration:
  host: api.graphloc.com
  scheme: https
specs:
  - name: Get Location of a an ip address
    request:
      method: post
      path: /graphql
      headers:
        - name: content-type
          value: application/json
      payload:
        body:
          type: json
          content:
            query: >
                   {
                    getLocation(ip: "8.8.8.8") {
                      country {
                        iso_code
                      }
                     }
                    }
            variables: null
            operationName: null
    response:
      status_code: 200
      json_data:
        - path: $.data.getLocation.country.iso_code
          value: "US"

A complex suite

When you need to test complex chained API flows, you can run dependencies in hooks to fetch prerequisite data and pass it to actual test.

Following example shows how to get data from dependencies with hooks and using custom validator functions to validate the response.

meta:
  name: Starwars suite
configuration:
  scheme: https
  host: swapi.co
  base_path: /api
specs:
  - name: get R2-D2 info
    request:
      path: /people/3/
      method: get
    response:
      status_code: 200
      json_data:
        - path: $.name
          value: R2-D2

  - name: search R2-D2 info
    before_test:
      run_type: inline
      inline:
        function: !js/asyncFunction >
          async function() {
            var response = await this.runSpec('get R2-D2 info');
            var jsonData = JSON.parse(response.body);
            this.test.query_params = { name:  jsonData.name };
          }
    request:
      path: /people
      method: get
    response:
      status_code: 200
      custom_validator:
        run_type: inline
        inline:
          function: !!js/function >
            function() {
              var jsonData = JSON.parse(this.response.body);
              var r2d2 = jsonData.results.find(result => result.name === 'R2-D2');
              
              if (!r2d2) throw new Error('R2-D2 not returned in search results');
            }

Note that you can also place your custom JS functions in separate JS file and specify the function name in YAML to import.

You can do much more advanced stuff with Just-API. Our documentation says it all. Take a look at Just-API Website for detailed documentation.

License

MIT