7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Glint + WEBrick で一時的なWebサーバを立ててテストする

Posted at

概要

HTTPクライアントなアプリをテストするときに、一時的にHTTPサーバを立てたい。
Glint と WEBrick を使用すれば一時的なHTTPサーバを立てることができる。

Glint

Glint は、空いているポートを検索して、
サーバ用プロセスとクライアント用プロセスを作成するモジュール。
サーバ用プロセスでポートがListen状態になるまで待ち、
Listen状態になったらクライアントプロセスでテストができるように調整してくれる。

サーバプロセスでは、WEBrickでHTTPサーバを立て、
クライアントプロセスでは、Net::HTTPでアクセスしてレスポンスの内容をテストする形になる。

ディレクトリ構成

今回のサンプルのディレクトリ構成は以下の通り。

.
├── Gemfile
├── Rakefile
└── spec
    ├── lib
    │   └── foo_spec.rb
    ├── servers
    │   └── httpserver.rb
    └── spec_helper.rb

今回、解説しないファイルの内容は以下の通り。

Gemfile
source 'https://rubygems.org'

gem 'glint'
gem 'rake'
gem 'rspec'
Rakefile
require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
  spec.pattern = FileList['spec/**/*_spec.rb']
  spec.ruby_opts = %w[-w]
end

task :default => :spec

サーバ設定

以下のプログラムで、サーバを立てる。

spec/servers/httpserver.rb
# Glintで一時的なWebサーバを生成
server = Glint::Server.new do |port|

  # WEBrickでWebサーバを生成
  require 'webrick'
  http = WEBrick::HTTPServer.new({
    :BindAddress => '127.0.0.1',
    :Port => port
  })
  http.mount_proc('/foo') do |req, res|
    body = 'foo'

    res.content_length = body.size
    res.content_type = 'text/plain'
    res.body = body
  end
  trap(:INT) { http.shutdown }
  trap(:TERM) { http.shutdown }
  http.start
end
server.start

# 立ち上げたサーバの Host と Port の情報を保存
Glint::Server.info[:httpserver] = {
  host: "127.0.0.1",
  port: server.port,
}

ヘルパーの設定

以下のプログラムで、上記のサーバ設定プログラムを実行し、
また、テストで使うHTTPクライアントを設定している。

今回は Net::HTTP を使用しているが、
本来ならば自作のなんらかのHTTPクライアントを設定することになる。

spec/spec_helper.rb
$LOAD_PATH.unshift '../../lib'

# サーバ起動のスクリプトを読み込む
require 'glint'
Dir[File.expand_path("../servers/*.rb", __FILE__)].each {|f| require f}

# テストで使うHTTPクライアントを生成する
require 'rspec'
shared_context 'httpserver' do
  require 'net/http'
  let(:client) {
    Net::HTTP.new(
      Glint::Server.info[:httpserver][:host],
      Glint::Server.info[:httpserver][:port]
    )
  }
end

RSpec.configure do |config|
end

テストを書く

以下のプログラムで、テストをする。

spec/lib/foo_spec.rb
require 'spec_helper'

describe 'foo' do
    include_context 'httpserver'
    before { @res = client.get('/foo') }
    it { expect(@res.body).to be == 'foo' }
end

テストの実行

以下のようなコマンドでテストを実行する。

$ bundle install
$ bundle exec -- rake

テストを実行すると、
WEBrick が起動して、テストが実行され、WEBrick が停止するといったログが出力されるはず。

本来のテストでは WEBrick の出力は邪魔になるので、
出力されないようになんとかしたい。

参考

WEBrickを使うためのメモ
http://d.hatena.ne.jp/lemniscus/20090722/1248261257

glint examples
https://github.com/kentaro/glint/tree/master/examples

7
7
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
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?