Skip to content

Commit 3dc31ca

Browse files
authored
Merge pull request #112 from filiphric/109-plugin-fails-with-areplace-is-not-a-function
2 parents 8da620c + 47e21cf commit 3dc31ca

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
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.
44

5+
### [2.10.3](https://github.com/filiphric/cypress-plugin-api/compare/v2.10.2...v2.10.3) (2023-02-03)
6+
7+
8+
### Bug Fixes
9+
10+
* incorrect parsing of JSON formats ([e71c3ce](https://github.com/filiphric/cypress-plugin-api/commits/e71c3ce380f22558ebc936e8bc54a7be3b2d0508))
11+
512
### [2.10.2](https://github.com/filiphric/cypress-plugin-api/compare/v2.10.1...v2.10.2) (2023-02-03)
613

714

cypress/e2e/formats.cy.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ describe('response formats', () => {
3030

3131
});
3232

33+
it('works with json that does not contain proper header', () => {
34+
35+
cy.api('/json-weird')
36+
37+
// numbers in json are formatted
38+
cy.contains('1234')
39+
.should('have.css', 'color', 'rgb(31, 169, 113)')
40+
41+
});
42+
3343
it('works with text', () => {
3444

3545
cy.api({

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Filip Hric (https://filiphric.com/)",
33
"license": "ISC",
44
"name": "cypress-plugin-api",
5-
"version": "2.10.2",
5+
"version": "2.10.3",
66
"keywords": [
77
"cypress",
88
"api",

server/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ app.get('/xml', (req, res) => {
8484
res.send(answerXML)
8585
})
8686

87+
app.get('/json-weird', (req, res) => {
88+
res.set('Content-Type', 'application/abcd+json');
89+
res.send(answerJSON)
90+
})
91+
8792
app.get('/undefined', (req, res) => {
8893
const answerXML = "<xml>XML</xml>"
8994
res.send(answerXML)

src/utils/isValidJson.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
export const isValidJson = (str: string) => {
2-
try {
3-
JSON.parse(str);
1+
export const isValidJson = (input: any): boolean => {
2+
// all objects are JSONs
3+
if (typeof input === 'object') {
44
return true;
5-
} catch (e) {
5+
} else if (typeof input === 'string') {
6+
// strings are tested for JSON format
7+
try {
8+
JSON.parse(input);
9+
return true;
10+
} catch (e) {
11+
return false;
12+
}
13+
} else {
614
return false;
715
}
816
}

0 commit comments

Comments
 (0)