Try using Aeromock 
Marverick.,inc. 
Hiroshi Fukuda / @chaltos 
ScalaMatsuri 2014
Lightweight mock web application server 
https://github.com/CyberAgent/aeromock 
Respond HTML, JSON, and static content 
Control URL Rewriting via routing script 
Scala-made 
2
Why I need mock 
In fronted development, I can't implement/test 
most of view without API 
3
Why I need mock 
Now, I need below for frontend development 
4 
… 
For develop/test view: 
For run API server: 
…
Why I need mock 
But, Scala's compilation is slow … 
5
Why I need mock 
So, I want develop view with 
6 
… 
…
Installation 
Install aeromock-brew 
aerocmock-brew is package manager for Aeromock 
$ git clone git@github.com:CyberAgent/aeromock-brew.git 
Install Aeromock by aeromock-brew 
to ~/.aeromock/applications/VERSION 
$ cd aeromock-brew 
$ ./gradlew use -PaeromockVersion=latest 
$ AEROMOCK_HOME=~/.aeromock/applications/current 
$ export PATH=$PATH:$AEROMOCK_HOME 
7
Settings 
~/.aeromock/config.yaml 
project_config_path: /PATH/TO/project.yaml 
8
Settings 
project.yaml 
template: # setting of HTML template engine 
root: ./template # root dir of template files 
# specificy template engine 
serviceClass: jp.co.cyberagent.aeromock.template. … 
data: # setting of data for templates 
root: ./data 
ajax: # setting of JSON API 
root: ./ajax 
… 
9
JSON API 
./ajax/api/hoge.yaml localhost:3183/api/hoge 
10 
{ 
"title": "hoge.yaml", 
"hash": { 
"hashArray": [ 
{ 
"id": 1, 
"name": "user_1" 
}, 
{ 
"id": 1, 
"name": "user_2" 
} 
] 
}, 
"intArray": [1, 2] 
} 
! 
title: hoge.yaml 
hash: 
hashArray: 
- 
id: 1 
name: user_1 
- 
id: 2 
name: user_2 
intArray: 
- 1 
- 2
JSON API 
Custom Response 
Aeromock can respond with specific status code and headers 
11 
__response: 
code: 400 
headers: 
X-Aeromock-Header1: aeromock-header1 
X-Aeromock-Header2: aeromock-header2
JSON API 
Various patterns per API 
If you request to /api/login with "_dataid=xxx" parameter, 
Aeromock return JSON generated from 
PATH/TO/PROJECT/api/login__xxx.yaml 
(xxx is must be unique) 
12
Custom Routing 
If actual URL and template path doesn't 
match 
replace "_dataid" with different parameter 
switch template file by request domain 
or useragent 
13
Custom Routing 
14 
./routing.groovy 
routing { 
server "localhost", { 
rewrite(/^api/login$/) { _ -> 
if(FORM_DATA.get("account")) { 
"${REQUEST_URI}?_dataid=success" 
else { 
"${REQUEST_URI}?_dataid=failure" 
} 
} 
} 
}
Impression 
Some API(like /api/user/list) requires big YAML. 
creating these YAML is so troublesome … 
15 
items: 
- 
id: 1 
name: user_1 
attr: … 
- 
id: 2 
name: user_2 
…
Impression 
If API has many patterns, routing.groovy would fat ? 
I'll have no need to wait for compilation. So good 
16

Try using Aeromock by Marverick, Inc.

  • 1.
    Try using Aeromock Marverick.,inc. Hiroshi Fukuda / @chaltos ScalaMatsuri 2014
  • 2.
    Lightweight mock webapplication server https://github.com/CyberAgent/aeromock Respond HTML, JSON, and static content Control URL Rewriting via routing script Scala-made 2
  • 3.
    Why I needmock In fronted development, I can't implement/test most of view without API 3
  • 4.
    Why I needmock Now, I need below for frontend development 4 … For develop/test view: For run API server: …
  • 5.
    Why I needmock But, Scala's compilation is slow … 5
  • 6.
    Why I needmock So, I want develop view with 6 … …
  • 7.
    Installation Install aeromock-brew aerocmock-brew is package manager for Aeromock $ git clone git@github.com:CyberAgent/aeromock-brew.git Install Aeromock by aeromock-brew to ~/.aeromock/applications/VERSION $ cd aeromock-brew $ ./gradlew use -PaeromockVersion=latest $ AEROMOCK_HOME=~/.aeromock/applications/current $ export PATH=$PATH:$AEROMOCK_HOME 7
  • 8.
  • 9.
    Settings project.yaml template:# setting of HTML template engine root: ./template # root dir of template files # specificy template engine serviceClass: jp.co.cyberagent.aeromock.template. … data: # setting of data for templates root: ./data ajax: # setting of JSON API root: ./ajax … 9
  • 10.
    JSON API ./ajax/api/hoge.yamllocalhost:3183/api/hoge 10 { "title": "hoge.yaml", "hash": { "hashArray": [ { "id": 1, "name": "user_1" }, { "id": 1, "name": "user_2" } ] }, "intArray": [1, 2] } ! title: hoge.yaml hash: hashArray: - id: 1 name: user_1 - id: 2 name: user_2 intArray: - 1 - 2
  • 11.
    JSON API CustomResponse Aeromock can respond with specific status code and headers 11 __response: code: 400 headers: X-Aeromock-Header1: aeromock-header1 X-Aeromock-Header2: aeromock-header2
  • 12.
    JSON API Variouspatterns per API If you request to /api/login with "_dataid=xxx" parameter, Aeromock return JSON generated from PATH/TO/PROJECT/api/login__xxx.yaml (xxx is must be unique) 12
  • 13.
    Custom Routing Ifactual URL and template path doesn't match replace "_dataid" with different parameter switch template file by request domain or useragent 13
  • 14.
    Custom Routing 14 ./routing.groovy routing { server "localhost", { rewrite(/^api/login$/) { _ -> if(FORM_DATA.get("account")) { "${REQUEST_URI}?_dataid=success" else { "${REQUEST_URI}?_dataid=failure" } } } }
  • 15.
    Impression Some API(like/api/user/list) requires big YAML. creating these YAML is so troublesome … 15 items: - id: 1 name: user_1 attr: … - id: 2 name: user_2 …
  • 16.
    Impression If APIhas many patterns, routing.groovy would fat ? I'll have no need to wait for compilation. So good 16