How to use the msw.rest.post function in msw

To help you get started, we’ve selected a few msw examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pipe-cd / pipe / pkg / app / web / src / mocks / create-handler.ts View on Github external
export function createHandlerWithError(
  serviceName: string,
  statusCode: number
): HandlerType {
  return rest.post(createMask(serviceName), (_, res, ctx) => {
    return res(
      ctx.status(200),
      ctx.set({
        "content-length": "0",
        "content-type": "application/grpc-web+proto",
        "grpc-message": "Error Message",
        "grpc-status": `${statusCode}`,
      })
    );
  });
}
github pipe-cd / pipe / pkg / app / web / src / mocks / create-handler.ts View on Github external
export function createHandler(
  serviceName: string,
  getResponseMessage: (requestData: Uint8Array) => T
): HandlerType {
  return rest.post(
    createMask(serviceName),
    (req, res, ctx) => {
      const arr: Uint8Array =
        typeof req.body === "string" ? encoder.encode(req.body) : req.body;
      const message = getResponseMessage(arr.slice(5));
      const data = serialize(message.serializeBinary());
      return res(
        ctx.status(200),
        ctx.set("Content-Type", "application/grpc-web+proto"),
        ctx.body(data)
      );
    }
  );
}
github pipe-cd / pipe / pkg / app / web / src / mocks / create-handler.ts View on Github external
import { rest } from "msw";
import { serialize } from "./serializer";
import { createMask } from "./utils";

const DummyHandler = rest.post("", (_0, res, ctx) => {
  return res(ctx.status(200));
});

type HandlerType = typeof DummyHandler;

const encoder = new TextEncoder();

export function createHandler(
  serviceName: string,
  getResponseMessage: (requestData: Uint8Array) => T
): HandlerType {
  return rest.post(
    createMask(serviceName),
    (req, res, ctx) => {
      const arr: Uint8Array =
        typeof req.body === "string" ? encoder.encode(req.body) : req.body;

msw

Seamless REST/GraphQL API mocking library for browser and Node.js.

MIT
Latest version published 7 days ago

Package Health Score

88 / 100
Full package analysis