-
Notifications
You must be signed in to change notification settings - Fork 11
EN.03.03.JSON API
Document to respond mock json data as JSON API.
When configure ajax
section at project.yaml, be able to respond to mock json data.
Root directory of JSON API. Specify a relative path from project root path.
ajax:
root: ./ajax
When configure callback parameter name for JSONP at jsonp_callback_name
, request with callback parameter, then respond JSONP(application/javascript) response.
ajax:
root: ./ajax
jsonp_callback_name: callback
http://localhost:3183/test?callback=callbackFunc
callbackFunc({"param":"value"})
If specified path of project.yaml is project_root/project.yaml
, project_root/ajax/
is JSON API root directory.
Aeromock supports JSON and YAML format file as data file respond mock json data. May be either, we recommend YAML format, because easy to share some specifications.
Create project_root/ajax/api/test.yaml
as follows.
title: test.yaml
hash:
hashArray:
-
id: 1
name: name1
-
id: 2
name: name2
intArray:
- 1
- 2
To responde this file, send request to http://localhost:3183/api/test . Without the extension of data file. Then Aeromock respond JSON response with converting to JSON format.
{
"title": "test.yaml",
"hash": {
"hashArray": [
{
"id": 1,
"name": "name1"
},
{
"id": 2,
"name": "name2"
}
]
},
"intArray": [
1,
2
]
}
Aeromock supports array root element.
- element1
- element2
[
"element1",
"element2"
]
Aeromock supports various pattern data per API.
Create Different pattern of project_root/ajax/api/test.yaml
as project_root/ajax/api/test__xxx.yaml
format. xxx
is unique id to identify the data. test__2.yaml
in this example.
title: test__2.yaml
hash:
hashArray:
-
id: 1
name: name1
-
id: 2
name: name2
intArray:
- 1
- 2
To respond this data, send request to http://localhost:3183/api/test?_dataid=2
{
"title": "test__2.yaml",
"hash": {
"hashArray": [
{
"id": 1,
"name": "name1"
},
{
"id": 2,
"name": "name2"
}
]
},
"intArray": [
1,
2
]
}
Enable to handle HTTP Method by data file name. It's only name with __(httpmethodname)
data file name. If http method has not specified, default method is GET. For example, as the following.
Request URI | HTTP method | Query String | Data file name |
---|---|---|---|
/test | GET | - | test.yaml or test__get.yaml |
/test | POST | - | test__post.yaml |
/test | PUT | - | test__put.yaml |
/test | DELETE | - | test__delete.yaml |
/test | GET | _dataid=2 | test__2.yaml or test__get__2.yaml |
/test | POST | _dataid=2 | test__post__2.yaml |
/test | PUT | _dataid=2 | test__put__2.yaml |
/test | DELETE | _dataid=2 | test__delete__2.yaml |
If you have data you want to include in all or majority, be able to define as common data file.
Create ajax.groovy
at project root directory. ajax.groovy
is script control to choise common data file depending on the situation of HTTP request.
Create project_root/ajax/common/common.yaml
as a common data file.
commonProp: commonPropValue
commonHash:
prop1: prop1Value
Implement to return array of common data file of path (without extension). This path is relative path from JSON API.
return ["common/common"]
In this example, it is used project_root/ajax/common/common.yaml
always as common data file. Mock json data Merged common data as follows.
{
"commonProp": "commonPropValue",
"commonHash": {
"prop1": "prop1Value"
},
"hash": {
"hashArray": [
{
"id": 1,
"name": "name1"
},
{
"id": 2,
"name": "name2"
}
]
},
"intArray": [
1,
2
],
"title": "test.yaml"
}
ajax.groovy
should return array type, so be able to specify multiple common data files.
return ["common/common", "common/common2"]
When uses common data file, property name may be conflict. In this case, the properties of the data file that was last
The order applied data files is array of common data file -> Target data file.
If you want to add data on property of common data file but to overwrite, possible to specify __additional: true
.
For example, add property to commonHash
.
title: additional.yaml
commonHash:
__additional: true
prop2: prop2Value
Respond merged prop2
to `commonHash as follows.
{
"commonProp": "commonPropValue",
"commonHash": {
"prop1": "prop1Value",
"prop2": "prop2Value"
},
"title": "additional.yaml"
}
In ajax.groovy
, be able to refer to builtin variables defined on Aeromock. Request information like REQUEST_URI
and USER_AGENT
has been prepared, so there are technique to change applying common data dynamically.
Respond specific response code and customized response headers. Specify property named __response
on target data file.
Respond 400 status code, two headers. Define __response
on the data file, code
and headers
property in this.
title: custom_response.yaml
__response:
code: 400
headers:
X-Aeromock-Header1: aeromock-header1
X-Aeromock-Header2: aeromock-header2