Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

Added test spec for helpers/util/contenttypes.js

Andrew Bastine0e2e0c2fb90
diff --git a/helpers/utils/__tests__/contenttypes.spec.js b/helpers/utils/__tests__/contenttypes.spec.js
new file mode 100644
index 000000000..48f9d486e
--- /dev/null
+++ b/helpers/utils/__tests__/contenttypes.spec.js
@@ -0,0 +1,32 @@
+import { isJSONContentType } from "../contenttypes"
+
+describe("isJSONContentType", () => {
+ test("returns true for JSON content types", () => {
+ expect(isJSONContentType("application/json")).toBe(true)
+ expect(isJSONContentType("application/vnd.api+json")).toBe(true)
+ expect(isJSONContentType("application/hal+json")).toBe(true)
+ })
+
+ test("returns true for JSON types with charset specified", () => {
+ expect(isJSONContentType("application/json; charset=utf-8")).toBe(true)
+ expect(isJSONContentType("application/vnd.api+json; charset=utf-8")).toBe(true)
+ expect(isJSONContentType("application/hal+json; charset=utf-8")).toBe(true)
+ })
+
+ test("returns false for non-JSON content types", () => {
+ expect(isJSONContentType("application/xml")).toBe(false)
+ expect(isJSONContentType("text/html")).toBe(false)
+ expect(isJSONContentType("application/x-www-form-urlencoded")).toBe(false)
+ })
+
+ test("returns false for non-JSON content types with charset", () => {
+ expect(isJSONContentType("application/xml; charset=utf-8")).toBe(false)
+ expect(isJSONContentType("text/html; charset=utf-8")).toBe(false)
+ expect(isJSONContentType("application/x-www-form-urlencoded; charset=utf-8")).toBe(false)
+ })
+
+ test("returns false for null/undefined", () => {
+ expect(isJSONContentType(null)).toBe(false)
+ expect(isJSONContentType(undefined)).toBe(false)
+ })
+})