npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

taws

v1.3.1

Published

A library for testing responses of requests

Downloads

2

Readme

Taws - simple tester of your http respones

NPM

Library for runnning tests suite on http request responses.

Getting Started

  1. Install Taws in your project $ npm intall taws
  2. 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);
    });
  1. 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

  1. 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.

  2. 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);
    }