paulwong

          「測試」 - 接口測試 & mock工具Moco

          當需要調用第三方HTTP接口時,別人的接口還沒完成,可先根據接口定義文檔,返回適當的數據,以便開發。

          在LINUX上的部署結構為:
          ├── boot
          │   ├── moco-runner-1.1.0-standalone.jar
          │   └── .version
          ├── foo.json
          ├── logs
          │   ├── back
          │   └── moco.log
          ├── moco.sh
          └── startup-moco.sh

          .version文件:
          /path/to/boot/moco-runner-1.1.0-standalone.jar 1.1.0

          moco.sh
          #!/usr/bin/env bash

          # Ensure this file is executable via `chmod a+x moco`, then place it
          # somewhere on your $PATH, like ~/bin. The rest of moco will be
          # installed upon first run into the ~/.moco directory.

          if [ `id -u` -eq 0 ] && [ "$MOCO_ROOT" = "" ]; then
              echo "WARNING: You're currently running as root; probably by accident."
              echo "Press control-C to abort or Enter to continue as root."
              echo "Set MOCO_ROOT to disable this warning."
              read _
          fi

          echo $*

          #export MOCO_HOME="$
          {MOCO_HOME:-"$HOME/.moco"}"
          export MOCO_HOME=$(cd `dirname $0`; cd boot; pwd)
          VERSION_LOG_FILE="$MOCO_HOME/.version"

          # Determine the Java command to use to start the JVM.
          if [ -n "$JAVA_HOME" ] ; then
              if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
                  # IBM's JDK on AIX uses strange locations for the executables
                  JAVACMD="$JAVA_HOME/jre/sh/java"
              else
                  JAVACMD="$JAVA_HOME/bin/java"
              fi
              if [ ! -x "$JAVACMD" ] ; then
                  die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME

          Please set the JAVA_HOME variable in your environment to match the
          location of your Java installation."
              fi
          else
              JAVACMD="java"
              which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

          Please set the JAVA_HOME variable in your environment to match the
          location of your Java installation."
          fi

          if [ "$HTTP_CLIENT" = "" ]; then
              if type -p curl >/dev/null 2>&1; then
                  if [ "$https_proxy" != "" ]; then
                      CURL_PROXY="-x $https_proxy"
                  fi
                  HTTP_CLIENT="curl $CURL_PROXY -f -L -o"
              else
                  HTTP_CLIENT="wget -O"
              fi
          fi

          function download_failed_message 
          {
              echo "Failed to download $1"
              echo "It's possible your HTTP client's certificate store does not have the"
              echo "correct certificate authority needed. This is often caused by an"
              echo "out-of-date version of libssl. Either upgrade it or set HTTP_CLIENT"
              echo "to turn off certificate checks
          :"
              echo "  export HTTP_CLIENT=\"wget --no-check-certificate -O\" # or"
              echo "  export HTTP_CLIENT=\"curl --insecure -f -L -o\""
              echo "It's also possible that you're behind a firewall haven't yet"
              echo "set HTTP_PROXY and HTTPS_PROXY."
          }

          function download 
          {
              $HTTP_CLIENT "$2.pending" "$1"
              if [ $? == 0 ]; then
                  # TODO
          : checksum
                  mv -f "$2.pending" "$2"
              else
                  rm "$2.pending" 2> /dev/null
                  download_failed_message "$1"
                  exit 1
              fi
          }

          function parse_tag 
          {
             tag_value=`grep "<$2>.*<.$2>" $1 | sed -e "s/^.*<$2/<$2/" | cut -f2 -d">"| cut -f1 -d"<"`
          }

          function parse_maven_metadata 
          {
              MOCO_METADATA_URL="http
          ://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/maven-metadata.xml"
              MOCO_METADATA="/tmp/maven-metadata.xml"
              download $MOCO_METADATA_URL $MOCO_METADATA
              parse_tag $MOCO_METADATA latest
              LATEST_VERSION=$tag_value
          }

          function parse_standalone_latest_url 
          {
              parse_maven_metadata
              VERSION=${LATEST_VERSION%
          }
              LATEST_MOCO_STANDALONE_JAR="moco-runner-$VERSION-standalone.jar"
              MOCO_STANDLONE_URL="http://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/$LATEST_VERSION/$LATEST_MOCO_STANDALONE_JAR"
          }

          function install 
          {
              echo "Install moco"
              echo "Parse the latest version of moco"
              parse_standalone_latest_url
              echo "Download the latest moco
          : $LATEST_VERSION"
              MOCO_STANDALONE="$MOCO_HOME/$LATEST_MOCO_STANDALONE_JAR"
              echo "$MOCO_STANDALONE $LATEST_VERSION" >> $VERSION_LOG_FILE
              download $MOCO_STANDLONE_URL $MOCO_STANDALONE
          }

          function load_current_version 
          {
              read MOCO_STANDALONE CURRENT_VERSION < $VERSION_LOG_FILE
              if [[ "$(uname)" -ne "Darwin" && "$(expr substr $(uname -s) 2 6)" == "CYGWIN"   ]];then
                  MOCO_STANDALONE=`cygpath -m "$MOCO_STANDALONE"`
              fi
          }

          function usage 
          {
            printf "
          options
          :
                 help      show help
                 start     start server, e.g. moco start -p 12306 -c configfile.json
                 shutdown  shutdown moco server 
                 upgrade   upgrade moco
          "
          }

          if [ ! -e "$MOCO_HOME" ]
          then
              mkdir "$MOCO_HOME"
              install
          fi

          if [ "$1" = "start" ]; then
              echo "Starting"
              load_current_version
              exec "$JAVACMD" -jar "$MOCO_STANDALONE" $*
          elif [ "$1" = "http" ]; then
              echo "Starting HTTP server"
              load_current_version
              exec "$JAVACMD" -jar "$MOCO_STANDALONE" $*
          elif [ "$1" = "https" ]; then
              echo "Starting HTTPS server"
              load_current_version
              exec "$JAVACMD" -jar "$MOCO_STANDALONE" $*
          elif [ "$1" = "socket" ]; then
              echo "Starting Socket server"
              load_current_version
              exec "$JAVACMD" -jar "$MOCO_STANDALONE" $*
          elif [ "$1" = "shutdown" ]; then
              echo "Shutting down server"
              load_current_version
              exec "$JAVACMD" -jar "$MOCO_STANDALONE" $*
          elif [ "$1" = "upgrade" ]; then
              echo "Check the new version"
              parse_maven_metadata
              load_current_version

              if [ "$LATEST_VERSION" = "$CURRENT_VERSION" ]; then
                  echo "The current version of moco is the latest"
              else
                  echo "Upgrading"
                  rm $VERSION_LOG_FILE
                  install
              fi
          elif [ "$1" = "version" ]; then
              load_current_version
              echo "Moco version: $CURRENT_VERSION"
          elif [ "$1" = "help" ]; then
              usage
          else
              usage
          fi

          這是根據GIT上的原始文件作的修改。

          startup-moco.sh
          CMD_PATH=$(cd `dirname $0`; pwd)

          # 項目日志輸出絕對路徑
          LOG_DIR=$
          {CMD_PATH}"/logs"
          LOG_FILE="moco.log"
          LOG_PATH="$
          {LOG_DIR}/${LOG_FILE}"

          # 當前時間
          NOW=`date +'%Y-%m-%m-%H-%M-%S'`
          NOW_PRETTY=`date +'%Y-%m-%m %H:%M:%S'`

          # 啟動日志
          STARTUP_LOG="================================================ $
          {NOW_PRETTY} ================================================\n"

          # 日志備份目錄
          LOG_BACK_DIR="$
          {LOG_DIR}/back/"

          # 如果logs文件夾不存在,則創建文件夾
          if [[ ! -d "$
          {LOG_DIR}" ]]; then
            mkdir "$
          {LOG_DIR}"
          fi

          # 如果logs/back文件夾不存在,則創建文件夾
          if [[ ! -d "$
          {LOG_BACK_DIR}" ]]; then
            mkdir "$
          {LOG_BACK_DIR}"
          fi

          # 如果項目運行日志存在,則重命名備份
          if [[ -f "$
          {LOG_PATH}" ]]; then
              mv $
          {LOG_PATH} "${LOG_BACK_DIR}/${APPLICATION}_back_${NOW}.log"
          fi

          # 創建新的項目運行日志
          echo "" > $
          {LOG_PATH}

          # 可支持多個json配置文件
          $CMD_PATH/moco.sh http -p 8088 -g "${CMD_PATH}/root.json" > ${LOG_PATH} 2>&1 &

          # 打印啟動日志
          echo -e $
          {STARTUP_LOG}

          root.json
          [
              
          {
                  "context"
          : "/service-a",
                  "include": "foo.json"
              
          },
              
          {
                  "context"
          : "/service-b",
                  "include": "bar.json"
              
          }
          ]

          foo.json
          [
              
          {
                  "request"
          : {
                      "method": "post",
                      "forms": {
                          "method": "uploadKycInfo"
                      
          }
                  },
                  "response": 
          {
                      "json"
          : {
                          "response": {
                              "subcode": "10000",
                              "submsg": "Success",
                              "sndDt": "20210121101800",
                              "remark": "上傳驗證基本信息",
                              "msgBody": {
                                  "merOrdrNo": "A00120210121654321",
                                  "retCode": "00000",
                                  "retMsg": "成功/處理完成",
                                  "remark": "上傳詳情或上傳信息的簡要描述"
                              
          }
                          },
                          "code": "0000",
                          "msg": "處理完成",
                          "sign": "DF2659FE3EB8184561135D9F55F5EF5"
                      }
                  }
              }
          ]

          訪問路徑:
          http://ip:port/service-a/

          https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md
          https://zhuanlan.zhihu.com/p/60076337 
          https://www.programmersought.com/article/68272293688/

          posted on 2021-01-21 14:09 paulwong 閱讀(599) 評論(0)  編輯  收藏 所屬分類: RESTFUL-API


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 贺兰县| 卫辉市| 枝江市| 登封市| 灌南县| 武汉市| 台州市| 鄯善县| 浦城县| 柳江县| 花莲县| 景德镇市| 新巴尔虎右旗| 曲阜市| 彭水| 饶河县| 乌拉特后旗| 平利县| 镇江市| 启东市| 安平县| 营口市| 陇南市| 三都| 汉中市| 阳春市| 沈阳市| 贵港市| 德惠市| 信宜市| 镇赉县| 察隅县| 华安县| 荔波县| 桃园县| 博兴县| 咸丰县| 米泉市| 寿宁县| 乐陵市| 廉江市|