Skip to content

Commit

Permalink
Merge pull request #112 from filiphric/109-plugin-fails-with-areplace…
Browse files Browse the repository at this point in the history
…-is-not-a-function
  • Loading branch information
filiphric authored Feb 3, 2023
2 parents 8da620c + 47e21cf commit 3dc31ca
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
10 changes: 10 additions & 0 deletions cypress/e2e/formats.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>XML</xml>"
res.send(answerXML)
Expand Down
16 changes: 12 additions & 4 deletions src/utils/isValidJson.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 3dc31ca

Please sign in to comment.