strest

repository·master·Indexed 23 days ago

https://github.com/eykrehbein/strest

A flexible REST API testing tool that allows developers to write complex test suites in YAML. It supports request chaining, data extraction via JsonPath, random data generation with Faker, and conditional execution. Strest provides multiple validation methods including exact match, regex, type checking, and JSON Schema, and allows for retrying requests until validation succeeds.

Tokens
7.8K
Snippets
31
Records
48
Agent score
82%

What's inside strest

  1. Configure requests in a test file

    master

    The requests key is a required object that holds all the requests to be tested. Each request is identified by a unique requestName. Avoid overwriting names, as this will overwrite the response data and prevent you from retrieving it later.

    requests:
      request1:
        ..
      request2:
        ..
  2. Pass objects between requests using Nunjucks

    master

    Strest uses Nunjucks for templating. You can pass complex objects from one request to another by using the dump filter and the safe filter within <$ $> tags.

    version: 2
    requests:
      objectSet:
        request:
          url: https://postman-echo.com/post
          method: POST
          postData:
            mimeType: application/json
            text:
              foo: bar
        log: true
      objectReset:
        request:
          url: https://postman-echo.com/post
          method: POST
          postData:
            mimeType: application/json
            text:
              new: <$ objectSet.content.data | dump | safe %>
        validate:
          - jsonpath: content.data
            expect: {"new":{"foo":"bar"}}
  3. Use Environment and Custom variables

    master

    You can inject values into your tests using system environment variables via Env("VAR_NAME") or by defining a variables block at the top level of your YAML file.

    # Using Environment Variables
    version: 2
    requests:
      environment:
        request:
          url: <$ Env("STREST_URL") $/todos/1
          method: GET
    
    # Using Custom Variables
    version: 2
    variables:
      testUrl: https://jsonplaceholder.typicode.com/todos/1
      to_log: true
    
    requests:
      my_variable_request:
        request:
          url: <$ testUrl $> 
          method: GET
        log: <$ to_log $> 
  4. Validate responses with exact match, regex, type, or jsonschema

    master

    Strest allows you to validate API responses using several methods within a .strest.yml file. You can target specific parts of the response using jsonpath. Supported validation methods include:

    • Exact match (expect): Validates that a value matches a specific string or object exactly.
    • Regex (regex): Validates a value against a regular expression pattern.
    • Type (type): Validates that a value matches one or more specified types (e.g., string, number, boolean).
    • JSON Schema (jsonschema): Validates the response against a JSON/YAML schema. The schema can be defined inline or passed from a variable using Nunjucks templating.
    # Example of multiple validation types
    requests:
      typeValidate:
        request:
          url: https://jsonplaceholder.typicode.com/todos
          method: GET
        validate:
        - jsonpath: headers["content-type"]
          type: [ string ]
        - jsonpath: status
          type: [ boolean, string, number ]
        - jsonpath: content.0.userId
          type: [ number ]
  5. Chain multiple requests using response data

    master

    You can pass data from one request to another by referencing the previous request's response. Strest stores responses in a HAR-like dictionary. Use the syntax <$ requestName.content.jsonKey $> to inject data into headers, URLs, or query parameters. To use the raw response content, use <$ requestName.content $>.

    version: 2
    
    requests:
      login:
        request:
          url: https://postman-echo.com/login
          method: POST
          # ... login logic
      authNeeded:
        request:
          url: https://postman-echo.com/secure-data
          method: GET
          headers:
          - name: Authorization
            value: Bearer <$ login.content.token $> 
  6. Configure request retries with maxRetries

    master
    The maxRetries option allows a request to continue executing until a response that passes validation is received. For best results, combine maxRetries with a delay to avoid overwhelming the server during retry attempts.
  7. Define custom variables

    master

    You can define custom variables using the variables key. These variables can be used across different files and within requests using the <$ variable_name $> syntax. This is useful for reusing URLs, IDs, or tokens.

    # Example
    variables:
      example_url: https://jsonplaceholder.typicode.com/todos/1
      example_id: 1
    
    requests:
      test:
        request:
          url: <$ example_url $>
          ...
  8. Run Strest tests via CLI

    master
    To execute tests, point the strest command to a specific .strest.yml or .strest.yaml file, a directory of test files, or simply run strest to recursively search the current working directory and its subdirectories for all test files.
  9. Retry requests until validation succeeds

    master

    If you are testing an asynchronous process or an endpoint that takes time to reach a certain state, you can use delay and maxRetries to retry the request until the validate conditions are met.

    requests:
      waiter:
        request:
          url: https://postman-echo.com/time/now
          method: GET
        delay: 900
        maxRetries: 30
        validate:
        - jsonpath: status
          expect: 200
  10. Install the Strest CLI

    master

    You can install the @strest/cli package globally using npm or yarn to run REST tests from your terminal. Alternatively, you can use Docker to run tests without local installation.

    # Via npm
    npm i -g @strest/cli
    
    # Via Yarn
    yarn global add @strest/cli
  11. Use JSON Schema for response validation

    master

    You can validate a response against a JSON schema. You can define the schema in the variables section of your file and reference it using Nunjucks syntax <$ schemaName | dump | safe $>, or define it directly within the validate block of a request.

    version: 2
    variables:
      schemaValidate:
        properties:
          fruits:
            type: array
            items:
              type: string
    requests:
      jsonschema1:
        request:
          url: https://postman-echo.com/post
          method: POST
        validate:
        - jsonpath: content.data
          jsonschema: <$ schemaValidate | dump | safe $>