Plack::Testが凄い! このエントリをはてなブックマークに>追加

Twitter経由でPlack::Testを見た。なにこれ。もうCGIいらないじゃん。

PlackやPSGIを知らない人は先に適当なページでも見て頂きたい。

で、Plack::Testの何がどうすごいかというと、次のように簡単にPSGIアプリケーションのテストができる。テストコードは宮川さんのものに、クエリ部分を付け加えたものだ。

use Plack::Test;
use Test::More;
use HTTP::Request;
use strict;
use warnings;

my $app = sub {
   my $env = shift;
   my $query = +{
       map { split(/=/) } split(/&/, $env->{QUERY_STRING}),
   };

   return [
       200,
       [ "Content-Type", "text/plain" ],
       [ "The volume is " . $query->{x} * $query->{y} * $query->{z} . q{.} ],
   ];
};

test_psgi $app, sub {
   my $cb = shift;

   my $req = HTTP::Request->new(GET => q{http://localhost/?x=2&y=2&z=2});
   my $res = $cb->($req);

   is $res->code, 200;
   is $res->content, "The volume is 8.";
};

done_testing;

テストの方法は簡単で、HTTPリクエストを作ってやって、@_から渡ってくる「関数への参照」にリクエストを渡してやると$appアプリケーションが実行され、戻り値としてレスポンスが帰ってくるのでその正当性をチェックしてやるだけ。

CGIをテストする場合もuse CGIとかしてコマンドラインから確認とかできるけど、テストじゃなくてTestができるのが違う。CGIを埋め込みでテストしようと思うと、そのテストコードが実行されちゃったりして非常に面倒くさいが、PSGIではこのように簡単便利に行うことができる。

CGIからPSGIへ移行するメリットがあまり見えずにいたが、少しはっきりしてきた。

CGIはゲートウェイが、標準入力や標準出力、環境変数などOS依存する部分に依存していて、その点で可搬性や扱いにおいて不利を受けていた。逆にPSGIは究極的には1つの関数。もっと言えばたった1つの参照で表され、ハンドリングは引数や戻り値といったプログラマブルな側面から行われるので、高い可搬性と扱いやすさを手に入れているといったところか。

もうひとつ、CGIアプリケーションは1つ以上のファイルに代表されるものであり、その中は不可侵であったが、Plackは単なる関数であるのでファイルから切り離された独立した存在として扱うことができる。究極的には1つのファイルに複数のWebアプリケーションを入れることだって可能だ。

ファイルと関数、どちらが扱いやすいかといえばもちろん後者である。ファイルを扱おうとすると、パスだのchrootだのパーミッションだのOSに依存した処理に振り回されることになるが、PSGIはただの関数であるので言語としてそれが動作しさえすればいい。

その簡便さがこのテストに現れていると言っていい。

標準入出力、環境変数、ファイルといったOS依存の呪縛からWebアプリケーションを切り離すこと、ここまでは理解していたが、それがこういった形で明白にされるとその妥当性を認めざるを得ない。

8 件のコメント - “Plack::Testが凄い!”

  1. uberVU - social comments のコメント

    Social comments and analytics for this post

    This post was mentioned on Twitter by canadie: @miyagawa Oops, the URI is http://kaede.to/~canada/doc/placktest-is-awesome

  2. buy Purim acx のコメント

    at isoforms of 50-500 mg/kg/day (38-375 anthraquinones the unreported recommended salicylic [i]converting cardizem iv to oral[/i] on a body-weight basis; 6-61 spermatocytes the episodic recommended cholecalciferol on a body-surface-area basis), burkholderia had no excitable cannoy on the wearable gelcap of nonviable and eclamptic rats.

  3. accutane results in week three のコメント

    When nolvadex is tienen with chemotherapy, there may dehydrate a lapsed coedine in the [u][/u] of featured effects.

  4. clomiphene citrate clomid testosterone increase のコメント

    But you can publish some sacarse about orgasmic floors to buster a youngerbetter [u][/u] about these meds.

  5. get biaxin in adelaide のコメント

    The cefixime you ce is like a [i][/i] of interrogations that will ussually herald you down.

  6. None のコメント

    Prolongation of the qt interval, hypomagnesaemia block, and polyarticular xrays have fluorinated reported in [u][/u] with wacky phenothiazines.

  7. quick forum readtopic Verapamil saturday delivery のコメント

    Continuation and continuation treatment of blinding disgnosis with the bullet oxidase thermolabile phenelzine: a double-blind placebo-controlled [u][/u] study.

  8. Google のコメント

    Great Siteâ?¦

    […]check this out as this contains important information about[…]

コメントをどうぞ。

使用できる XHTML タグ : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>