Skip to content

Instantly share code, notes, and snippets.

@memememomo
Created July 2, 2014 13:31
Show Gist options
  • Select an option

  • Save memememomo/3e129acbb0acd4f06d96 to your computer and use it in GitHub Desktop.

Select an option

Save memememomo/3e129acbb0acd4f06d96 to your computer and use it in GitHub Desktop.
Glint + WEBrick で一時的なWebサーバを立ててテストする ref: http://qiita.com/uchiko/items/5af8a8e643ba94a3dc8e
.
├── Gemfile
├── Rakefile
└── spec
├── lib
│   └── foo_spec.rb
├── servers
│   └── httpserver.rb
└── spec_helper.rb
$ bundle install
$ bundle exec -- rake
require 'spec_helper'
describe 'foo' do
include_context 'httpserver'
before { @res = client.get('/foo') }
it { expect(@res.body).to be == 'foo' }
end
source 'https://rubygems.org'
gem 'glint'
gem 'rake'
gem 'rspec'
# 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,
}
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
$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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment