jpmobile 0.0.8 → 0.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. data/.gitignore +0 -3
  2. data/{spec/spec.opts → .rspec} +1 -2
  3. data/README.rdoc +122 -128
  4. data/Rakefile +23 -101
  5. data/VERSION.yml +4 -3
  6. data/jpmobile.gemspec +130 -182
  7. data/lib/jpmobile.rb +37 -24
  8. data/lib/jpmobile/docomo_guid.rb +2 -1
  9. data/lib/jpmobile/email.rb +3 -4
  10. data/lib/jpmobile/emoticon.rb +2 -6
  11. data/lib/jpmobile/encoding.rb +29 -0
  12. data/lib/jpmobile/filter.rb +42 -134
  13. data/lib/jpmobile/helpers.rb +5 -18
  14. data/lib/jpmobile/hook_action_controller.rb +24 -16
  15. data/lib/jpmobile/hook_action_view.rb +28 -89
  16. data/lib/jpmobile/mobile/abstract_mobile.rb +21 -4
  17. data/lib/jpmobile/mobile/android.rb +10 -0
  18. data/lib/jpmobile/mobile/au.rb +28 -3
  19. data/lib/jpmobile/mobile/ddipocket.rb +12 -0
  20. data/lib/jpmobile/mobile/display.rb +29 -26
  21. data/lib/jpmobile/mobile/docomo.rb +38 -12
  22. data/lib/jpmobile/mobile/iphone.rb +26 -0
  23. data/lib/jpmobile/mobile/smart_phone.rb +21 -0
  24. data/lib/jpmobile/mobile/softbank.rb +16 -41
  25. data/lib/jpmobile/mobile/vodafone.rb +17 -0
  26. data/lib/jpmobile/mobile/willcom.rb +1 -10
  27. data/lib/jpmobile/mobile/windows_phone.rb +10 -0
  28. data/lib/jpmobile/position.rb +4 -3
  29. data/lib/jpmobile/rack.rb +43 -0
  30. data/lib/jpmobile/rack/filter.rb +51 -0
  31. data/lib/jpmobile/rack/mobile_carrier.rb +33 -0
  32. data/lib/jpmobile/rack/params_filter.rb +47 -0
  33. data/lib/jpmobile/rails.rb +22 -0
  34. data/lib/jpmobile/request_with_mobile.rb +8 -12
  35. data/lib/jpmobile/trans_sid.rb +44 -26
  36. data/lib/jpmobile/util.rb +68 -0
  37. data/{tasks → lib/tasks}/jpmobile_tasks.rake +32 -13
  38. data/spec/rack/jpmobile/android_spec.rb +20 -0
  39. data/spec/rack/jpmobile/au_spec.rb +206 -0
  40. data/spec/rack/jpmobile/docomo_spec.rb +237 -0
  41. data/spec/rack/jpmobile/emoticon_spec.rb +215 -0
  42. data/spec/rack/jpmobile/filter_spec.rb +181 -0
  43. data/spec/rack/jpmobile/iphone_spec.rb +32 -0
  44. data/spec/rack/jpmobile/mobile_by_ua_spec.rb +39 -0
  45. data/spec/rack/jpmobile/params_filter_spec.rb +193 -0
  46. data/spec/rack/jpmobile/softbank_spec.rb +123 -0
  47. data/spec/rack/jpmobile/willcom_spec.rb +67 -0
  48. data/spec/rack/jpmobile/windows_phone.rb +20 -0
  49. data/spec/rack_helper.rb +86 -0
  50. data/spec/spec_helper.rb +3 -2
  51. data/spec/unit/detect_by_email_spec.rb +21 -27
  52. data/spec/unit/is_carrier_spec.rb +56 -84
  53. data/spec/unit/spec_helper.rb +1 -2
  54. data/spec/unit/util_spec.rb +2 -2
  55. data/spec/unit/valid_ip_spec.rb +43 -52
  56. data/test/legacy/autoload_test.rb +1 -3
  57. data/test/legacy/emoticon_test.rb +5 -6
  58. data/test/legacy/helper.rb +0 -71
  59. data/test/rails/overrides/.rspec +5 -0
  60. data/test/rails/overrides/Gemfile +32 -0
  61. data/test/rails/overrides/app/controllers/docomo_guid_always_controller.rb +3 -0
  62. data/test/rails/overrides/app/controllers/docomo_guid_base_controller.rb +5 -0
  63. data/test/rails/overrides/app/controllers/docomo_guid_docomo_controller.rb +3 -0
  64. data/test/rails/overrides/app/controllers/filter_controller.rb +0 -34
  65. data/test/rails/overrides/app/controllers/filter_controller_base.rb +19 -0
  66. data/test/rails/overrides/app/controllers/hankaku_filter_controller.rb +3 -0
  67. data/test/rails/overrides/app/controllers/links_controller.rb +28 -0
  68. data/test/rails/overrides/app/controllers/mobile_spec_controller.rb +1 -1
  69. data/test/rails/overrides/app/controllers/template_path_controller.rb +3 -0
  70. data/test/rails/overrides/app/controllers/trans_sid_always_and_session_off_controller.rb +4 -0
  71. data/test/rails/overrides/app/controllers/trans_sid_always_controller.rb +3 -0
  72. data/test/rails/overrides/app/controllers/trans_sid_base_controller.rb +18 -0
  73. data/test/rails/overrides/app/controllers/trans_sid_mobile_controller.rb +3 -0
  74. data/test/rails/overrides/app/controllers/trans_sid_none_controller.rb +3 -0
  75. data/test/rails/overrides/app/views/links/au_gps.html.erb +1 -0
  76. data/test/rails/overrides/app/views/links/au_location.html.erb +1 -0
  77. data/test/rails/overrides/app/views/links/docomo_foma_gps.html.erb +1 -0
  78. data/test/rails/overrides/app/views/links/docomo_openiarea.html.erb +1 -0
  79. data/test/rails/overrides/app/views/links/docomo_utn.html.erb +1 -0
  80. data/test/rails/overrides/app/views/links/link.html.erb +1 -0
  81. data/test/rails/overrides/app/views/links/show_all.html.erb +1 -0
  82. data/test/rails/overrides/app/views/links/softbank_location.html.erb +1 -0
  83. data/test/rails/overrides/app/views/links/willcom_location.html.erb +1 -0
  84. data/test/rails/overrides/app/views/template_path/_partial_smart_phone.html.erb +1 -0
  85. data/test/rails/overrides/app/views/template_path/_partial_smart_phone_iphone.html.erb +1 -0
  86. data/test/rails/overrides/app/views/template_path/index_smart_phone.html.erb +1 -0
  87. data/test/rails/overrides/app/views/template_path/index_smart_phone_iphone.html.erb +1 -0
  88. data/test/rails/overrides/app/views/template_path/show_mobile.html.erb +1 -0
  89. data/test/rails/overrides/app/views/template_path/show_mobile_docomo.html.erb +1 -0
  90. data/test/rails/overrides/autotest/discover.rb +2 -0
  91. data/test/rails/overrides/config/initializers/jpmobile_generator.rb +1 -0
  92. data/test/rails/overrides/config/routes.rb +58 -0
  93. data/test/rails/overrides/db/migrate/001_add_sessions_table.rb +16 -0
  94. data/test/rails/overrides/spec/helpers/helpers_spec.rb +2 -2
  95. data/test/rails/overrides/spec/{controllers → requests}/docomo_guid_spec.rb +37 -14
  96. data/test/rails/overrides/spec/{controllers → requests}/docomo_spec.rb +11 -8
  97. data/test/rails/overrides/spec/{controllers → requests}/emobile_spec.rb +24 -8
  98. data/test/rails/overrides/spec/requests/filter_spec.rb +171 -0
  99. data/test/rails/overrides/spec/requests/helpers_spec.rb +187 -0
  100. data/test/rails/overrides/spec/requests/pc_spec.rb +22 -0
  101. data/test/rails/overrides/spec/requests/softbank_emulator_spec.rb +21 -0
  102. data/test/rails/overrides/spec/requests/template_path_spec.rb +234 -0
  103. data/test/rails/overrides/spec/requests/trans_sid_spec.rb +142 -0
  104. data/test/rails/overrides/spec/spec_helper.rb +17 -48
  105. data/test/sinatra/guestbook.rb +40 -0
  106. data/test/sinatra/test/filter_test.rb +39 -0
  107. metadata +185 -312
  108. data/lib/jpmobile/hook_request.rb +0 -5
  109. data/lib/jpmobile/rack/auth.rb +0 -55
  110. data/lib/jpmobile/rack/combined_logger.rb +0 -54
  111. data/lib/jpmobile/rack/request.rb +0 -7
  112. data/lib/jpmobile/rack/trans_sid.rb +0 -14
  113. data/lib/jpmobile/version.rb +0 -9
  114. data/spec/unit/detect_by_ua_spec.rb +0 -40
  115. data/test/legacy/au_test.rb +0 -173
  116. data/test/legacy/docomo_test.rb +0 -187
  117. data/test/legacy/emoticon_functional_test.rb +0 -195
  118. data/test/legacy/filter_test.rb +0 -34
  119. data/test/legacy/helpers_test.rb +0 -217
  120. data/test/legacy/softbank_test.rb +0 -183
  121. data/test/legacy/willcom_test.rb +0 -56
  122. data/test/rails/2.3.2/spec/controllers/trans_sid_spec.rb +0 -99
  123. data/test/rails/overrides/app/controllers/docomo_guid_controller.rb +0 -13
  124. data/test/rails/overrides/app/controllers/trans_sid_controller.rb +0 -28
  125. data/test/rails/overrides/lib/tasks/rspec.rake +0 -165
  126. data/test/rails/overrides/spec/controllers/filter_spec.rb +0 -159
  127. data/test/rails/overrides/spec/controllers/pc_spec.rb +0 -13
  128. data/test/rails/overrides/spec/controllers/softbank_emulator_spec.rb +0 -42
  129. data/test/rails/overrides/spec/controllers/template_path_spec.rb +0 -117
  130. data/test/rails/overrides/spec/controllers/trans_sid_spec.rb +0 -117
  131. data/test/rails/overrides/spec/rspec.rake +0 -165
  132. data/test/rails/overrides/spec/spec.opts +0 -4
@@ -3,28 +3,31 @@
3
3
  require File.dirname(__FILE__) + '/../spec_helper'
4
4
 
5
5
  describe "DoCoMo SH902i からのアクセス" do
6
- before do
7
- request.user_agent = "DoCoMo/2.0 SH902i(c100;TB;W24H12)"
8
- end
9
- controller_name :mobile_spec
10
6
  it "request.mobile は Docomo のインスタンスであるべき" do
7
+ get "/mobile_spec/index", {}, {"HTTP_USER_AGENT" => "DoCoMo/2.0 SH902i(c100;TB;W24H12)"}
8
+
11
9
  request.mobile.should be_an_instance_of(Jpmobile::Mobile::Docomo)
12
10
  end
13
11
  it "request.mobile? は true であるべき" do
12
+ get "/mobile_spec/index", {}, {"HTTP_USER_AGENT" => "DoCoMo/2.0 SH902i(c100;TB;W24H12)"}
13
+
14
14
  request.mobile?.should be_true
15
15
  end
16
16
  end
17
17
 
18
18
  describe "DoCoMo SH902i からguid付きのアクセス" do
19
- controller_name :mobile_spec
20
- before do
21
- request.user_agent = "DoCoMo/2.0 SH902i(c100;TB;W24H12)"
22
- request.env['HTTP_X_DCMGUID'] = "000000a"
19
+ before(:each) do
20
+ @headers = {"HTTP_USER_AGENT" => "DoCoMo/2.0 SH902i(c100;TB;W24H12)", 'HTTP_X_DCMGUID' => "000000a"}
23
21
  end
22
+
24
23
  it "guidを正しく取得できること" do
24
+ get "/mobile_spec/index", {}, @headers
25
+
25
26
  request.mobile.guid.should == "000000a"
26
27
  end
27
28
  it "ident_subscriberでも正しく取得できること" do
29
+ get "/mobile_spec/index", {}, @headers
30
+
28
31
  request.mobile.ident_subscriber.should == "000000a"
29
32
  end
30
33
  end
@@ -2,36 +2,52 @@
2
2
 
3
3
  require File.dirname(__FILE__) + '/../spec_helper'
4
4
 
5
- describe "H11T モバイルブラウザからのアクセス", :behaviour_type=>:controller do
5
+ describe "H11T モバイルブラウザからのアクセス" do
6
6
  before do
7
- request.user_agent = "emobile/1.0.0 (H11T; like Gecko; Wireless) NetFront/3.4"
8
- request.env['HTTP_X_EM_UID'] = "u00000000000000000"
9
- request.env['REMOTE_ADDR'] = "117.55.1.232"
7
+ @headers = {
8
+ "HTTP_USER_AGENT" => "emobile/1.0.0 (H11T; like Gecko; Wireless) NetFront/3.4",
9
+ "HTTP_X_EM_UID" => "u00000000000000000",
10
+ "REMOTE_ADDR" => "117.55.1.232",
11
+ }
10
12
  end
11
- controller_name :mobile_spec
13
+
12
14
  it "request.mobile は Emobile のインスタンスであること" do
15
+ get "/mobile_spec/index", {}, @headers
16
+
13
17
  request.mobile.should be_an_instance_of(Jpmobile::Mobile::Emobile)
14
18
  end
15
19
  it "request.mobile? は true であること" do
20
+ get "/mobile_spec/index", {}, @headers
21
+
16
22
  request.mobile?.should be_true
17
23
  end
18
24
  it "のsubscribe番号を取得できること" do
25
+ get "/mobile_spec/index", {}, @headers
26
+
19
27
  request.mobile.ident_subscriber.should == "u00000000000000000"
20
28
  end
21
29
  it "のIPアドレス空間を正しく検証できること" do
30
+ get "/mobile_spec/index", {}, @headers
31
+
22
32
  request.mobile.valid_ip?.should be_true
23
33
  end
24
34
  end
25
35
 
26
- describe "S11HT からのアクセス", :behaviour_type=>:controller do
36
+ describe "S11HT からのアクセス" do
27
37
  before do
28
- request.user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.7) S11HT"
38
+ @headers = {
39
+ "HTTP_USER_AGENT" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.7) S11HT",
40
+ }
29
41
  end
30
- controller_name :mobile_spec
42
+
31
43
  it "request.mobile は Emobile のインスタンスであること" do
44
+ get "/mobile_spec/index", {}, @headers
45
+
32
46
  request.mobile.should be_an_instance_of(Jpmobile::Mobile::Emobile)
33
47
  end
34
48
  it "request.mobile? は true であること" do
49
+ get "/mobile_spec/index", {}, @headers
50
+
35
51
  request.mobile?.should be_true
36
52
  end
37
53
  end
@@ -0,0 +1,171 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ describe "jpmobile integration spec" do
5
+ include Jpmobile::Util
6
+ shared_examples_for "文字コードフィルタが動作しているとき" do
7
+ it "はhtml以外は変換しないこと" do
8
+ get "/#{@controller}/rawdata", {}, {"HTTP_USER_AGENT" => @user_agent}
9
+ body.should == "アブラカダブラ"
10
+ headers['Content-Type'].should_not =~ /charset/i
11
+ end
12
+ it "response.bodyが空のときは文字コードを変更しないこと" do
13
+ get "/#{@controller}/empty", {}, {"HTTP_USER_AGENT" => @user_agent}
14
+ headers['Content-Type'].should =~ /utf-8/i
15
+ end
16
+ end
17
+
18
+ shared_examples_for "Shift_JISで通信する端末との通信" do
19
+ it "はShift_JISで携帯に送出されること" do
20
+ get "/#{@controller}/abracadabra_utf8", {}, {"HTTP_USER_AGENT" => @user_agent}
21
+ body.should == utf8_to_sjis("アブラカダブラ")
22
+ headers['Content-Type'].should =~ /Shift_JIS/i
23
+ end
24
+ it "はxhtmlでもShift_JISで携帯に送出されること" do
25
+ get "/#{@controller}/abracadabra_xhtml_utf8", {}, {"HTTP_USER_AGENT" => @user_agent}
26
+
27
+ body.should == utf8_to_sjis("アブラカダブラ")
28
+ headers['Content-Type'].should =~ /Shift_JIS/i
29
+ end
30
+ it "はShift_JISで渡されたパラメタがparamsにUTF-8に変換されて格納されること" do
31
+ get "/#{@controller}", {:q => utf8_to_sjis("アブラカダブラ")}, {"HTTP_USER_AGENT" => @user_agent}
32
+ assigns(:q).should == utf8("アブラカダブラ")
33
+ end
34
+ it "は半角カナのparamsを変換しないこと" do
35
+ # アブラカダブラ半角,SJIS
36
+ get "/#{@controller}", {:q => sjis("\261\314\336\327\266\300\336\314\336\327")}, {"HTTP_USER_AGENT" => @user_agent}
37
+ assigns(:q).should == utf8("アブラカダブラ")
38
+ end
39
+ it_should_behave_like "文字コードフィルタが動作しているとき"
40
+ end
41
+
42
+ shared_examples_for "UTF-8で通信する端末との通信" do
43
+ it "はUTF-8で携帯に送出されること" do
44
+ get "/#{@controller}/abracadabra_utf8", {}, {"HTTP_USER_AGENT" => @user_agent}
45
+ body.should == "アブラカダブラ"
46
+ headers['Content-Type'].should =~ /utf-8/i
47
+ end
48
+ it "はxhtmlでもUTF-8で携帯に送出されること" do
49
+ get "/#{@controller}/abracadabra_xhtml_utf8", {}, {"HTTP_USER_AGENT" => @user_agent}
50
+ body.should == "アブラカダブラ"
51
+ headers['Content-Type'].should =~ /utf-8/i
52
+ end
53
+ it "はparamsにUTF-8のまま格納されること" do
54
+ get "/#{@controller}/index", {:q => "アブラカダブラ"}, {"HTTP_USER_AGENT" => @user_agent}
55
+ assigns(:q).should == utf8("アブラカダブラ")
56
+ end
57
+ it "は半角カナのparamsを変換しないこと" do
58
+ get "/#{@controller}/index", {:q => "アブラカダブラ"}, {"HTTP_USER_AGENT" => @user_agent}
59
+ assigns(:q).should == utf8("アブラカダブラ")
60
+ end
61
+ it_should_behave_like "文字コードフィルタが動作しているとき"
62
+ end
63
+
64
+ shared_examples_for "Shift_JISで通信する端末との通信(半角変換付き)" do
65
+ it "は半角に変換されShift_JISで携帯に送出されること" do
66
+ get "/#{@controller}/abracadabra_utf8", {}, {"HTTP_USER_AGENT" => @user_agent}
67
+ body.should == sjis("\261\314\336\327\266\300\336\314\336\327") # アブラカダブラ半角,SJIS
68
+ headers['Content-Type'].should =~ /Shift_JIS/
69
+ end
70
+ it "はShift_JISで渡されたパラメタがparamsにUTF-8に変換されて格納されること" do
71
+ get "/#{@controller}/index", {:q => utf8_to_sjis("アブラカダブラ")}, {"HTTP_USER_AGENT" => @user_agent}
72
+ assigns(:q).should == utf8("アブラカダブラ")
73
+ end
74
+ it "は半角Shift_JISで渡されたパラメタがparamsに全角UTF-8に変換されて格納されること" do
75
+ # アブラカダブラ半角,SJIS
76
+ get "/#{@controller}/index", {:q => sjis("\261\314\336\327\266\300\336\314\336\327")}, {"HTTP_USER_AGENT" => @user_agent}
77
+ assigns(:q).should == utf8("アブラカダブラ")
78
+ end
79
+ it_should_behave_like "文字コードフィルタが動作しているとき"
80
+ end
81
+
82
+ shared_examples_for "UTF-8で通信する端末との通信(半角変換付き)" do
83
+ it "はUTF-8半角で携帯に送出されること" do
84
+ get "/#{@controller}/abracadabra_utf8", {}, {"HTTP_USER_AGENT" => @user_agent}
85
+ body.should == "アブラカダブラ"
86
+ headers['Content-Type'].should =~ /utf-8/i
87
+ end
88
+ it "はparamsにUTF-8のまま格納されること" do
89
+ get "/#{@controller}/index", {:q => "アブラカダブラ"}, {"HTTP_USER_AGENT" => @user_agent}
90
+ assigns(:q).should == utf8("アブラカダブラ")
91
+ end
92
+ it "は半角で渡されたparamsを全角に変換して格納すること" do
93
+ get "/#{@controller}/index", {:q => "アブラカダブラ"}, {"HTTP_USER_AGENT" => @user_agent}
94
+ assigns(:q).should == utf8("アブラカダブラ")
95
+ end
96
+ it_should_behave_like "文字コードフィルタが動作しているとき"
97
+ end
98
+
99
+ #
100
+ # PCからのアクセス
101
+ #
102
+ describe FilterController, "PCからのアクセス" do
103
+ before(:each) do
104
+ @user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"
105
+ @controller = "filter"
106
+ end
107
+ it_should_behave_like "UTF-8で通信する端末との通信"
108
+ end
109
+
110
+ describe HankakuFilterController, "PCからのアクセス" do
111
+ before(:each) do
112
+ @user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"
113
+ @controller = "hankaku_filter"
114
+ end
115
+ it_should_behave_like "UTF-8で通信する端末との通信"
116
+ end
117
+
118
+ #
119
+ # 携帯からのアクセス
120
+ #
121
+ describe FilterController, "DoCoMo SH902i からのアクセス" do
122
+ before do
123
+ @user_agent = "DoCoMo/2.0 SH902i(c100;TB;W24H12)"
124
+ @controller = "filter"
125
+ end
126
+ it_should_behave_like "Shift_JISで通信する端末との通信"
127
+ end
128
+
129
+ describe FilterController, "au CA32 からのアクセス" do
130
+ before do
131
+ @user_agent = "KDDI-CA32 UP.Browser/6.2.0.7.3.129 (GUI) MMP/2.0"
132
+ @controller = "filter"
133
+ end
134
+ it_should_behave_like "Shift_JISで通信する端末との通信"
135
+ end
136
+
137
+ describe FilterController, "Vodafone V903T からのアクセス" do
138
+ before do
139
+ @user_agent = "Vodafone/1.0/V903T/TJ001 Browser/VF-Browser/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 Ext-J-Profile/JSCL-1.2.2 Ext-V-Profile/VSCL-2.0.0"
140
+ @controller = "filter"
141
+ end
142
+ it_should_behave_like "UTF-8で通信する端末との通信"
143
+ end
144
+
145
+ describe FilterController, "SoftBank 910T からのアクセス" do
146
+ before do
147
+ @user_agent = "SoftBank/1.0/910T/TJ001/SN000000000000000 Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1"
148
+ @controller = "filter"
149
+ end
150
+ it_should_behave_like "UTF-8で通信する端末との通信"
151
+ end
152
+
153
+ #
154
+ # 半角フィルタ
155
+ #
156
+ describe HankakuFilterController, "DoCoMo SH902i からのアクセス" do
157
+ before do
158
+ @user_agent = "DoCoMo/2.0 SH902i(c100;TB;W24H12)"
159
+ @controller = "hankaku_filter"
160
+ end
161
+ it_should_behave_like "Shift_JISで通信する端末との通信(半角変換付き)"
162
+ end
163
+
164
+ describe HankakuFilterController, "SoftBank 910T からのアクセス" do
165
+ before do
166
+ @user_agent = "SoftBank/1.0/910T/TJ001/SN000000000000000 Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1"
167
+ @controller = "hankaku_filter"
168
+ end
169
+ it_should_behave_like "UTF-8で通信する端末との通信(半角変換付き)"
170
+ end
171
+ end
@@ -0,0 +1,187 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+ require 'nokogiri'
4
+
5
+ describe Jpmobile::Helpers do
6
+ # 文字列 +str+ 中に含まれるリンクについて、
7
+ # リンクテキスト、属性のHash、URLのqueryをのぞいた部分、ueryをHashにしたもの
8
+ # の3要素からなる配列の配列で返す。
9
+ def get_href_and_texts(str)
10
+ results = []
11
+ (Nokogiri::HTML.parse(str)/"a").each do |link|
12
+ path, query = link["href"].split(/\?/, 2)
13
+ params = query.nil? ? nil : Rack::Utils.parse_query(query)
14
+ results << [link.inner_html, link.attributes, path, params]
15
+ end
16
+ return results
17
+ end
18
+
19
+ it "get_position_link_to_がエラー無く終わるか" do
20
+ lambda {
21
+ get "/links/show_all", {}, {"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"}
22
+ }.should_not raise_error
23
+ end
24
+
25
+ context "docomo で" do
26
+ it "get_position_link_to が正常に表示されること" do
27
+ get "/links/link", {}, { "HTTP_USER_AGENT" => "DoCoMo/2.0 SH903i(c100;TB;W24H16)"}
28
+ links = get_href_and_texts(body)
29
+
30
+ links.size.should == 1
31
+ text, attrs, path, params = links.first
32
+ text.should == "STRING"
33
+ path.should == "http://www.example.com/links/link"
34
+ body.should =~ /lcs>/
35
+ end
36
+
37
+ it "docomo_utn_link_to が正しく出力されること" do
38
+ get "/links/docomo_utn", {}, {"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"}
39
+ links = get_href_and_texts(body)
40
+
41
+ links.size.should == 1
42
+ text, attrs, path, params = links.first
43
+ text.should == "STRING"
44
+ path.should == "http://www.example.com/links/docomo_utn"
45
+ body.should =~ /utn>/
46
+ end
47
+
48
+ it "オープンiエリア取得用のリンクが正しく出力されること" do
49
+ get "/links/docomo_openiarea", {}, {"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"}
50
+ links = get_href_and_texts(body)
51
+
52
+ links.size.should == 1
53
+ text, attrs, path, params = links.first
54
+ text.should == "STRING"
55
+ path.should == "http://w1m.docomo.ne.jp/cp/iarea"
56
+ params["ecode"].should == "OPENAREACODE"
57
+ params["msn"].should == "OPENAREAKEY"
58
+ params["posinfo"].should == "1"
59
+ params["nl"].should == "http://www.example.com/links/docomo_openiarea"
60
+ end
61
+
62
+ it "GPS取得用のリンクが正しく出力されること" do
63
+ get "/links/docomo_foma_gps", {}, {"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"}
64
+ links = get_href_and_texts(body)
65
+
66
+ links.size.should == 1
67
+ text, attrs, path, params = links.first
68
+ text.should == "STRING"
69
+ path.should == "http://www.example.com/links/docomo_foma_gps"
70
+ body.should =~ /lcs>/
71
+ end
72
+ end
73
+
74
+ context "au で" do
75
+ # get_position_link_to(自動判別), au, location only
76
+ def test_get_position_link_to_au_location_only
77
+ get "/links/link", {}, {"HTTP_USER_AGENT" => "KDDI-SN26 UP.Browser/6.2.0.6.2 (GUI) MMP/2.0"}
78
+ links = get_href_and_texts(body)
79
+ assert_equal(1, links.size)
80
+ text, attrs, path, params = links.first
81
+ assert_equal("STRING", text)
82
+ assert_equal("device:location", path)
83
+ assert_equal("http://www.example.com/links/link", params["url"])
84
+ end
85
+
86
+ # get_position_link_to(自動判別), au, gps
87
+ def test_get_position_link_to_au_gps
88
+ get "/links/link", {}, {"HTTP_USER_AGENT" => "KDDI-CA32 UP.Browser/6.2.0.7.3.129 (GUI) MMP/2.0"}
89
+ links = get_href_and_texts(body)
90
+ assert_equal(1, links.size)
91
+ text, attrs, path, params = links.first
92
+ assert_equal("STRING", text)
93
+ assert_equal("device:gpsone", path)
94
+ assert_equal("http://www.example.com/links/link", params["url"])
95
+ assert_equal("0", params["number"])
96
+ assert_equal("0", params["acry"])
97
+ assert_equal("1", params["ver"])
98
+ assert_equal("0", params["unit"])
99
+ assert_equal("0", params["datum"])
100
+ end
101
+
102
+ # au簡易位置情報取得用のリンクが正しく出力されるか。
103
+ def test_au_location_link_to
104
+ get "/links/au_location", {}, {"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"}
105
+ links = get_href_and_texts(body)
106
+ assert_equal(1, links.size)
107
+ text, attrs, path, params = links.first
108
+ assert_equal("STRING", text)
109
+ assert_equal("device:location", path)
110
+ assert_equal("http://www.example.com/links/au_location", params["url"])
111
+ end
112
+
113
+ # au GPS位置情報取得用のリンクが正しく出力されるか。
114
+ def test_au_gps_link_to
115
+ get "/links/au_gps", {}, {"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"}
116
+ links = get_href_and_texts(body)
117
+ assert_equal(1, links.size)
118
+ text, attrs, path, params = links.first
119
+ assert_equal("STRING", text)
120
+ assert_equal("device:gpsone", path)
121
+ assert_equal("http://www.example.com/links/au_gps", params["url"])
122
+ assert_equal("0", params["number"])
123
+ assert_equal("0", params["acry"])
124
+ assert_equal("1", params["ver"])
125
+ assert_equal("0", params["unit"])
126
+ assert_equal("0", params["datum"])
127
+ end
128
+ end
129
+
130
+ context "softbank で" do
131
+ # get_position_link_to(自動判別), Vodafone
132
+ def test_get_position_link_to_vodafone
133
+ get "/links/link", {}, {"HTTP_USER_AGENT" => "Vodafone/1.0/V903T/TJ001 Browser/VF-Browser/1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 Ext-J-Profile/JSCL-1.2.2 Ext-V-Profile/VSCL-2.0.0"}
134
+ links = get_href_and_texts(body)
135
+ assert_equal(1, links.size)
136
+ text, attrs, path, params = links.first
137
+ assert_equal("STRING", text)
138
+ assert_equal("location:auto", path)
139
+ end
140
+
141
+ # get_position_link_to(自動判別), Softbank
142
+ def test_get_position_link_to_softbank
143
+ get "/links/link", {}, {"HTTP_USER_AGENT" => "SoftBank/1.0/910T/TJ001/SN000000000000000 Browser/NetFront/3.3 Profile/MIDP-2.0 Configuration/CLDC-1.1"}
144
+ links = get_href_and_texts(body)
145
+ assert_equal(1, links.size)
146
+ text, attrs, path, params = links.first
147
+ assert_equal("STRING", text)
148
+ assert_equal("location:auto", path)
149
+ end
150
+
151
+ # Softbank 3G 位置情報取得用のリンクが正しく出力されるか。
152
+ def test_softbank_location_link_to
153
+ get "/links/softbank_location", {}, {"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"}
154
+ links = get_href_and_texts(body)
155
+ assert_equal(1, links.size)
156
+ text, attrs, path, params = links.first
157
+ assert_equal("STRING", text)
158
+ assert_equal("location:auto", path)
159
+ end
160
+ end
161
+
162
+ context "willcom で" do
163
+ # get_position_link_to(自動判別), Willcom
164
+ def test_get_position_link_to_willcom
165
+ get "/links/link", {}, {"HTTP_USER_AGENT" => "Mozilla/3.0(WILLCOM;KYOCERA/WX310K/2;1.2.2.16.000000/0.1/C100) Opera 7.0"}
166
+ links = get_href_and_texts(body)
167
+ assert_equal(1, links.size)
168
+ text, attrs, path, params = links.first
169
+ assert_equal("STRING", text)
170
+ assert_equal("http://location.request/dummy.cgi", path)
171
+ assert_equal("http://www.example.com/links/link", params["my"])
172
+ assert_equal("$location", params["pos"])
173
+ end
174
+
175
+ # Willcom 位置情報取得用のリンクが正しく出力されるか。
176
+ def test_willcom_location_link_to
177
+ get "/links/willcom_location", {}, {"HTTP_USER_AGENT" => "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)"}
178
+ links = get_href_and_texts(body)
179
+ assert_equal(1, links.size)
180
+ text, attrs, path, params = links.first
181
+ assert_equal("STRING", text)
182
+ assert_equal("http://location.request/dummy.cgi", path)
183
+ assert_equal("http://www.example.com/links/willcom_location", params["my"])
184
+ assert_equal("$location", params["pos"])
185
+ end
186
+ end
187
+ end