LoginSignup
0
0

More than 3 years have passed since last update.

HTTPステータスコードのテスト (httpstatus on Azure App Service)

Posted at

VS で https://github.com/Readify/httpstatus を clone して Azure App Service で動作させ、HTTPステータスコードのテストに使用するだけの簡単な作業を行いました。
わざわざ記事にする内容ではないのですが、VSで非常に簡単にでき、便利さを実感したのであえて備忘録を作りました。
ちなみに、わざわざ Azure にデプロイせずとも
httpstat.us
から同じサービスにアクセスできるので、本記事の手順は Azure への Publish を VS からやってみた以上の意味はありません。

環境

  • Microsoft Windows 10 Pro (Azure VM で稼働)
  • Microsoft Visual Studio Community 2019 (Version 16.2.3)

VSでの作業

ソリューションを開くまで

Clone or check out code

image.png

image.png

Teapot.sln

Teapot.sln をダブルクリックして開いたら、Teapot.Web プロジェクトのコンテクストメニューから Publish を選択します。

image.png

(Teapot なんて名前がついている理由はわかりません。418 I'm a teapotが由来でしょうか?)

Azure App Service へのデプロイ

Publish

Azure App Service の target に対して、 Create New を選択して、 Publish します。

image.png

Name, Subscription, Resource group, Hosting Plan などを必要に応じて変更し、 Create ボタンですぐに開始できます。

表示

ブラウザで https://teapotweb____.azurewebsites.net/ (実際の Azure App Service の URL を指定します)にアクセスすると、下記のようなページが表示されます。

image.png

URL のパスを 200 , 403 , 500 などに変更して各種ステータスコードの応答を得ることができます。

curl でテスト

curl --head -XGET https://teapotweb____.azurewebsites.net/404
HTTP/1.1 404 Not Found
Cache-Control: private
Content-Length: 13
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.1
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
...

Node.js + Axios でテスト

あらかじめ npm で axios を install しておきます。
次のような簡易スクリプトでGETリクエストをテストします。

index.js
// Usage: AZEP=https://https://teapotweb____.azurewebsites.net node index.js CODE SLEEP_MSEC
// E.g. AZEP=https://https://teapotweb____.azurewebsites.net node index.js 200 1000
// Axios timeout can be tested by setting SLEEP_MSEC greater than 10000.
if (require.main === module) {
  const code = process.argv[2];
  const msec = process.argv[3];
  const client = require("axios").create({
    baseURL: (process.env["AZEP"] || "http://localhost:8080"),
    timeout: 10000,
    headers: {"Content-Type": "application/json"}
  });
  const params = {
    params: (!!msec ? { sleep: msec } : {})
  };
  client.get(code, params).then((resp) => {
    console.log({
      status: resp.status,
      statusText: resp.statusText,
      contentLength: resp.headers["content-length"],
      data: resp.data
    });
  }).catch((e) => {
    if (!!e.response) {
      console.error("ERROR", {
        status: e.response.status,
        statusText: e.response.statusText,
        contentLength: e.response.headers["content-length"],
        data: e.response.data
      });
    } else {
      console.error("AXIOS ERROR", e.message);
    }
  });
}

実行例

AZEP=https://teapotweb____.azurewebsites.net/ node index.js 200 1000
{
  status: 200,
  statusText: 'OK',
  contentLength: '34',
  data: { code: 200, description: 'OK' }
}

ステータスコード300番以上の指定でエラーの扱いとなり axios の catch で処理されます。

AZEP=https://teapotweb____.azurewebsites.net/ node index.js 404
ERROR {
  status: 404,
  statusText: 'Not Found',
  contentLength: '41',
  data: { code: 404, description: 'Not Found' }
}

References

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0