-
-
Notifications
You must be signed in to change notification settings - Fork 752
improve: jsonresponse validation #3316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b7e95fb
WIP
kobenguyent be1cb05
fix: handle the returned response is array
kobenguyent 3dc7f1a
merge with 3.x branch
kobenguyent 138a083
fix: code reviews
kobenguyent 282f7f1
Merge branch '3.x' into no-ticket-improve-jsonresponse-validation
kobenguyent e08ec68
fix: code reviews
kobenguyent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,12 @@ Checks for deep inclusion of a provided json in a response data. | |
| I.dontSeeResponseContainsJson({ user: 2 }); | ||
| ``` | ||
|
|
||
| ```js | ||
| // response.data == [{ data: { user: 1 } }] | ||
|
|
||
| I.dontSeeResponseContainsJson({ user: 2 }); | ||
| ``` | ||
|
|
||
| #### Parameters | ||
|
|
||
| - `json` **[object][2]** | ||
|
|
@@ -139,6 +145,12 @@ Checks for deep inclusion of a provided json in a response data. | |
| I.seeResponseContainsJson({ user: { email: '[email protected]' } }); | ||
| ``` | ||
|
|
||
| ```js | ||
| // response.data == [{ user: { name: 'jon', email: '[email protected]' } }] | ||
|
|
||
| I.seeResponseContainsJson({ user: { email: '[email protected]' } }); | ||
| ``` | ||
|
|
||
| #### Parameters | ||
|
|
||
| - `json` **[object][2]** | ||
|
|
@@ -153,6 +165,12 @@ Checks for deep inclusion of a provided json in a response data. | |
| I.seeResponseContainsKeys(['user']); | ||
| ``` | ||
|
|
||
| ```js | ||
| // response.data == [{ user: { name: 'jon', email: '[email protected]' } }] | ||
|
|
||
| I.seeResponseContainsKeys(['user']); | ||
| ``` | ||
|
|
||
| #### Parameters | ||
|
|
||
| - `keys` **[array][3]** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,12 +173,21 @@ class JSONResponse extends Helper { | |
| * | ||
| * I.seeResponseContainsJson({ user: { email: '[email protected]' } }); | ||
| * ``` | ||
| * ```js | ||
| * // response.data == [{ user: { name: 'jon', email: '[email protected]' } }] | ||
| * | ||
| * I.seeResponseContainsJson({ user: { email: '[email protected]' } }); | ||
| * ``` | ||
| * | ||
| * @param {object} json | ||
| */ | ||
| seeResponseContainsJson(json = {}) { | ||
| this._checkResponseReady(); | ||
| expect(this.response.data).to.deep.match(json); | ||
| if (this.response.data.length > 0) { | ||
| this.response.data.forEach(data => expect(data).to.deep.match(json)); | ||
| } else { | ||
| expect(this.response.data).to.deep.match(json); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -189,12 +198,21 @@ class JSONResponse extends Helper { | |
| * | ||
| * I.dontSeeResponseContainsJson({ user: 2 }); | ||
| * ``` | ||
| * ```js | ||
| * // response.data == [{ data: { user: 1 } }] | ||
| * | ||
| * I.dontSeeResponseContainsJson({ user: 2 }); | ||
| * ``` | ||
| * | ||
| * @param {object} json | ||
| */ | ||
| dontSeeResponseContainsJson(json = {}) { | ||
| this._checkResponseReady(); | ||
| expect(this.response.data).not.to.deep.match(json); | ||
| if (this.response.data.length > 0) { | ||
| this.response.data.forEach(data => expect(data).not.to.deep.match(json)); | ||
| } else { | ||
| expect(this.response.data).not.to.deep.match(json); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -206,11 +224,21 @@ class JSONResponse extends Helper { | |
| * I.seeResponseContainsKeys(['user']); | ||
| * ``` | ||
| * | ||
| * ```js | ||
| * // response.data == [{ user: { name: 'jon', email: '[email protected]' } }] | ||
| * | ||
| * I.seeResponseContainsKeys(['user']); | ||
| * ``` | ||
| * | ||
| * @param {array} keys | ||
| */ | ||
| seeResponseContainsKeys(keys = []) { | ||
| this._checkResponseReady(); | ||
| expect(this.response.data).to.include.keys(keys); | ||
| if (this.response.data.length > 0) { | ||
kobenguyent marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.response.data.forEach(data => expect(data).to.include.keys(keys)); | ||
| } else { | ||
| expect(this.response.data).to.include.keys(keys); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,7 +83,23 @@ describe('JSONResponse', () => { | |
| { id: 1, author: 'davert' }, | ||
| ], | ||
| }); | ||
| expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('AssertionError'); | ||
| expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('expected { …(2) } to deeply match { Object (posts) }'); | ||
| }); | ||
|
|
||
| it('should check for json inclusion - returned Array', () => { | ||
| const arrayData = [{ ...data }]; | ||
| restHelper.config.onResponse({ data: arrayData }); | ||
| I.seeResponseContainsJson({ | ||
| posts: [ | ||
| { id: 2 }, | ||
| ], | ||
| }); | ||
| I.seeResponseContainsJson({ | ||
| posts: [ | ||
| { id: 1, author: 'davert' }, | ||
| ], | ||
| }); | ||
| expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('expected { …(2) } to deeply match { Object (posts) }'); | ||
| }); | ||
|
|
||
| it('should simply check for json inclusion', () => { | ||
|
|
@@ -93,16 +109,33 @@ describe('JSONResponse', () => { | |
| I.dontSeeResponseContainsJson({ name: 'joe' }); | ||
| }); | ||
|
|
||
| it('should simply check for json inclusion - returned Array', () => { | ||
| restHelper.config.onResponse({ data: [{ user: { name: 'jon', email: '[email protected]' } }] }); | ||
| I.seeResponseContainsJson({ user: { name: 'jon' } }); | ||
| I.dontSeeResponseContainsJson({ user: { name: 'jo' } }); | ||
| I.dontSeeResponseContainsJson({ name: 'joe' }); | ||
| }); | ||
|
|
||
| it('should simply check for json equality', () => { | ||
| restHelper.config.onResponse({ data: { user: 1 } }); | ||
| I.seeResponseEquals({ user: 1 }); | ||
| }); | ||
|
|
||
| it('should simply check for json equality - returned Array', () => { | ||
| restHelper.config.onResponse({ data: [{ user: 1 }] }); | ||
| I.seeResponseEquals({ user: 1 }); | ||
| }); | ||
|
|
||
| it('should check json contains keys', () => { | ||
| restHelper.config.onResponse({ data: { user: 1, post: 2 } }); | ||
| I.seeResponseContainsKeys(['user', 'post']); | ||
| }); | ||
|
|
||
| it('should check json contains keys - returned Array', () => { | ||
| restHelper.config.onResponse({ data: [{ user: 1, post: 2 }] }); | ||
| I.seeResponseContainsKeys(['user', 'post']); | ||
| }); | ||
|
|
||
| it('should check for json by callback', () => { | ||
| restHelper.config.onResponse({ data }); | ||
| const fn = ({ expect, data }) => { | ||
|
|
@@ -124,7 +157,7 @@ describe('JSONResponse', () => { | |
| name: joi.string(), | ||
| }), | ||
| }); | ||
| const fn = (joi) => { | ||
| const fn = () => { | ||
| return schema; | ||
| }; | ||
| I.seeResponseMatchesJsonSchema(fn); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.