Socket
Book a DemoInstallSign in
Socket

taws

Package Overview
Dependencies
Maintainers
4
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

taws

A library for testing responses of requests

latest
Source
npmnpm
Version
1.3.1
Version published
Weekly downloads
2
Maintainers
4
Weekly downloads
 
Created
Source

Taws - simple tester of your http respones

NPM

Library for runnning tests suite on http request responses.

Getting Started

  • Install Taws in your project $ npm intall taws
  • Copy & paste below code
    const { TestsRunner } = require('taws');
    //example tests suite configuration
    const params = {
        "name" : "example_tests_suite",
        "config" : [{
            "type" : "request",
            "options" : {
                "method" : "GET",
                "url" : "https://restcountries.eu/rest/v2/name/Poland",
                "headers" : {}
            },
            "tests" : [{
                "type" : "regexp",
                "key" : "[0].name",
                "value" : "Poland"
            }]
        }]
    };
    
    const runner = new TestsRunner();
    let promiseChain = runner.run(params.config);
    
    promiseChain
        .then((result) => {
            console.info('TEST - %s - name=%s tests_overall=%s tests_failed=%s duration=%s ms',
                result.testsSuiteId,
                params.name,
            result.testsRun,
                result.testsFail,
            result.endTime - result.startTime
            )
        })
        .catch((error) => {
            console.error(error);
    });
  • Run your code with node>6.x.x

By default, logging of processing tests is enabled, if you want to disable this, pass object with silentModel property set to true to TestRunner constructor.

    //example tests suite configuration
    const options={
        silentMode: true
    };
    
    const runner = new TestsRunner(options);

Tests suite definition

Taws TestsRunner run method accepts tests suite to run.

Example of tests suite definition:

{
    "name" : "pulse2story_save",
    "config" : [{
            "type" : "request",
            "options" : {
                "method" : "GET",
                "url" : "https://restcountries.eu/rest/v2/name/Poland",
                "headers" : {
                    "cache-control" : "no-cache"
                }
            },
            "tests" : [{
                    "type" : "regexp",
                    "key" : "[0].name",
                    "value" : "Poland"
                }
            ]
        },
        {
            "type": "delay",
            "time": 6000
        }
    ]
}

Tests suite consists of two keys: name and config

name is a name of tests suite config is a set of action definitions to perform within the tests suite. See action-definition for more information.

Action definition

Tests suite configuration consist of actions that are executed synchronously in defined order.

There are two types of actions to use

  • request type - run http request

    Requires options key with request definition ( see https://github.com/request/request )

    {
        "type" : "request",
        "options" : {
            "method" : "GET",
            "url" : "https://restcountries.eu/rest/v2/name/Poland",
            "headers" : {
                "cache-control" : "no-cache"
            }
    }
    

    If you want to retry request when error occurs, add retries key with the desired number of retries

    {
        "type" : "request",
        "retries": 2,
        "options" : {
            "method" : "GET",
            "url" : "https://restcountries.eu/rest/v2/name/Poland",
            "headers" : {
                "cache-control" : "no-cache"
            }
    }
    

    This action accepts a tests definition to run on a response of request.

    See test-definition section.

  • delay type - wait specified time before executing next action

    Requires time key with number of miliseconds to wait

     {
         "type": "delay",
         "time": 6000
     }
    
Tests definition

Every "request" step allows to specify tests, that will be run in order to check correctness of the response. Tests property is an array, where every object represents one condition.

Example:

    "tests" : 
    [{
        "type" : "regexp",
        "key" : "[0].name",
        "value" : "Poland"
        }, {
        "type" : "regexp",
        "key" : "[0].capital",
        "value" : "Warsaw"
        }, {
        "type" : "regexp",
        "key" : "[0].region",
        "value" : "(Europe|europe)"
        }]

Single test definition consist of following key:

type - Type of test. Only regexp type is available. key - Path in response's object, which value will be tested against regexp value - Regexp to test the value.

Accessing responses of previous requests

Every request action and tests definition can use data from previous requests response. Responses of previous requests are available under response[index] variable, where index corresponds to requests order.

For example, to access response of first request use response[0] variable, for second response[1] etc..

Below you find common use examples:

Using name key from response of first request in URL of the next

     {
         "type" : "request",
         "options" : {
             "method" : "POST",
             "url" : "https://myexampleapiendoint.com/${response[0].name}",
             "headers" : {
                 "cache-control" : "no-cache",
                 "my-custom-header": "example-header"
             },
             "body" : {
                 "data": {
                    "age": 25
                 }
             }
         }
     }

Validating tests suite

To validate tests suite definition, validateConfig method is available.

Arguments:

testConfig - test suite definition throwError - decides if excepion should be trowed when validation of tests suite fail

Example:

    const { validateConfig } = require('taws');

    try {
        validateConfig(testConfig, true);
    } catch (error) {
        console.warn('Schema validation error:', error);
    }

Keywords

http request

FAQs

Package last updated on 11 Jul 2019

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