How to use the nock/lib/common.isUtf8Representable function in nock

To help you get started, we’ve selected a few nock 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 Netflix / pollyjs / packages / @pollyjs / adapter-node-http / src / index.js View on Github external
}

    // If content-encoding is set in the header then the body/content
    // is as an array of hex strings
    if (isContentEncoded(headers)) {
      const hexChunks = JSON.parse(body);

      return hexChunks.map(chunk => Buffer.from(chunk, 'hex'));
    }

    const buffer = Buffer.from(body);

    // The body can be one of two things:
    //  1. A hex string which then means its binary data.
    //  2. A utf8 string which means a regular string.
    return [Buffer.from(buffer, isUtf8Representable(buffer) ? 'utf8' : 'hex')];
  }
}
github Netflix / pollyjs / packages / @pollyjs / adapter-node-http / src / index.js View on Github external
);
          chunk = Buffer.from(chunk);
        }

        return chunk.toString('hex');
      });

      return JSON.stringify(hexChunks);
    }

    const buffer = mergeChunks(chunks);

    // The merged buffer can be one of two things:
    //  1. A binary buffer which then has to be recorded as a hex string.
    //  2. A string buffer.
    return buffer.toString(isUtf8Representable(buffer) ? 'utf8' : 'hex');
  }
github Netflix / pollyjs / packages / @pollyjs / adapter-node-http / src / index.js View on Github external
interceptor.intercept(/.*/, m).reply(function(_, body, respond) {
        const { req, method } = this;
        const { headers } = req;
        const parsedArguments = normalizeClientRequestArgs(
          ...REQUEST_ARGUMENTS.get(req)
        );
        const url = getUrlFromOptions(parsedArguments.options);

        if (body) {
          if (
            typeof body === 'string' &&
            !isUtf8Representable(Buffer.from(body, 'hex'))
          ) {
            // Nock internally converts a binary buffer into its hexadecimal
            // representation so convert it back to a buffer.
            body = Buffer.from(body, 'hex');
          } else if (isJSONContent(headers)) {
            // Nock will parse json content into an object. We have our own way
            // of dealing with json content so convert it back to a string.
            body = JSON.stringify(body);
          }
        }

        adapter.handleRequest({
          url,
          method,
          headers,
          body,