text/htmlやtext/plainの文字列エンコーディングを指定できるのですが、これが厄介です。
何故なら一般的なブラウザはHTML内のmetaタグでの文字列エンコーディングより、HTTPレスポンスヘッダーの文字列エンコーディングを優先するからです。
一つのapacheで複数文字列エンコーディングを指定する場合には
- metaタグを駆使する
- こまめにディレクトリを分けてAddDefaultCharsetを使う
例 $ curl -k \ -d "pull[base]=master" \ -d "pull[head]=master" \ -d "pull[issue]=1234" \ -u "kennyj:xxxxx" \ https://github.com/api/v2/json/pulls/rails/rails -k SSL証明書の問題?を避ける -d "pull[base]=master" 送信先のbranch名 -d "pull[head]=master" 送信元のbranch名 -d "pull[issue]=1234" バグ票番号 -u "kennyj:xxxxx" ユーザ名とパスワード https://github.com/api/v2/json/pulls/rails/rails 送信先のユーザ名とリポジトリ名無駄にIssueを増やさない為にも知ってて損はなさそうです。
#!/bin/sh | |
# railsプロジェクト作成して移動する | |
rails new "$1" -T | |
cd "$1" | |
# Gemfileに必要なgemを追記する | |
cat << HERE >> Gemfile | |
gem 'rails-backbone' | |
group :development, :test do | |
gem 'rspec' | |
gem 'rspec-rails' | |
gem 'capybara' | |
gem 'capybara-webkit' | |
gem 'headless' | |
gem 'simplecov' | |
gem 'simplecov-rcov' | |
gem 'ci_reporter' | |
end | |
HERE | |
perl -pi -e "s/# gem 'capistrano'/gem 'capistrano'/g" Gemfile | |
perl -pi -e "s/# gem 'ruby-debug19'/gem 'ruby-debug19'/g" Gemfile | |
# gemをインストールする | |
bundle install | |
# ジェネレータを起動する | |
bundle exec rails g rspec:install | |
bundle exec rails g backbone:install | |
bundle exec capify . | |
# 日本語翻訳ファイルを用意する | |
perl -pi -e 's/# config\.i18n\.default_locale = :de/config.i18n.default_locale = :ja/g' config/application.rb | |
cd config/locales | |
wget https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/ja.yml | |
cat <<HERE > translation_ja.yml | |
ja: | |
activerecord: | |
models: | |
attributes: | |
HERE | |
cd - | |
# specコマンドへのオプションを指定する | |
cat << HERE > .rspec | |
--color | |
--format documentation | |
--drb | |
HERE | |
# .gitignoreに履歴管理しないファイルを記載する | |
cat << HERE > .gitignore | |
*.swp | |
**.orig | |
*.rbc | |
*.sassc | |
.sass-cache | |
capybara-*.html | |
.rspec | |
/.bundle | |
/vendor/bundle | |
/log/* | |
/tmp/* | |
/db/*.sqlite3 | |
/db/*.db | |
/public/system/* | |
/coverage/ | |
/spec/reports/ | |
/spec/tmp/* | |
config/*.yml | |
rerun.txt | |
pickle-email-*.html | |
HERE | |
# レイアウトに文字コード指定を追記する | |
cat << HERE > app/views/layouts/application.html.erb | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>$1</title> | |
<meta charset="utf-8"/> | |
<%= stylesheet_link_tag "application" %> | |
<%= javascript_include_tag "application" %> | |
<%= csrf_meta_tag %> | |
</head> | |
<body> | |
<%= yield %> | |
</body> | |
</html> | |
HERE | |
# simplecov、capybara利用をspec_helper.rbに付加 | |
cat << HERE > spec/spec_helper.rb | |
if %w(yes y on).include?(ENV['COVERAGE']) | |
require 'simplecov' | |
require 'simplecov-rcov' | |
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter | |
SimpleCov.start 'rails' | |
end | |
# This file is copied to spec/ when you run 'rails generate rspec:install' | |
ENV["RAILS_ENV"] ||= 'test' | |
require File.expand_path("../../config/environment", __FILE__) | |
require 'rspec/rails' | |
require 'capybara/rails' | |
require 'capybara/rspec' | |
# Requires supporting ruby files with custom matchers and macros, etc, | |
# in spec/support/ and its subdirectories. | |
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} | |
RSpec.configure do |config| | |
# == Mock Framework | |
# | |
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: | |
# | |
# config.mock_with :mocha | |
# config.mock_with :flexmock | |
# config.mock_with :rr | |
config.mock_with :rspec | |
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures | |
config.fixture_path = "#{::Rails.root}/spec/fixtures" | |
# If you're not using ActiveRecord, or you'd prefer not to run each of your | |
# examples within a transaction, remove the following line or assign false | |
# instead of true. | |
config.use_transactional_fixtures = true | |
end | |
# see http://kennyj-jp.blogspot.com/2011/08/capybara-webkit.html | |
Capybara.javascript_driver = :webkit | |
# see http://kennyj-jp.blogspot.com/2011/09/capybarawebkitselenium.html | |
class ActiveRecord::Base | |
mattr_accessor :shared_connection | |
@@shared_connection = nil | |
def self.connection | |
@@shared_connection || retrieve_connection | |
end | |
end | |
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection | |
HERE | |
# headlessをサポート | |
mkdir -p spec/support | |
cat << HERE > spec/support/headless.rb | |
if %w(yes y on).include?(ENV['HEADLESS']) | |
require 'headless' | |
headless = Headless.new | |
headless.start | |
at_exit do | |
headless.destroy | |
end | |
end | |
HERE | |
# ci_reporterのtaskを追加 | |
cat << HERE > lib/tasks/ci_reporter.rake | |
require 'ci/reporter/rake/rspec' | |
HERE | |
# stagingに対応しておく | |
cp config/environments/production.rb config/environments/staging.rb | |
cat << HERE >> config/database.yml | |
staging: | |
adapter: sqlite3 | |
database: db/staging.sqlite3 | |
pool: 5 | |
timeout: 5000 | |
HERE | |
# database.ymlは履歴管理しないので | |
cp config/database.yml config/database.yml.example | |
# http://kennyj-jp.blogspot.com/2011/08/rails3sub.html | |
perl -pi -e 's/(run .+Application)/map ActionController::Base.config.relative_url_root || "\/" do\n \1\nend/g' config.ru | |
# 履歴管理開始する | |
git init | |
git add . | |
git commit -m "first commit" | |
# bundle exec rails g scaffold post title:string body:text published:boolean | |
# rm public/index.html | |
# とりあえず一度specを流す | |
bundle exec rake db:migrate | |
HEADLESS=yes COVERAGE=yes bundle exec rake ci:setup:rspec spec | |
# とりあえずサーバ起動 | |
bundle exec rails s |
# app/controllers/foo_controller.rb class FooController < ApplicationController def index puts cookies["key1"] cookies["key1"] = '2' cookies.permanent["key2"] = "3" end end # spec/requests/foo_spec.rb describe "foo周辺の仕様" do def cookies Capybara.current_session.driver.browser.current_session.instance_variable_get(:@rack_mock_session).cookie_jar end it "foo/index" do cookies["key1"] = '1' visit "/foo/index" cookies["key1"].should == '2' cookies["key2"].should == '3' end end上記の検証の過程で、