Moco 詳解

1、下載及安裝:

運行環境:java

  • JAVA環境
  • linux

下載地址:https://github.com/dreamhead/mocolinux

下載下來的是一個jar包,如:moco-runner-0.12.0-standalone.jargit

把這個jar包上傳到linux服務器上,而後在與moco-runner-0.12.0-standalone.jar同級的目錄下建立 foo.json 文件:github

[ { "response" : { "text" : "Hello, Moco" } } ]

Moco的運行很是簡單,只須要一行命令便可: java -jar <path-to-moco-runner> http -p <monitor-port> -c < configuration -file>正則表達式

  • <path-to-moco-runner>:moco-runner-0.11.0-standalone.jar包的路徑
  • <monitor-port>:http服務監聽的端口
  • <configuration -file>:配置文件路徑
java -jar moco-runner-0.12.0-standalone.jar http -p 9999 -c foo.json

經過瀏覽器訪問服務器的9999端口:shell

返回正確就說明Moco服務搭建成功了。json

雖說服務運行沒問題了,可是總以爲啓動和中止很麻煩,如今用shell來寫一個啓動和中止的腳本api

啓動start.sh:瀏覽器

#!/bin/bash dirWiremock=`pwd` getCount=`ps -ef | grep "moco-runner" | grep -v "grep" |wc -l` wiremock_jar=${dirWiremock}/moco-runner-0.12.0-standalone.jar port=9999 exe_name=moco-runner if [ $getCount -ne 0 ]; then
        echo $exe_name is already running with $ssu_pid_count processes echo $exe_name started failed exit 1; else nohup java -jar ${wiremock_jar} http -p ${port} -c foo.json &
        echo "Start success!......"
fi

中止stop.sh:bash

#!/bin/sh exe_name=moco-runner # 進入可執行目錄 base_path=$(cd `dirname $0`; pwd) cd $base_path # 中止進程 exe_pid_path=`pwd` exe_pid_list=`ps -ef | grep $exe_name|grep -v 'grep'|awk '{print $2}'` our_pid_list=""
for exe_pid in $exe_pid_list do pid_path=`pwdx $exe_pid | awk '{print $2}'` if [ "$pid_path"x == "$exe_pid_path"x ]; then our_pid_list=$our_pid_list" "$exe_pid fi
done

if [ "$our_pid_list"x != "x" ]; then
        kill -9 $our_pid_list echo $exe_name process"$our_pid_list" killed else
        echo $exe_name is not running fi

運行shell腳本,啓動和中止就方便多了。

2、Moco配置

啓動服務以後,必然會根據需求stub出各類各樣接口反饋,咱們會把這個配置放在一個json文件中,啓動Moco的時候,須要指定使用的配置文件路徑,這樣配置就能夠生效了。Moco服務能夠檢測到配置文件的變動,假如你修改了配置文件,不須要從新啓動Moco,服務照樣能夠生效。更詳細的配置介紹請查看:https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md

配置文件的工做原理大體以下: 

如何在配置文件添加註釋

json不支持註釋,想要添加註釋的話,能夠在description字段中加入描述 
image_1aukces4c1bld1ilv120vtc713rk1g.png-21.5kB

約定請求Body

image_1aukcg2219uc4a89v2eia169i1t.png-63.5kB

約定接口的uri

image_1aukcgr1h1pvg1iqede15mtjtv2a.png-35.2kB

約定請求參數

image_1aukcheemo95pmp1q351n0vjq12n.png-40.6kB

約定請求方法

image_1aukci164cq717av19nc1cp0b0q34.png-32.7kB

約定HTTP版本

image_1aukcin2p1cnoenf1nfe1hn895u3h.png-38.2kB

約定請求頭部

image_1aukp23qh1io01g611k67l49ie9.png-47.6kB

約定cookie

image_1aukp2ppph2215lv4mp165n1begm.png-45.5kB

約定請求form

image_1aukp3eip1unv1s971l1s8ke1pr713.png-47.1kB

表單能夠添加多項,多項的時候,必須所有匹配,接口才算匹配成功

約定以指定xml做爲請求body

image_1aukp4l8rblj9p712cq13au1nlc1g.png-69.9kB

用xpath對請求進行匹配

image_1aukp54ud10dcbhi1a981sii1j1t.png-47.8kB

約定以指定json做爲請求body

image_1aukp62mg1c1p1ilpa371fg71okd2a.png-118.5kB

用正則表達式對請求進行匹配

image_1aukp6kjk1cgktok1ksl3pl11ra2n.png-50.1kB

匹配操做

image_1aukp79rk6stah19u7138o10df34.png-132kB

設置Response content

image_1aukpa21f1nt21hrq15iv5111lcc3h.png-71kB

設置Response 狀態碼

image_1aukpam111jae1kj31trj1vqau2d3u.png-50.7kB

設置Response HTTP版本

image_1aukpb88b17f119i1oatsdi1pti4b.png-58.3kB

設置Response 頭部

image_1aukpboqh15l6ib31gvrcjo1dbq4o.png-45.9kB

設置重定向

image_1aukpca591qrht381uv31cvt43g55.png-39.9kB

設置cookie

image_1aukpcqun1tq911n314fv1o8r12hi5i.png-42.1kB

掛載文件

image_1aukpdacf1sq3p241s14qbqdnb5v.png-34.4kB

相關文章
相關標籤/搜索