Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

momons/QStubServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Q Stub Server

GO言語で作成しております。

いろいろな開発においてAPIへのアクセスやテスト等で自分自身がほしいな〜と思ったスタブサーバを作成しました。

機能

  • 特定URLからローカルファイルを転送します。
  • 特定URLから情報を別サーバへ転送します。
  • リクエスト、レスポンスの内容をファイル出力します。
  • URLだけでなくヘッダ、ボディの内容でフィルタリングも可能です。

特定のURLからローカルファイルを転送する。

Setting.jsonの内容を編集します。

アクセスを全て/user/hoge/webroot/配下のローカルファイルのアクセスに変換する。
正規表現で対応します。

{
  "convertList": [
    {
      "srcURL" : "/(.*)",
      "destPath" : "/user/hoge/webroot/$1"
    }
  ]
}

/へのアクセスにindex.htmlを返却する。その他は上記と同じに。
上から順に処理されていくため、優先するものは上位に設定します。�

{
  "convertList": [
    {
      "srcURL" : "/",
      "destPath" : "/user/hoge/webroot/index.html"
    },
    {
      "srcURL" : "/(.*)",
      "destPath" : "/user/hoge/webroot/$1"
    }
  ]
}

ファイルに別途ContentTypeを指定する。
index.htmlの場合Content-Type: application/octet-streamが指定される。
省略時はContentTypeList.jsonから拡張子で自動で設定されます。

{
  "convertList": [
    {
      "srcURL" : "/",
      "destPath" : "/user/hoge/webroot/index.html",
      "contentType" : "application/octet-stream"
    },
    {
      "srcURL" : "/(.*)",
      "destPath" : "/user/hoge/webroot/$1"
    }
  ]
}

特定のURLを別サーバへ転送する。

アクセスを全てhttp://192.168.1.10:9999/に転送する。

{
  "convertList": [
    {
      "srcURL" : "/(.*)",
      "destURL" : "http://192.168.1.10:9999/$1"
    }
  ]
}

/hoge/の場合ローカルファイルにアクセスする。その他は上記と同じに。

{
  "convertList": [
    {
      "srcURL" : "/hoge/(.*)",
      "destPath" : "/user/hoge/webroot/$1"
    },
    {
      "srcURL" : "/(.*)",
      "destURL" : "http://192.168.1.10:9999/$1"
    }
  ]
}

ヘッダ情報でフィルタリングする。

文字コードがutf-8ならばローカルファイルにアクセスし、それ以外は別サイトへ転送する(フィルタリングの正規表現は適当です)。

{
  "convertList": [
    {
      "srcURL" : "/(.*)",
      "destPath" : "/user/hoge/webroot/$1",
      "filterList" : [
        {
          "type" : "header",
          "condition" : "Content-Type: ([\\S]+); charset=utf-8"
        }
      ]
    },
    {
      "srcURL" : "/(.*)",
      "destURL" : "http://192.168.1.10:9999/$1"
    }
  ]
}

文字コードがutf-8でボディ情報がOKならばローカルファイルにアクセスし、それ以外は別サイトへ転送する(フィルタリングの正規表現は適当です)。

{
  "convertList": [
    {
      "srcURL" : "/(.*)",
      "destPath" : "/user/hoge/webroot/$1",
      "filterList" : [
        {
          "type" : "header",
          "condition" : "Content-Type: ([\\S]+); charset=utf-8"
        },
        {
          "type" : "body",
          "condition" : "OK"
        }
      ]
    },
    {
      "srcURL" : "/(.*)",
      "destURL" : "http://192.168.1.10:9999/$1"
    }
  ]
}

現在typeに指定できるのはheaderbodyのみです。

コマンドについて

ポートを9999に設定する。(デフォルトは8080)

> QStubServer -port 9999

Setting.jsonのパスを指定する。(デフォルトは[カレントディレクトリ]+Setting.json)

> QStubServer -setting /usr/hoge/QStubServer/Setting.json

ContentTypeList.jsonのパスを指定する。(デフォルトは[カレントディレクトリ]+ContentTypeList.json)

> QStubServer -contenttype /usr/hoge/QStubServer/ContentTypeList.json

リクエスト、レスポンス内容をファイルに出力する。
/usr/hoge/QStubServer/log配下へ内容を出力します。

> QStubServer -outputlog /usr/hoge/QStubServer/log

ビルド環境について

このライブラリのビルドは
https://github.com/constabulary/gb
で行っております。

About

API stub server

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages