sinatra��ActiveRecord��ERB��BBS��ä��Τǥ�������������Ƥߤ� - ��������˿�ȯ����Ʋ���BBS���äƤߤ褦�ȡ���������Sinatra�򤤤��äƤ�����Sinatra����ϺǶ�С�����󥢥åפ��Ƥ��������뤱�ɡ��ɤ���������Ruby1.9.1�Ǥ�ư���褦������Ũ��

�ǡ���Ĵ�˹Ԥ����Ȼפä�������ɡ��ɤ��ˤ�񤭹��ߤ�spec���̤�ʤ��������ǽ��ơ�POST��GET�ǥѥ�᡼���˥ޥ���Х��Ȥ�ʸ�������äƤ�Ȳ��������������Ȥ˵��դ�����

�ǽ�˽񤤤�spec�ȥ��ץ�¦�Υ����ɤ�ȴ�衣

# coding: utf-8
require 'rubygems'
require 'rack/test'
require 'routes.rb' #sinatra���ץ�

set :environment, :test

include Rack::Test::Methods

def app 
  Sinatra::Application
end

describe '��Ƥ���Ȥ�' do
  it '����Ƥ���������������˥�����쥯�Ȥ��뤳��' do
    @props = { 
      'user'  => '����',
      'body'  => '�١��̤˥��󥿤���ʸ�����ۤʤ󤸤�ʤ�������顪',
    }
    Entry.should_receive(:create).with(@props)
    post '/add', @props
    last_response.status.should be_equal(302)
  end 
end
# coding: utf-8
require 'rubygems'
require 'sinatra'
require 'haml'
require 'sequel'

post '/add' do
  Entry.create(params)
  redirect '/'
end

DB = Sequel.connect('sqlite://db/trabbs.db')
class Entry < Sequel::Model; end

�ºݤˤ�get '/'�Ȥ�Entry�Υ������ޤȤ�index.haml�Ȥ����Ѱդ��Ƥ���ޤ�������Ͽ����ܤ��䴰���Ƥ����졣��ǡ����Ԥ��Ƥ�ΤϤ�����������ĥ�ǥ����Ƥ򤷤Ƥ��줬DB�˵�Ͽ���졢���θ�ȥåפ˥�����쥯�Ȥ���뤳�Ȥʤ�����ɡ����줬�����롣

1)
Spec::Mocks::MockExpectationError in '��Ƥ���Ȥ� �ϡ���Ƥ���������������˥�����쥯�Ȥ��뤳��'
 expected :create with ({"user"=>"����", "body"=>"�١��̤˥��󥿤���ʸ�����ۤʤ󤸤�ʤ�������顪"}) but received it with ({"user"=>"\xE3\x82", "body"=>"\xE3\x81\xB9\xE3\x80\x81\xE5\x88\xA5\xE3\x81\xAB\xE3\x82\xA2\xE3\x83\xB3\xE3\x82\xBF\xE3\x81\xAE"})

����㡣UTF-8��ʸ���󤬤���٤��Ȥ����ˡ�ASCII-8BIT���Ĥޤ�Х����󤬤��Τޤ������äƤ롣��ࡢ�����ȡ��ºݤΥХ�������ѹ������˥��󥳡��ǥ��󥰤����Ѥ���Τ�force_encoding���ä���͡��Ȥ������Ȥ�force_encoding('UTF-8')���Ƥߤ롣

1)
Spec::Mocks::MockExpectationError in '��Ƥ���Ȥ� �ϡ���Ƥ���������������˥�����쥯�Ȥ��뤳��'
 expected :create with ({"user"=>"����", "body"=>"�١��̤˥��󥿤���ʸ�����ۤʤ󤸤�ʤ�������顪"}) but received it with ({"user"=>"\xE3\x82", "body"=>"�١��̤˥��󥿤�"})

�������졩�ʤ�­��ʤ��ʤ�������Ϥɤ��������ȡ�

Rack::Utils#escape

�Ȥ����ǡ���Ǥ�ü�ޤä����ɡ��¤�spec��¹Ԥ���ݤˤ����warning���ФƤ��롣

/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.0.0/lib/rack/utils.rb:13: warning: regexp match /.../n against to UTF-8 string

UTF-8���Ф��ƥ��󥳡��ǥ��󥰤�̵�뤷������ɽ���˥ޥå������Ƥ�Ĥ��󡢤ɤ��⤳�줬�����������Ƥ���Ϥɤ�����ФƤ뤫�Ȥ����ȡ�����ʥ����ɤ���ФƤ��롣

# rack-1.0.0/lib/rack/utils.rb
module Rack
  # Rack::Utils contains a grab-bag of useful methods for writing web

  module Utils
    # Performs URI escaping so that you can construct proper
    # query strings faster.  Use this rather than the cgi.rb
    # version since it's faster.  (Stolen from Camping).
    def escape(s)
      s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
        '%'+$1.unpack('H2'*$1.size).join('%').upcase
      }.tr(' ', '+')
    end
    module_function :escape

    # Unescapes a URI escaped string. (Stolen from Camping).
    def unescape(s)
      s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
        [$1.delete('%')].pack('H*')
      }
    end
    module_function :unescape

    # �ʲ���ά
  end
end

�ʤ�ۤɤǤ��͡����פ���ˡ�

  1. escape���unpack����Ȥ��ˡ�String#size��ȤäƤ��롣1.8�ޤǤϤ��Υ᥽�åɤϥХ��ȿ����֤��Ƥ����ɡ�1.9�Ǥ�ʸ�������֤��褦�˻��ͤ��ѹ�����Ƥ������ޥ���Х��Ȥ�ʸ������������Х��ȿ���ʸ�����ϰ��פ��ʤ��Τǡ�������ڤ���㤦���Ȥˤʤ롣
  2. pack�����֤äƤ���Τ�ñ�ʤ�Х����󡢤Ĥޤ�ASCII-8BIT��String��
  3. 1.8�ޤǤ�ʸ���󤬤ɤ�ʥ��󥳡��ǥ��󥰤Ǥ��졢unpack����pack����Фޤ�ʸ�����������Ǥ�1.9�Ǥ�unpack����pack�β����Ǹ��Υ��󥳡��ǥ��󥰾���ϼ��ʤ��Ƥ���Τǡ�����Ū�˥��󥳡��ǥ��󥰾�����ղä��Ƥ��ɬ�פ�������

�Ȥ������Ȥ����顣�ޤ�1�˴ؤ��Ƥ�String#size��ȤäƤ�Ȥ�����String#bytesize��Ȥ��褦�˽񤭴�����Ф��������ʤߤ�Rack::Utils�Ǥ�1.8�Ǥ�1.9�Ǥ�ư���褦��Rack::Utils#bytesize�Ȥ����᥽�åɤ��������Ƥ���Τǡ������Ȥ��Ȥ������Ȥ��������ʤ�ǻȤäƤʤ����������ñ��˺��Ƥ��������ʤ��ġ�

module Rack
  module Utils                                                                          
    def escape(s)
      s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/) {                                              
        '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase                               
      }.tr(' ', '+')
    end
  end
end

�����rack��require������˽񤤤�Rack::Utils#escape���񤭤��Ƥ��ȡ�������escape�����褦�ˤʤ롣

����ASCII-8BIT�ˤʤä���äƤ�Τ�force_encoding���ƥ��󥳡��ǥ��󥰤��Ѥ��롣�����������Rack::Utils#unescape���񤭤��ơ�������ǥ��󥳡��ǥ��󥰤��¬����force_encoding���Ƥ����ơ����ץ�¦�Υեå���String#encoding��Ȥä������ǻȤ����󥳡��ǥ��󥰤��Ѵ����Ƥ��Τ��������ʤ��Ȥ��פä�������ɡ�����Ǥ����Τ��ʡ���

��Ǥ�ޤ��������UA�˱�����view�Υ��󥳡��ǥ����Ѥ���ߤ����ʤ��ȤϤ��Ƥʤ�����UTF-8����Ǥ����ɤ���͡��ĤȤ����櫓�ǡ�Sinatra��before����ǡ�RUBY_VERSION��1.9�ʾ���ä��Ȥ���param��ʸ�����force_encoding('UTF-8')����褦�ˤ����ä����ɤ��ҤϤ����Ȥ��Ʋ�������

�Ϥ�������Ǥ�����spec�̤�ޤ������ա���

�Ĥǡ�

�������顢���٤�Rack��fork����ľ����pull request����Ф��������͡�����������ɡ����ʤ���ʡ��Ȥϻפ�������ɡ����ɤ������դΤ���޼����ʤ��Τǡ�ï��1.9�Ǥ������ư���褦��ľ����Rack�γ�ȯ�����åդ˶����Ƥ����Ƥ�館��Ƚ�����ޤ��������ƥ������͡�