Hypertest v2 Docs
HyperTest
  • Overview
    • How It Works?
  • SETUP GUIDE
    • Installation
      • Deploy HyperTest Server
      • Creating your first User
      • Adding your first service
      • Integrate SDK
        • Node.js
          • Node.js SDK with CJS
          • Node.js SDK with ESM
        • Java
    • Start a Test Run
      • CLI Login
      • Type References
      • Java
  • Interpreting Test Results
    • Test Results
    • Understanding Results Categories
    • Mock Not Found
    • AI Match Mocks
    • Accepting Changes
  • USER GUIDES
    • Node.js SDK
      • Limit memory usage
      • Supported NPM packages
      • Mock Dependencies Manually
      • Unmocking/Passing Through
      • Sampling and blocking requests
      • Manage Global Variables
      • Mocking Environment Variables
      • Tags
      • Set HTTP path patterns
      • Discard a test case(Request) while recording
      • Set Git Commit Hash
      • Code coverage based features
        • Continuous Coverage
        • Updating test coverage
        • Running post test deduplication
        • Only testing modified requests
        • Ignore differences for unmodified requests
      • Experimental flags
      • Manual Request
      • Only testing modified requests
      • Server hooks
    • Java SDK
      • Sampling and blocking requests
      • Mock Dependencies Manually
      • Tags
      • Unmocking/Passing Through
      • Code Coverage Setup and Report Generation
      • Supported Java packages
    • Build your own Docker Image
    • CLI Config
    • Ignoring Differences
      • Type References for Filter functions
  • Impact Features
    • Fast Mode
    • Code Coverage Report
    • Delete Recorded Requests
    • Inter Service Testing
  • Release History
    • Slack Integration
    • Version History
Powered by GitBook
On this page
  1. USER GUIDES
  2. Node.js SDK

Server hooks

Server hooks, what are they? How to implement them ?

Hypertest provides server hooks to intercept incoming server requests and their responses. This is valuable when your requests and responses are encoded or encrypted. You can intercept your requests and responses via the hooks and write the decoding/decryption logic in the hooks

How to implement ?

Http

Hypertest provides hooks for http request and response. There are two hooks each for both of them. A hook that runs in record mode and one that runs in replay mode.

  1. Http request, record and replay hooks

An http request hook in record mode would usually deal with decrypting your http request and an http request hook in replay mode would be responsible to do the exact opposite of record mode hook (i.e encrypt or encode the request for your controller to consume)

htSdk.hooks.httpServer.request.v1({
  beforeRecord({ readableInput, inputMeta, userMeta }) {
    // Write your decryption/decoding logic
    // console.log(readableInput, inputMeta, userMeta);

    return { inputMeta, readableInput, userMeta }
  },
  beforeReplay({ readableInput, inputMeta, userMeta }) {
    // Write your encoding/encryption logic
    // console.log(readableInput, inputMeta, userMeta);

    return { inputMeta, readableInput, userMeta };
  },
});
  1. Http response, record and replay hooks

The purpose of a response hook in record and replay modes is to decode/decrypt the http response for hypertest.

htSdk.hooks.httpServer.response.v1({
  beforeRecord({ realOutput, outputMeta, userMeta, }) {
    // Write your decryption/decoding logic
    // console.log(realOutput, outputMeta, userMeta);

    return { realOutput, outputMeta, userMeta }
  },
  beforeReplay({ realOutput, outputMeta, userMeta, }) {
    // Write your decryption/decoding logic
    // console.log(realOutput, outputMeta, userMeta);

    return { outputMeta, realOutput, userMeta };
  },
})

UserMeta

You can add any key value pairs in user meta. Its purpose is to facilitate you in marking certain requests. You can add a key value pair in record mode, and that would be given to you in the subsequent replay mode hooks. Then you can use the key value pairs present to take necessary actions.

PreviousOnly testing modified requestsNextJava SDK

Last updated 21 days ago