Created
July 2, 2014 13:31
-
-
Save memememomo/3e129acbb0acd4f06d96 to your computer and use it in GitHub Desktop.
Glint + WEBrick で一時的なWebサーバを立ててテストする ref: http://qiita.com/uchiko/items/5af8a8e643ba94a3dc8e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| . | |
| ├── Gemfile | |
| ├── Rakefile | |
| └── spec | |
| ├── lib | |
| │ └── foo_spec.rb | |
| ├── servers | |
| │ └── httpserver.rb | |
| └── spec_helper.rb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ bundle install | |
| $ bundle exec -- rake |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'spec_helper' | |
| describe 'foo' do | |
| include_context 'httpserver' | |
| before { @res = client.get('/foo') } | |
| it { expect(@res.body).to be == 'foo' } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source 'https://rubygems.org' | |
| gem 'glint' | |
| gem 'rake' | |
| gem 'rspec' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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, | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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