feat: better media types detection for JSON, XML and HTML lenses (#1438)
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com> Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
This commit is contained in:
@@ -5,24 +5,28 @@ describe("isJSONContentType", () => {
|
||||
expect(isJSONContentType("application/json")).toBe(true)
|
||||
expect(isJSONContentType("application/vnd.api+json")).toBe(true)
|
||||
expect(isJSONContentType("application/hal+json")).toBe(true)
|
||||
expect(isJSONContentType("application/ld+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)
|
||||
expect(isJSONContentType("application/ld+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)
|
||||
expect(isJSONContentType("foo/jsoninword")).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)
|
||||
expect(isJSONContentType("foo/jsoninword; charset=utf-8")).toBe(false)
|
||||
})
|
||||
|
||||
test("returns false for null/undefined", () => {
|
||||
|
||||
@@ -10,19 +10,5 @@ export const knownContentTypes = [
|
||||
]
|
||||
|
||||
export function isJSONContentType(contentType) {
|
||||
if (contentType && contentType.includes(";")) {
|
||||
const [justContentType] = contentType.split(";")
|
||||
|
||||
return (
|
||||
justContentType === "application/json" ||
|
||||
justContentType === "application/vnd.api+json" ||
|
||||
justContentType === "application/hal+json"
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
contentType === "application/json" ||
|
||||
contentType === "application/vnd.api+json" ||
|
||||
contentType === "application/hal+json"
|
||||
)
|
||||
}
|
||||
return /\bjson\b/i.test(contentType);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user