From e71c3ce380f22558ebc936e8bc54a7be3b2d0508 Mon Sep 17 00:00:00 2001 From: Filip Hric Date: Fri, 3 Feb 2023 13:11:16 +0100 Subject: [PATCH 1/2] fix: incorrect parsing of JSON formats --- cypress/e2e/formats.cy.ts | 10 ++++++++++ server/index.js | 5 +++++ src/utils/isValidJson.ts | 16 ++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cypress/e2e/formats.cy.ts b/cypress/e2e/formats.cy.ts index e121d43..20ffc78 100644 --- a/cypress/e2e/formats.cy.ts +++ b/cypress/e2e/formats.cy.ts @@ -30,6 +30,16 @@ describe('response formats', () => { }); + it('works with json that does not contain proper header', () => { + + cy.api('/json-weird') + + // numbers in json are formatted + cy.contains('1234') + .should('have.css', 'color', 'rgb(31, 169, 113)') + + }); + it('works with text', () => { cy.api({ diff --git a/server/index.js b/server/index.js index d8ca479..f98196a 100644 --- a/server/index.js +++ b/server/index.js @@ -84,6 +84,11 @@ app.get('/xml', (req, res) => { res.send(answerXML) }) +app.get('/json-weird', (req, res) => { + res.set('Content-Type', 'application/abcd+json'); + res.send(answerJSON) +}) + app.get('/undefined', (req, res) => { const answerXML = "XML" res.send(answerXML) diff --git a/src/utils/isValidJson.ts b/src/utils/isValidJson.ts index 3a87199..fd5b660 100644 --- a/src/utils/isValidJson.ts +++ b/src/utils/isValidJson.ts @@ -1,8 +1,16 @@ -export const isValidJson = (str: string) => { - try { - JSON.parse(str); +export const isValidJson = (input: any): boolean => { + // all objects are JSONs + if (typeof input === 'object') { return true; - } catch (e) { + } else if (typeof input === 'string') { + // strings are tested for JSON format + try { + JSON.parse(input); + return true; + } catch (e) { + return false; + } + } else { return false; } } \ No newline at end of file From 47e21cf8f2543801d4bf61e4d7aae30dc2e9f5aa Mon Sep 17 00:00:00 2001 From: Filip Hric Date: Fri, 3 Feb 2023 13:12:45 +0100 Subject: [PATCH 2/2] chore(release): 2.10.3 --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eee44c9..d0e0c32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [2.10.3](https://github.com/filiphric/cypress-plugin-api/compare/v2.10.2...v2.10.3) (2023-02-03) + + +### Bug Fixes + +* incorrect parsing of JSON formats ([e71c3ce](https://github.com/filiphric/cypress-plugin-api/commits/e71c3ce380f22558ebc936e8bc54a7be3b2d0508)) + ### [2.10.2](https://github.com/filiphric/cypress-plugin-api/compare/v2.10.1...v2.10.2) (2023-02-03) diff --git a/package-lock.json b/package-lock.json index aef6b89..5684f91 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cypress-plugin-api", - "version": "2.10.2", + "version": "2.10.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "cypress-plugin-api", - "version": "2.10.2", + "version": "2.10.3", "license": "ISC", "dependencies": { "highlight.js": "11.4.0", diff --git a/package.json b/package.json index a397399..c41cf6e 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Filip Hric (https://filiphric.com/)", "license": "ISC", "name": "cypress-plugin-api", - "version": "2.10.2", + "version": "2.10.3", "keywords": [ "cypress", "api",