forked from iain/http_accept_language
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test integration into Rails controller
- Loading branch information
Showing
6 changed files
with
184 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,37 @@ | ||
Before "@rails" do | ||
@rails = RailsDriver.new | ||
end | ||
|
||
When /^I generate a new Rails app$/ do | ||
@aruba_io_wait_seconds = 10 | ||
app_name = "foobar" | ||
# install rails with as few things as possible, for speed! | ||
run_simple "bundle exec rails new #{app_name} --skip-git --skip-active-record --skip-sprockets --skip-javascript --skip-test-unit --old-style-hash" | ||
cd app_name | ||
@rails.generate_rails | ||
end | ||
|
||
When /^I add http_accept_language to my Gemfile$/ do | ||
# Specifiy a path so cucumber will use the unreleased version of the gem | ||
path = File.expand_path('../../../', __FILE__) | ||
append_to_file "Gemfile", "gem 'http_accept_language', :path => '#{path}'" | ||
@rails.append_gemfile | ||
end | ||
|
||
Given /^I have installed http_accept_language$/ do | ||
@rails.install_gem | ||
end | ||
|
||
When /^I generate the following controller:$/ do |string| | ||
@rails.generate_controller "languages", string | ||
end | ||
|
||
When /^I access that action with the HTTP_ACCEPT_LANGUAGE header "(.*?)"$/ do |header| | ||
@rails.with_rails_running do | ||
@rails.request_with_http_accept_language_header(header, "/languages") | ||
end | ||
end | ||
|
||
Then /^the response should contain "(.*?)"$/ do |output| | ||
@rails.output_should_contain(output) | ||
end | ||
|
||
When /^I run `rake middleware`$/ do | ||
@rails.bundle_exec("rake middleware") | ||
end | ||
|
||
Then /^the output should contain "(.*?)"$/ do |expected| | ||
@rails.assert_partial_output(expected, @rails.all_output) | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
require 'aruba/api' | ||
|
||
class RailsDriver | ||
include Aruba::Api | ||
|
||
def initialize | ||
@aruba_io_wait_seconds = 10 | ||
# @announce_stdout = true | ||
# @announce_stderr = true | ||
# @announce_cmd = true | ||
# @announce_dir = true | ||
# @announce_env = true | ||
end | ||
|
||
def app_name | ||
"foobar" | ||
end | ||
|
||
def install_gem | ||
if app_exists? | ||
cd app_name | ||
else | ||
generate_rails | ||
append_gemfile | ||
end | ||
end | ||
|
||
def app_exists? | ||
in_current_dir do | ||
File.exist?("#{app_name}/Gemfile") | ||
end | ||
end | ||
|
||
def bundle_exec(cmd) | ||
run_simple "bundle exec #{cmd}" | ||
end | ||
|
||
def generate_rails | ||
# install rails with as few things as possible, for speed! | ||
bundle_exec "rails new #{app_name} --force --skip-git --skip-active-record --skip-sprockets --skip-javascript --skip-test-unit --old-style-hash" | ||
cd app_name | ||
end | ||
|
||
def append_gemfile | ||
# Specifiy a path so cucumber will use the unreleased version of the gem | ||
append_to_file "Gemfile", "gem 'http_accept_language', :path => '#{gem_path}'" | ||
end | ||
|
||
def gem_path | ||
File.expand_path('../../../', __FILE__) | ||
end | ||
|
||
def generate_controller(name, content) | ||
bundle_exec "rails generate resource #{name} --force" | ||
write_file "app/controllers/#{name}_controller.rb", content | ||
end | ||
|
||
def request_with_http_accept_language_header(header, path) | ||
run_simple "curl --retry 10 -H 'Accept-language: #{header}' #{File.join(host, path)} -o #{response}" | ||
run_simple "cat out.html" | ||
end | ||
|
||
def host | ||
"http://localhost:13000" | ||
end | ||
|
||
def with_rails_running | ||
start_rails | ||
yield | ||
ensure | ||
stop_rails | ||
end | ||
|
||
def start_rails | ||
bundle_exec "rails server -p 13000 -d" | ||
end | ||
|
||
def stop_rails | ||
in_current_dir do | ||
`cat tmp/pids/server.pid | xargs kill -9` | ||
end | ||
end | ||
|
||
def response | ||
File.expand_path(File.join(current_dir, 'out.html')) | ||
end | ||
|
||
def output_should_contain(expected) | ||
actual = File.open(response, 'r:utf-8').read | ||
actual.should include expected | ||
end | ||
|
||
end |
This file contains 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