GithubHelp home page GithubHelp logo

melongbob / just-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kiranz/just-api

0.0 1.0 0.0 1.15 MB

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

Home Page: https://kiranz.github.io/just-api/

License: MIT License

JavaScript 94.78% CSS 5.22%

just-api's Introduction

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, you provide request and response validation specification in a 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

Links

Features

  • Runs test suites in parallel/serial mode
  • Supports all widely used HTTP methods
  • Supports Form, Multipart requests, File uploads
  • Response Validation (Headers, Status code, JSON body, JSON schema, Custom Response Validation function)
  • Runs imported custom functions from modules
  • Custom inline and module Javascript sync/async functions
  • Hooks (Before all, After all, Before each, After each, Before test, After test)
  • Custom suite configuration
  • Chained Request flows
  • Ability to define/override Request path, query params, path params, headers, body at runtime
  • Storing suite & test data in context for reuse
  • Importing specs from multiple suites
  • Intrasuite and Intersuite spec dependency execution
  • Reusing test specification
  • Retry failed tests
  • Looping: Generate 'n' number of tests with a list
  • Built-in HTML, JSON reporters
  • Ability to generate reports in multiple formats for the same run
  • Logging HTTP request/response data for failed tests
  • Proper error reporting
  • Runs only tests matching with a given pattern/string
  • Skipping tests with specification
  • Disable or Enable redirections
  • Reports test duration
  • Allows user to plug-in custom reporters

Find out all 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"

Chained Dynamic request construction with hooks and Custom response validation

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.

Maintainer

Kiran [email protected]

License

Just-API is MIT-licensed

just-api's People

Contributors

gitter-badger avatar kiranz avatar melongbob avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.