Git Product home page Git Product logo

strest's Introduction

🚀 Flexible REST Tests

License: MIT License: MIT

🔗 Connect multiple requests: Example Embed an authorization token you got as a response from a login request in your following requests automatically

📝 YAML Syntax: Write all of your tests in YAML files

🎉 Easy to understand: You'll understand the concept in seconds and be able to start instantly (seriously!)

Getting Started

Install Strest using yarn

yarn global add strest-cli

Or via npm

npm i -g strest-cli

To test something, you have to create a REST-API first. If you already have an API to test, you can skip this step.

We'll be using express in this tutorial to create a simple API endpoint but you can use whatever you want

Let's get started by creating a simple endpoint first

const express = require('express');

const app = express();

app.get('/user', (req, res) => {
  res.send({
    username: req.query.name,
    id: 123,
    premium: false,
  })
})

app.listen(3001)

Then, create a file called tutorial.strest.yaml (You can name it however you want as long as it ends with .strest.yaml)

version: 1                            # only version at the moment

requests:                             # all test requests will be listed here
  userRequest:                        # name the request however you want
    url: http://localhost:3001/user   # required
    method: GET                       # required
    data:                             # valid data types: params, json and raw
      params:
        name: testUser
    log: true                         # false by default. This will log all response information in the console
    

No more configuration needed, so you're ready to go!

To run the test, open your terminal and type

strest tutorial.strest.yaml

You may also run multiple test files at the same time by pointing to the directory, where the files are stored

strest
// or
strest someDir/

Success! If you've done everything correctly, you'll get a response like this

[ Strest ] Found 1 test file(s)
[ Strest ] Schema validation: 1 of 1 file(s) passed

✔ Testing userRequest succeeded
Status: 200
Status Text: OK

Headers:

{
  "x-powered-by": "Express",
  "content-type": "application/json; charset=utf-8",
  "content-length": "48",
  "etag": "W/\"30-lW4maan3YXQcTCT4eKF1mDtPx3Y\"",
  "date": "Sun, 09 Sep 2018 11:20:19 GMT",
  "connection": "close"
}

Data:

{
  "username": "testUser",
  "id": 123,
  "premium": false
}


[ Strest ] ✨  Done in 0.046s

Writing .strest.yaml test files

You can find a full Documentation of how to write tests here

Documentation

Using & Connecting multiple requests

With traditional tools like Postman or Insomnia it's common to perform only a single request at a time. Moreover, you have to trigger each request on your own with a click on a button.

With Strest you're able to predefine a very well structured test file once, and every time you make any changes to your API you can test it with just one command in your terminal. Additionally, you can add hundreds or thousands of requests and endpoints which will run synchronously one after the other.

To create multiple requests, simply add multiple entries into the requests yaml object.

version: 1

requests:
  requestOne:
    ...
  requestTwo:
    ...
  requestThree:
    ...

Running this will result in something like

[ Strest ] Found 1 test file(s)
[ Strest ] Schema validation: 1 of 1 file(s) passed

✔ Testing requestOne succeeded
✔ Testing requestTwo succeeded
✔ Testing requestThree succeeded

[ Strest ] ✨  Done in 0.12s

Connecting multiple requests

What is meant by connecting multiple requests?
Connecting multiple requests means that you write a request and in each of the following requests you are able to use and insert any of the data that was responded by this request.

Usage

requests:
  
  login: # will return { token: "someToken" }
    ...

  authNeeded:
    ...
    headers:
      Authorization: Bearer Value(login.token)

As you could see, the usage is very simple. Just use Value(requestName.jsonKey) to use any of the JSON data that was retrieved from a previous request. If you want to use raw data, just use Value(requestName) without any keys.

You can use this syntax anywhere regardless of whether it is inside of some string like https://localhost/posts/Value(postKey.key)/... or as a standalone term like Authorization: Value(login.token)

Response Validation

With Strest you can validate respones either by a specific value or by a Type. List of all valid Types

Raw Validation

requests:
  example:
    ...
    validate:
      raw: "the response has to match this string exactly"

JSON Validation

requests:
  example:
    ...
    validate:
      json:
        user: 
          name: Type(String) # name has to be of type String
          id: Type(Null | Number | String) # id has to be of type Number, String or Null
          iconUrl: Type(String.Url)
        someOtherData: "match this string" 

Errors

Strest is a testing library so of course, you'll run into a few errors when testing an endpoint. Error handling is made very simple so can instantly see what caused an error and fix it.

Example of a Validation Error

[ Strest ] Found 1 test file(s)
[ Strest ] Schema validation: 1 of 1 file(s) passed

✖ Testing test failed

[ Validation ] The required item test wasn't found in the response data

[ Strest ] ✨  Done in 0.045s

License

Strest is MIT Licensed

strest's People

Contributors

eykrehbein avatar

Watchers

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