UNPKG

sensai

Version:

Because even AI needs a master

197 lines (196 loc) 7.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _nodetest = /*#__PURE__*/ _interop_require_default(require("node:test")); const _nodeassert = /*#__PURE__*/ _interop_require_default(require("node:assert")); const _nodestream = require("node:stream"); const _body = /*#__PURE__*/ _interop_require_wildcard(require("./body")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } // Utility to create a mock request function createMockRequest(body, headers = {}) { const mockRequest = new _nodestream.Readable(); mockRequest.push(body); mockRequest.push(null); mockRequest.headers = { "content-length": Buffer.byteLength(body).toString(), ...headers }; return mockRequest; } (0, _nodetest.default)("should parse JSON content type correctly", async ()=>{ const jsonData = { name: "test", value: 123 }; const request = createMockRequest(JSON.stringify(jsonData), { "content-type": "application/json" }); const result = await (0, _body.default)(request); _nodeassert.default.deepEqual(result, jsonData); }); (0, _nodetest.default)("should parse URL-encoded form data correctly", async ()=>{ // Include all primitive types in URL-encoded format const formData = "string=hello&number=123&zero=0&negative=-10&float=3.14&boolean_true=true&boolean_false=false&empty=&undefined=&null=null&array=1,2,3&special=hello%20world%21"; const request = createMockRequest(formData, { "content-type": "application/x-www-form-urlencoded" }); const result = await (0, _body.default)(request); _nodeassert.default.deepEqual(result, { string: "hello", number: "123", zero: "0", negative: "-10", float: "3.14", boolean_true: "true", boolean_false: "false", empty: "", undefined: "", null: "null", array: "1,2,3", special: "hello world!" }); }); (0, _nodetest.default)("should handle empty body", async ()=>{ const request = createMockRequest("", { "content-type": "application/json" }); const result = await (0, _body.default)(request); _nodeassert.default.deepStrictEqual(result, {}); }); (0, _nodetest.default)("should handle missing content-type header by returning raw body as string", async ()=>{ const body = "raw content"; const request = createMockRequest(body); const result = await (0, _body.default)(request); _nodeassert.default.deepEqual(result, { raw: body }); }); (0, _nodetest.default)("should reject with error for invalid JSON", async ()=>{ const invalidJson = '{ "name": "test", value: 123 }'; // Missing quotes around value const request = createMockRequest(invalidJson, { "content-type": "application/json" }); await _nodeassert.default.rejects(async ()=>await (0, _body.default)(request), (err)=>{ _nodeassert.default.ok(err instanceof Error); _nodeassert.default.ok(err.message.includes("JSON")); return true; }); }); (0, _nodetest.default)("should handle malformed URL-encoded data gracefully", async ()=>{ const malformedData = "name=test&value=123&invalid"; // Missing value for 'invalid' const mockReq = createMockRequest(malformedData, { "content-type": "application/x-www-form-urlencoded" }); const result = await (0, _body.default)(mockReq); _nodeassert.default.deepEqual(result, { name: "test", value: "123", invalid: "" }); }); (0, _nodetest.default)("should handle content type with various charset parameters", async ()=>{ const jsonData = { name: "test", value: 123 }; const jsonString = JSON.stringify(jsonData); // Test multiple common charsets const charsets = [ "utf-8", "ascii", "latin1", "UTF-8", "iso-8859-1", "ISO-8859-1" ]; for (let charset of charsets){ // Try to encode with the specified charset const encodedString = Buffer.from(jsonString).toString((0, _body.normalizeCharset)(charset)); console.log(`application/json; charset=${charset}`); const mockReq = createMockRequest(encodedString, { "content-type": `application/json; charset=${charset}` }); try { const result = await (0, _body.default)(mockReq); _nodeassert.default.deepEqual(result, jsonData, `Failed with charset: ${charset}`); } catch (error) { console.log(error); } } }); (0, _nodetest.default)("should throw 400 exception if data byte length does not match Content-Length", async ()=>{ const mockReq = createMockRequest(JSON.stringify({ name: "test", value: 123 }), { "content-type": `application/json`, "content-length": "3" }); try { await (0, _body.default)(mockReq); _nodeassert.default.fail("Expected error not thrown"); } catch (error) { _nodeassert.default.equal(error.code, 400); } }); (0, _nodetest.default)("should throw 413 exception if Content-Length greater than limit", async ()=>{ const mockReq = createMockRequest(JSON.stringify({ name: "test", value: 123 }), { "content-type": `application/json`, "Content-Length": "3" }); try { await (0, _body.default)(mockReq, 2); _nodeassert.default.fail("Expected error not thrown"); } catch (error) { _nodeassert.default.equal(error.code, 413); } });