-
Notifications
You must be signed in to change notification settings - Fork 89
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
Feature/post with dynamic body #84
Open
HerrPfister
wants to merge
7
commits into
namshi:master
Choose a base branch
from
HerrPfister:feature/POST-with-dynamic-body
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+190
−103
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f68777
update README and add tests for body in json file
joshmorel 8ac889c
Ability to match request json to file contents
joshmorel 85b679f
support node < 6
joshmorel d7acc24
Merge branch 'json-body-separate-file' of github.com:joshmorel/mockse…
8a8487f
Adding vscode config to gitignore
3a224a4
Upading previous PR to allow for POST with dynamic body. Using format…
1f60f92
cleaning up tests
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 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ node_modules | |
test.js | ||
.nyc_output | ||
*.code-workspace | ||
.vscode/ |
This file contains 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 |
---|---|---|
|
@@ -205,6 +205,8 @@ In the same way, if your POST body is a json like `{"json": "yesPlease"}`, | |
mockserver will look for a file called `POST--{"json": "yesPlease"}.mock`. | ||
_Warning! This feature is_ **NOT compatible with Windows**_. This is because Windows doesn't accept curly brackets as filenames._ | ||
|
||
To overcome this limitation or to keep the file names tidy, you can put the body in [a json file](#body-in-json-file). | ||
|
||
If no parametrized mock file is found, mockserver will default to the | ||
nearest headers based .mock file | ||
|
||
|
@@ -217,6 +219,24 @@ Authorization: 12345 | |
|
||
if there's no `hello/GET_Authorization=12345--a=b.mock`, we'll default to `hello/GET_Authorization=12345.mock` or to `hello/GET.mock` | ||
|
||
## Body in json file | ||
|
||
To support Windows and tidier file naming, the expected body of the request can be saved in a separate `.json` file. If the request contains a body in json format, mockserver will look for that body in json files in the same `$REQUEST-PATH` directory. | ||
|
||
For example, if a POST body is `{"json": "yesPlease"}`, and a file in the path called `payload.json` has the same content (order is important, but spacing between keys/values is not), mockserver will look for a file called `[email protected]`. | ||
|
||
The general naming convention is: | ||
|
||
``` | ||
$REQUEST-PATH/$HTTP-METHOD@$JSON-FILENAME.mock | ||
``` | ||
|
||
The precedence for matching requests containing a json body is: | ||
|
||
1) Contained within mock file name | ||
2) Contained within .json file | ||
3) No match - nearest headers based .mock file | ||
|
||
## Wildcard slugs | ||
|
||
If you want to match against a route with a wildcard - say in the case of an ID or other parameter in the URL, you can | ||
|
This file contains 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
File renamed without changes.
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
HTTP/1.1 200 OK | ||
Content-Type: text | ||
|
||
Passed! |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
HTTP/1.1 404 OK | ||
Content-Type: application/json; charset=utf-8 | ||
|
||
{ | ||
"error": "User not found" | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
HTTP/1.1 200 OK | ||
Content-Type: application/json; charset=utf-8 | ||
|
||
{ | ||
"token": "longJWT" | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"user": { | ||
"username": "notTheUser", | ||
"password": "123456" | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"user": { | ||
"username": "theUser", | ||
"password": "123456" | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could change this check to
if (JSON.stringify(JSON.parse(jsonBody)) === JSON.stringify(JSON.parse(data))) {
to avoid afalsy
result due to an extra white space or some other formatting detail.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with that because without it, it couldn't work correctly (if some space character exist for example)