Restfox Documentation

repository·main·Indexed 25 days ago

https://github.com/flawiddsouza/restfox

An offline-first, minimalistic HTTP and Socket testing client for Web and Desktop environments. Restfox serves as a lightweight alternative for API testing, featuring a plugin system for pre-request and post-request scripts, environment variable management, and support for cURL imports. It can be deployed via Docker, Homebrew, Snap, Scoop, or as a standalone web application.

Tokens
17.7K
Snippets
50
Records
97
Agent score
83%

What's inside Restfox

  1. Overview of Restfox Plugins

    main

    Restfox provides a plugin system that allows you to extend functionality similar to Postman's pre-request and test scripts. You can use plugins to:

    • Modify request data before a request is sent.
    • Modify response data after a response is received.
    • Set environment variables based on response data.
    • Perform automated testing.
  2. Apply fetch polyfill for Tauri

    main

    When using Restfox with Tauri, you may need to apply a fetch polyfill to ensure compatibility with Tauri's HTTP plugin.

    After building the UI, locate the ui/assets/index.[hash].js file and prepend the following polyfill code to the top of the file. This polyfill redirects standard fetch calls to window.__TAURI__.http.fetch and handles body transformations for URLSearchParams and different HTTP methods.

    export async function fetch(input, init) {
        const fetch = window.__TAURI__.http.fetch
    
        const params = {
            ...init,
            body: {
                type: 'Text',
                payload: init.body
            }
        };
    
        if(params.body.payload instanceof URLSearchParams) {
            params.body.payload = params.body.payload.toString()
        }
    
        if(init.method === 'GET' || 'body' in init === false || init.body === null) {
            delete params.body
        }
    
        const res = await fetch(input.toString(), params)
    
        return new Response(JSON.stringify(res.data), res)
    }
  3. Use crypto-js in a Restfox plugin or script

    main

    To use the crypto-js library within a Restfox plugin or script, import it directly from esm.sh using the following URL. This allows you to perform encryption and decryption tasks, such as AES encryption, within your Restfox workflows.

    import CryptoJS from 'https://esm.sh/crypto-js@latest?target=es2020'
    
    var data = 'my string'
    const secretKey = 'secret key 123'
    
    // Encrypt
    var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), secretKey).toString()
    
    // Decrypt
    var bytes  = CryptoJS.AES.decrypt(ciphertext, secretKey)
    var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8))
    
    console.log(decryptedData) // output: my string
  4. Build and use Web Standalone

    main

    To build and run Restfox as a web-standalone application locally, follow these steps in order:

    1. Clone the repository.
    2. Build the UI package.
    3. Build the web-standalone package.
    4. Start the standalone application.

    By default, the application runs on port 4004. You can override this using the PORT environment variable.

    git clone https://github.com/flawiddsouza/Restfox
    cd Restfox/packages/ui
    npm i
    npm run build-web-standalone
    cd ../web-standalone
    npm i
    npm start
    
    # To override port
    PORT=5040 npm start
  5. Set environment variables using response data via plugins

    main

    You can automate the extraction of data from an API response (such as an access token) and store it in a Restfox environment variable by using a plugin. This allows subsequent requests to use the extracted value dynamically.

    To implement this, write a script that accesses the context.response object, parses the body text, and calls context.response.setEnvironmentVariable(name, value).

    function handleResponse() {
        const response = context.response.getBodyText()
        const responseData = JSON.parse(response)
        context.response.setEnvironmentVariable('MyAccessToken', responseData.accessToken)
    }
    
    if('response' in context) {
        handleResponse()
    }
  6. GZIP compress and decompress text in Restfox scripts

    main

    You can perform GZIP compression and decompression within a Restfox plugin or script by importing the pako library and utilizing the rf global object for buffer and base64 conversions.

    To compress a string, convert it to an ArrayBuffer, use pako.deflate(), and then convert the resulting Uint8Array to a base64 string.

    To decompress, convert a base64 string back to a Uint8Array, use pako.inflate(), and then convert the resulting buffer back to a string.

    import pako from 'https://unpkg.com/pako@2.1.0/dist/pako.esm.mjs?module'
    
    // compressing a string to a base64 gzipped string
    const buffer = rf.arrayBuffer.fromString('My Gzipped Text')
    const compressedBuffer = pako.deflate(buffer)
    const compressedText = rf.base64.fromUint8Array(compressedBuffer)
    console.log(compressedText) // output: eJzzrVQISa0oAQAJewKM
    
    // decompressing a base64 gzipped string
    const buffer2 = rf.base64.toUint8Array(compressedText)
    const decompressedGzip = pako.inflate(buffer2)
    const uncompressedText = rf.arrayBuffer.toString(decompressedGzip)
    console.log(uncompressedText) // output: My Gzipped Text
  7. Add a new cURL import test case

    main

    To add a new test case for the cURL import functionality, follow these steps:

    1. Create a new directory for the test case.
    2. Save the raw cURL command into a file named input.txt within that directory (no escaping required).
    3. Run the existing test suite once to observe the current output, then create an expected.json file containing the expected parsed object (the system uses toMatchObject for partial matching).
    4. (Optional) Create a name.txt file to provide a custom name for the test; otherwise, the directory name is used.
    mkdir my-test-case
    echo "curl ..." > my-test-case/input.txt
    # Run tests to generate output, then create expected.json
    echo "My Test Case #123" > my-test-case/name.txt
  8. Build a custom Restfox Docker image

    main

    To build your own Docker image from source:

    1. Follow the Web Standalone compilation steps first.
    2. From the project root, build the image using docker build with a version tag.
    3. Run the container using docker run.
    # Build (replace xx with version)
    docker build -t restfox:xx .
    
    # Run
    docker run -d -p:4004:4004 restfox:xx
  9. Compile the Restfox UI

    main

    Commands for developing and building the UI package.

    • Development: Run the dev server.
    • Distribution: Build the UI for production.
    • Desktop distribution: Build for desktop use.
    • Web Standalone distribution: Build for web-standalone use.
    # Development
    npm run dev
    
    # Distribution
    npm run build
    
    # Desktop distribution and development
    npm run build-desktop
    
    # Web Standalone distribution and development
    npm run build-web-standalone