schmobile 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 802ea6e9f8a8f7704a536ad2c160f00b8bfd878a
4
+ data.tar.gz: 7df41ab6221edceac3207e95db03344ad9535d88
5
+ SHA512:
6
+ metadata.gz: 06d2ae455e5cd12a9a7c25a8a7ffd258c60c4aa8558839f2c57c0ca27d8e7c6c4b1b0e8bc56cfbbdae3e86ed3ffc308aefbe63a24d4866a5191dfa376211d7d9
7
+ data.tar.gz: 679ef4bb196954cbe593a5fe5c5cafc457c45422da3af56831007bdac54e62c920b82c6a86178892e1dd0b21dcdc6032e2bd94621c2e351efd4f82c40f106b2f
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  Gemfile.lock
2
+ gemfiles/*.gemfile.lock
2
3
  pkg/
3
4
  # rcov generated
@@ -1,7 +1,26 @@
1
- before_install:
2
- - gem update --system
1
+ sudo: false
2
+
3
+ cache: bundler
4
+
5
+ branch:
6
+ only: master
7
+
3
8
  rvm:
4
- - 1.8.7
5
- - 1.9.3
6
- - jruby
7
- - ree
9
+ - 2.1.8
10
+ - 2.2.4
11
+ - 2.3.0
12
+ - rbx-3.14
13
+ - jruby-9.0.5.0
14
+
15
+ gemfile:
16
+ - gemfiles/rack1.0.gemfile
17
+ - gemfiles/rack2.0.gemfile
18
+
19
+ matrix:
20
+ fast_finish: true
21
+ exclude:
22
+ - rvm: 2.1.8
23
+ gemfile: gemfiles/rack2.0.gemfile
24
+ allow_failures:
25
+ - rvm: jruby-9.0.5.0
26
+ gemfile: gemfiles/rack2.0.gemfile
data/Gemfile CHANGED
@@ -1,3 +1 @@
1
- source "http://rubygems.org"
2
-
3
- gemspec
1
+ eval_gemfile('gemfiles/rack1.0.gemfile')
data/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Schmobile [![Build Status](https://secure.travis-ci.org/morten/schmobile.png)](http://travis-ci.org/morten/schmobile)
1
+ # Schmobile [![Build Status](https://secure.travis-ci.org/zendesk/schmobile.png)](http://travis-ci.org/zendesk/schmobile)
2
2
 
3
3
  A mobile user agent detection Rack middleware. It provides `Rack::Request#is_mobile?`
4
4
 
@@ -50,6 +50,13 @@ Rack::Request#is_mobile?
50
50
  use Schmobile, :redirect_to => "/mobile", :if => Proc.new { |request| request.host =~ /staging/ }
51
51
  ```
52
52
 
53
- ## License
53
+ ## Copyright and license
54
54
 
55
- Released under the Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.html
55
+ Copyright 2013 Zendesk
56
+
57
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
58
+ You may obtain a copy of the License at
59
+
60
+ http://www.apache.org/licenses/LICENSE-2.0
61
+
62
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
data/Rakefile CHANGED
@@ -1,12 +1,9 @@
1
+ require 'bundler/setup'
1
2
  require 'bundler/gem_tasks'
2
- require 'rake/testtask'
3
+ require 'rspec/core/rake_task'
4
+ require 'wwtd/tasks'
5
+ require 'bump/tasks'
3
6
 
4
- Rake::TestTask.new do |test|
5
- test.libs << 'lib' << 'test'
6
- test.pattern = 'test/**/test_*.rb'
7
- test.verbose = true
8
- end
7
+ RSpec::Core::RakeTask.new(:spec)
9
8
 
10
- task :default do
11
- sh "bundle exec rake test"
12
- end
9
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: Bundler.root.sub('/gemfiles', '')
4
+
5
+ gem 'rack', '~> 1.6'
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec path: Bundler.root.sub('/gemfiles', '')
4
+
5
+ gem 'rack', '~> 2.0.0.alpha'
6
+ gem 'actionpack', '~> 5.0.0.beta2'
7
+ gem 'activesupport', '~> 5.0.0.beta2'
@@ -1,10 +1,16 @@
1
1
  require 'rack'
2
2
 
3
+ begin
4
+ require 'action_dispatch'
5
+ rescue LoadError
6
+ # It's fine, actionpack isn't present
7
+ end
8
+
9
+ require 'schmobile/version'
3
10
  require 'schmobile/middleware'
4
11
  require 'schmobile/request_extension'
5
12
  require 'schmobile/filters'
6
13
 
7
14
  module Schmobile
8
- VERSION = "0.5.0"
9
- IS_MOBILE = "is_mobile"
15
+ IS_MOBILE = 'is_mobile'
10
16
  end
@@ -3,7 +3,7 @@ module Schmobile
3
3
  module IsMobileParam
4
4
  def self.call(request)
5
5
  if request.params.key?(Schmobile::IS_MOBILE)
6
- request.session[Schmobile::IS_MOBILE] = (request.params[Schmobile::IS_MOBILE] == "true")
6
+ request.session[Schmobile::IS_MOBILE] = (request.params[Schmobile::IS_MOBILE] == 'true')
7
7
  end
8
8
 
9
9
  nil
@@ -10,7 +10,7 @@ module Schmobile
10
10
  request = Rack::Request.new(env)
11
11
 
12
12
  if request.is_mobile? && redirect?(request)
13
- [ 301, { "Location" => redirect_location(request) }, [] ]
13
+ [ 301, { 'Location' => redirect_location(request) }, [] ]
14
14
  else
15
15
  @app.call(env)
16
16
  end
@@ -31,7 +31,7 @@ module Schmobile
31
31
  redirecting = false
32
32
  end
33
33
 
34
- redirecting ? redirect_location(request) : nil
34
+ redirecting
35
35
  end
36
36
 
37
37
  def redirect_location(request)
@@ -14,11 +14,10 @@ module Schmobile
14
14
  end
15
15
 
16
16
  def is_device?(identifier)
17
- user_agent =~ /#{identifier}/i
17
+ !!(user_agent =~ /#{identifier}/i)
18
18
  end
19
19
  end
20
20
  end
21
21
 
22
- Rack::Request.class_eval do
23
- include Schmobile::Request
24
- end
22
+ Rack::Request.include Schmobile::Request
23
+ ActionDispatch::Request.include Schmobile::Request if defined?(ActionDispatch::Request)
@@ -40,11 +40,11 @@ module Schmobile
40
40
  end
41
41
 
42
42
  def self.mobile_agent_matcher
43
- @mobile_agent_matcher ||= Regexp.new(MOBILE_USER_AGENTS.uniq.compact.map { |v| Regexp.escape(v) }.join("|"))
43
+ @mobile_agent_matcher ||= Regexp.new(MOBILE_USER_AGENTS.uniq.compact.map { |v| Regexp.escape(v) }.join('|'))
44
44
  end
45
45
 
46
46
  def self.non_mobile_agent_matcher
47
- @non_mobile_agent_matcher ||= Regexp.new(NON_MOBILE_USER_AGENTS.uniq.compact.map { |v| Regexp.escape(v) }.join("|"))
47
+ @non_mobile_agent_matcher ||= Regexp.new(NON_MOBILE_USER_AGENTS.uniq.compact.map { |v| Regexp.escape(v) }.join('|'))
48
48
  end
49
49
 
50
50
  def self.matched_by?(user_agent)
@@ -0,0 +1,3 @@
1
+ module Schmobile
2
+ VERSION = '2.0.0'
3
+ end
@@ -1,24 +1,28 @@
1
- Gem::Specification.new "Schmobile", "1.0.0" do |s|
1
+ require './lib/schmobile/version'
2
+
3
+ Gem::Specification.new do |s|
2
4
  s.name = 'schmobile'
5
+ s.version = Schmobile::VERSION
3
6
  s.date = '2012-06-14'
4
7
  s.files = `git ls-files`.split("\n")
5
- s.license = "Apache License Version 2.0"
8
+ s.license = 'Apache License Version 2.0'
6
9
 
7
- s.summary = "A mobile user agent detection Rack middleware."
8
- s.description = "A mobile user agent detection Rack middleware. See the README."
10
+ s.summary = 'A mobile user agent detection Rack middleware.'
11
+ s.description = 'A mobile user agent detection Rack middleware. See the README.'
9
12
 
10
- s.authors = ["Morten Primdahl"]
11
- s.email = 'primdahl@me.com'
13
+ s.authors = ['Morten Primdahl', 'Pierre Schambacher']
14
+ s.email = %w[primdahl@me.com pschambacher@zendesk.com]
12
15
  s.homepage = 'http://github.com/zendesk/schmobile'
13
16
 
14
17
  s.require_paths = %w[lib]
15
18
 
16
- s.add_runtime_dependency("rack")
19
+ s.add_runtime_dependency('rack')
17
20
 
18
21
  s.add_development_dependency('rake')
19
22
  s.add_development_dependency('bundler')
20
- s.add_development_dependency('shoulda')
21
- s.add_development_dependency('mocha')
23
+ s.add_development_dependency('wwtd')
24
+ s.add_development_dependency('bump')
25
+ s.add_development_dependency('rspec')
22
26
 
23
27
  s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
24
28
  end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ if defined?(ActionDispatch::Request)
4
+ describe ActionDispatch::Request do
5
+ it 'respond to the schmobile helpers' do
6
+ expect(action_dispatch_request('HTTP_USER_AGENT' => ipad) .is_device?('ipad')) .to be true
7
+ expect(action_dispatch_request('HTTP_USER_AGENT' => ipad) .is_device?('blackberry')).to be false
8
+ expect(action_dispatch_request('HTTP_USER_AGENT' => blackberry).is_device?('blackberry')).to be true
9
+ expect(action_dispatch_request('HTTP_USER_AGENT' => iphone) .is_device?('blackberry')).to be false
10
+ expect(action_dispatch_request('HTTP_USER_AGENT' => iphone) .is_device?('iphone')) .to be true
11
+ expect(action_dispatch_request('HTTP_USER_AGENT' => msie6) .is_device?('iphone')) .to be false
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Schmobile::Filters do
4
+ let(:session) { {} }
5
+ let(:params) { {} }
6
+ let(:request) { double('Request', user_agent: msie8, params: params, session: session) }
7
+
8
+ describe '#apply' do
9
+ describe 'on a pristine request' do
10
+ it 'returns true if the session is already mobile' do
11
+ expect(Schmobile::UserAgents).to_not receive(:is_mobile_agent?)
12
+ session[Schmobile::IS_MOBILE] = true
13
+ expect(Schmobile::Filters.apply(request)).to be true
14
+ end
15
+
16
+ it "returns true if there's an is_mobile parameter with value true" do
17
+ expect(Schmobile::UserAgents).to_not receive(:is_mobile_agent?)
18
+ params.merge!('is_mobile' => 'true')
19
+ expect(Schmobile::Filters.apply(request)).to be true
20
+ end
21
+
22
+ it "returns false if there's an is_mobile parameter with value false" do
23
+ expect(Schmobile::UserAgents).to_not receive(:is_mobile_agent?)
24
+ params.merge!('is_mobile' => 'false')
25
+ expect(Schmobile::Filters.apply(request)).to be false
26
+ end
27
+
28
+ it "defers to user agent check if there's no session or parameter" do
29
+ expect(Schmobile::UserAgents).to receive(:is_mobile_agent?).once
30
+ expect(Schmobile::Filters.apply(request)).to be false
31
+ end
32
+ end
33
+
34
+ describe 'on a request with mobile session set to false' do
35
+ it 'allows to change the session value with a parameter' do
36
+ expect(Schmobile::UserAgents).to_not receive(:is_mobile_agent?)
37
+ session[Schmobile::IS_MOBILE] = false
38
+ expect(Schmobile::Filters.apply(request)).to be false
39
+ params.merge!('is_mobile' => 'true')
40
+ expect(Schmobile::Filters.apply(request)).to be true
41
+ expect(request.session[Schmobile::IS_MOBILE]).to be true
42
+ end
43
+ end
44
+
45
+ describe 'on a request with mobile session set to true' do
46
+ it 'allows to change the session value with a parameter' do
47
+ expect(Schmobile::UserAgents).to_not receive(:is_mobile_agent?)
48
+ session[Schmobile::IS_MOBILE] = true
49
+ expect(Schmobile::Filters.apply(request)).to be true
50
+ params.merge!('is_mobile' => 'false')
51
+ expect(Schmobile::Filters.apply(request)).to be false
52
+ expect(request.session[Schmobile::IS_MOBILE]).to be false
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Schmobile::Middleware do
4
+ before do
5
+ @app = Class.new { def call(app); true; end }.new
6
+ @rack = Schmobile::Middleware.new(@app)
7
+ end
8
+
9
+ it 'returns an HTTP permanent redirect when given a redirect path and used by a mobile client' do
10
+ @rack = Schmobile::Middleware.new(@app, redirect_to: '/hello')
11
+ expect_any_instance_of(Rack::Request).to receive(:is_mobile?).and_return(true)
12
+
13
+ expect(@rack.call(environment)).to eq(
14
+ [
15
+ 301,
16
+ { 'Location'=>'/hello' },
17
+ []
18
+ ]
19
+ )
20
+ end
21
+
22
+ describe 'with no redirect_to' do
23
+ it 'does not redirect mobile traffic' do
24
+ expect_any_instance_of(Rack::Request).to receive(:params).and_return({})
25
+ expect(@app).to receive(:call)
26
+ @rack.call(environment('HTTP_USER_AGENT' => iphone))
27
+ end
28
+
29
+ it 'queries the request object to determine if this is a mobile request' do
30
+ expect_any_instance_of(Rack::Request).to receive(:is_mobile?).and_return(true)
31
+ @rack.call(environment('HTTP_USER_AGENT' => iphone))
32
+ end
33
+ end
34
+
35
+ describe 'with redirect_to' do
36
+ before do
37
+ @rack = Schmobile::Middleware.new(@app, redirect_to: '/wonderland')
38
+ end
39
+
40
+ describe '#redirect?' do
41
+ it 'returns true when not on mobile path' do
42
+ expect(@rack.redirect?(request('PATH_INFO' => '/somewhere'))).to be true
43
+ end
44
+
45
+ it 'returns true on base path' do
46
+ expect(@rack.redirect?(request('PATH_INFO' => '/'))).to be true
47
+ end
48
+
49
+ it 'returns false when already on path' do
50
+ expect(@rack.redirect?(request('PATH_INFO' => '/wonderland'))).to be false
51
+ expect(@rack.redirect?(request('PATH_INFO' => '/wonderland/more/stuff'))).to be false
52
+ end
53
+
54
+ it 'returns false when :if resolves to false' do
55
+ @rack = Schmobile::Middleware.new(@app, redirect_to: '/wonderland', redirect_if: Proc.new { |request| false })
56
+ expect(@rack.redirect?(request('PATH_INFO' => '/somewhere'))).to be false
57
+ end
58
+ end
59
+
60
+ describe '#redirect' do
61
+ it 'interpolates the argument string xxxx' do
62
+ @rack = Schmobile::Middleware.new(@app, redirect_to: '/wonderland', redirect_with: '/{{path}}')
63
+ expect(@rack.redirect_location(request('PATH_INFO' => 'wiffle'))).to eq('/wonderland/wiffle')
64
+ end
65
+
66
+ it 'interpolates a multipart argument string' do
67
+ @rack = Schmobile::Middleware.new(@app, redirect_to: '/wonderland/', redirect_with: '{{path}}/lemurs/{{path}}')
68
+ expect(@rack.redirect_location(request('PATH_INFO' => 'wiffle'))).to eq('/wonderland/wiffle/lemurs/wiffle')
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe Schmobile::Request do
4
+ describe '#is_device?' do
5
+ it 'matchs devices as part of the user agent case insensitively' do
6
+ expect(request('HTTP_USER_AGENT' => ipad) .is_device?('ipad')) .to be true
7
+ expect(request('HTTP_USER_AGENT' => ipad) .is_device?('blackberry')).to be false
8
+ expect(request('HTTP_USER_AGENT' => blackberry).is_device?('blackberry')).to be true
9
+ expect(request('HTTP_USER_AGENT' => iphone) .is_device?('blackberry')).to be false
10
+ expect(request('HTTP_USER_AGENT' => iphone) .is_device?('iphone')) .to be true
11
+ expect(request('HTTP_USER_AGENT' => msie6) .is_device?('iphone')) .to be false
12
+ end
13
+ end
14
+
15
+ describe '#is_mobile?' do
16
+ it 'only calls the filter chain once' do
17
+ expect(Schmobile::Filters).to receive(:apply).once.and_return(false)
18
+ one_request = request
19
+ 3.times { one_request.is_mobile? }
20
+ end
21
+
22
+ it 're-calls the filter chain once reset' do
23
+ expect(Schmobile::Filters).to receive(:apply).twice.and_return(false)
24
+ one_request = request
25
+ 3.times { one_request.is_mobile? }
26
+ one_request.toggle_mobile_session!
27
+ 3.times { one_request.is_mobile? }
28
+ end
29
+
30
+ describe 'without params' do
31
+ before do
32
+ allow_any_instance_of(Rack::Request).to receive(:params).and_return({})
33
+ end
34
+
35
+ it 'does not detect a regular browser as mobile' do
36
+ expect(request.is_mobile?).to be false
37
+ expect(request('HTTP_USER_AGENT' => nil).is_mobile?).to be false
38
+
39
+ [ :msie6, :msie8, :opera, :chrome ].each do |browser|
40
+ agent = self.send(browser)
41
+ expect(request('HTTP_USER_AGENT' => agent).is_mobile?).to be false
42
+ end
43
+ end
44
+
45
+ it 'detects mobile units as mobile' do
46
+ [ :ipod, :iphone, :android, :blackberry, :samsung, :palm, :htc_touch, :danger ].each do |phone|
47
+ agent = self.send(phone)
48
+ expect(request('HTTP_USER_AGENT' => agent).is_mobile?).to be true
49
+ end
50
+ end
51
+
52
+ it 'not detects an ipad as a mobile browser' do
53
+ expect(request('HTTP_USER_AGENT' => ipad).is_mobile?).to be false
54
+ end
55
+
56
+ it 'returns false when forced in the session' do
57
+ allow_any_instance_of(Rack::Request).to receive(:session).and_return({ Schmobile::IS_MOBILE => false })
58
+ expect(request('HTTP_USER_AGENT' => iphone).is_mobile?).to be false
59
+ end
60
+
61
+ it 'returns true when forced in the session' do
62
+ allow_any_instance_of(Rack::Request).to receive(:session).and_return({ Schmobile::IS_MOBILE => true })
63
+ expect(request.is_mobile?).to be true
64
+ end
65
+ end
66
+
67
+ describe 'with params' do
68
+ it 'returns false when forced via a request parameter' do
69
+ allow_any_instance_of(Rack::Request).to receive(:params).and_return({ Schmobile::IS_MOBILE => 'false' })
70
+ expect(request('HTTP_USER_AGENT' => iphone).is_mobile?).to be false
71
+ end
72
+
73
+ it 'returns true when forced via a request parameter' do
74
+ allow_any_instance_of(Rack::Request).to receive(:params).and_return({ Schmobile::IS_MOBILE => 'true' })
75
+ expect(request.is_mobile?).to be true
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,6 @@
1
+ require 'schmobile'
2
+ require 'support/request_helper'
3
+
4
+ RSpec.configure do |c|
5
+ c.include RequestHelper
6
+ end
@@ -1,14 +1,4 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'shoulda'
4
- require 'mocha'
5
-
6
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
- $LOAD_PATH.unshift(File.dirname(__FILE__))
8
-
9
- require 'schmobile'
10
-
11
- class Test::Unit::TestCase
1
+ module RequestHelper
12
2
 
13
3
  def ipod
14
4
  'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20'
@@ -27,11 +17,11 @@ class Test::Unit::TestCase
27
17
  end
28
18
 
29
19
  def ipad
30
- "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10"
20
+ 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10'
31
21
  end
32
22
 
33
23
  def palm
34
- "Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0"
24
+ 'Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0'
35
25
  end
36
26
 
37
27
  def samsung
@@ -39,33 +29,37 @@ class Test::Unit::TestCase
39
29
  end
40
30
 
41
31
  def htc_touch
42
- "HTC_Touch_Diamond2_T5353 Opera/9.50 (Windows NT 5.1; U; en)"
32
+ 'HTC_Touch_Diamond2_T5353 Opera/9.50 (Windows NT 5.1; U; en)'
43
33
  end
44
34
 
45
35
  def danger
46
- "Mozilla/5.0 (Danger hiptop 5.0; U; rv:1.7.12) Gecko/20050920"
36
+ 'Mozilla/5.0 (Danger hiptop 5.0; U; rv:1.7.12) Gecko/20050920'
47
37
  end
48
38
 
49
39
  def msie6
50
- "Mozilla/4.0 (compatible; MSIE 6.0; Update a; AOL 6.0; Windows 98)"
40
+ 'Mozilla/4.0 (compatible; MSIE 6.0; Update a; AOL 6.0; Windows 98)'
51
41
  end
52
42
 
53
43
  def msie8
54
- "Client: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; MDDR; .NET CLR 3.5.30729; InfoPath.2; .NET CLR 3.0.30729; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 4.0.20506; Creative AutoUpdate v1.40.01)"
44
+ 'Client: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; MDDR; .NET CLR 3.5.30729; InfoPath.2; .NET CLR 3.0.30729; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 4.0.20506; Creative AutoUpdate v1.40.01)'
55
45
  end
56
46
 
57
47
  def chrome
58
- "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71 Safari/534.24"
48
+ 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71 Safari/534.24'
59
49
  end
60
50
 
61
51
  def opera
62
- "Opera/9.25 (Windows NT 6.0; U; en)"
52
+ 'Opera/9.25 (Windows NT 6.0; U; en)'
63
53
  end
64
54
 
65
55
  def request(overwrite = {})
66
56
  Rack::Request.new(environment(overwrite))
67
57
  end
68
58
 
59
+ def action_dispatch_request(overwrite = {})
60
+ ActionDispatch::Request.new(environment(overwrite))
61
+ end
62
+
69
63
  def environment(overwrite = {})
70
64
  {
71
65
  'GATEWAY_INTERFACE'=> 'CGI/1.2',
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Schmobile::UserAgents do
4
+ describe '#remove_user_agent_pattern' do
5
+ it 'allows removal of a user agent' do
6
+ expect(Schmobile::UserAgents.is_mobile_agent?('novarra')).to be true
7
+
8
+ Schmobile::UserAgents.remove_user_agent_pattern('novarra')
9
+
10
+ expect(Schmobile::UserAgents.is_mobile_agent?('novarra')).to be false
11
+ end
12
+ end
13
+
14
+ describe '#add_user_agent_pattern' do
15
+ it 'allows adding a user agent' do
16
+ expect(Schmobile::UserAgents.is_mobile_agent?('wibble')).to be false
17
+
18
+ Schmobile::UserAgents.add_user_agent_pattern('wibble')
19
+
20
+ expect(Schmobile::UserAgents.is_mobile_agent?('wibble')).to be true
21
+ end
22
+
23
+ it 'allows changing behavior for a non-mobile agent' do
24
+ expect(Schmobile::UserAgents.is_mobile_agent?('ipad')).to be false
25
+
26
+ Schmobile::UserAgents.add_user_agent_pattern('ipad')
27
+
28
+ expect(Schmobile::UserAgents.is_mobile_agent?('ipad')).to be false
29
+
30
+ Schmobile::UserAgents.remove_non_mobile_user_agent_pattern('ipad')
31
+
32
+ expect(Schmobile::UserAgents.is_mobile_agent?('ipad')).to be true
33
+
34
+ Schmobile::UserAgents.add_non_mobile_user_agent_pattern('ipad')
35
+ end
36
+ end
37
+
38
+ describe '#is_mobile_agent?' do
39
+ it 'returns false for common browsers' do
40
+ [ :msie6, :msie8, :opera, :chrome ].each do |browser|
41
+ agent = self.send(browser)
42
+ expect(Schmobile::UserAgents.is_mobile_agent?(agent)). to be(false), "#{browser} should not detect as mobile, #{agent}"
43
+ end
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schmobile
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 2.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Morten Primdahl
8
+ - Pierre Schambacher
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
@@ -14,95 +14,104 @@ dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
16
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
17
  requirements:
19
- - - ! '>='
18
+ - - ">="
20
19
  - !ruby/object:Gem::Version
21
20
  version: '0'
22
21
  type: :runtime
23
22
  prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
- - - ! '>='
25
+ - - ">="
28
26
  - !ruby/object:Gem::Version
29
27
  version: '0'
30
28
  - !ruby/object:Gem::Dependency
31
29
  name: rake
32
30
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
31
  requirements:
35
- - - ! '>='
32
+ - - ">="
36
33
  - !ruby/object:Gem::Version
37
34
  version: '0'
38
35
  type: :development
39
36
  prerelease: false
40
37
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
38
  requirements:
43
- - - ! '>='
39
+ - - ">="
44
40
  - !ruby/object:Gem::Version
45
41
  version: '0'
46
42
  - !ruby/object:Gem::Dependency
47
43
  name: bundler
48
44
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
45
  requirements:
51
- - - ! '>='
46
+ - - ">="
52
47
  - !ruby/object:Gem::Version
53
48
  version: '0'
54
49
  type: :development
55
50
  prerelease: false
56
51
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
52
  requirements:
59
- - - ! '>='
53
+ - - ">="
60
54
  - !ruby/object:Gem::Version
61
55
  version: '0'
62
56
  - !ruby/object:Gem::Dependency
63
- name: shoulda
57
+ name: wwtd
64
58
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
59
  requirements:
67
- - - ! '>='
60
+ - - ">="
68
61
  - !ruby/object:Gem::Version
69
62
  version: '0'
70
63
  type: :development
71
64
  prerelease: false
72
65
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
66
  requirements:
75
- - - ! '>='
67
+ - - ">="
76
68
  - !ruby/object:Gem::Version
77
69
  version: '0'
78
70
  - !ruby/object:Gem::Dependency
79
- name: mocha
71
+ name: bump
80
72
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
73
  requirements:
83
- - - ! '>='
74
+ - - ">="
84
75
  - !ruby/object:Gem::Version
85
76
  version: '0'
86
77
  type: :development
87
78
  prerelease: false
88
79
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
80
  requirements:
91
- - - ! '>='
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
92
96
  - !ruby/object:Gem::Version
93
97
  version: '0'
94
98
  description: A mobile user agent detection Rack middleware. See the README.
95
- email: primdahl@me.com
99
+ email:
100
+ - primdahl@me.com
101
+ - pschambacher@zendesk.com
96
102
  executables: []
97
103
  extensions: []
98
104
  extra_rdoc_files: []
99
105
  files:
100
- - .document
101
- - .gitignore
102
- - .travis.yml
106
+ - ".document"
107
+ - ".gitignore"
108
+ - ".travis.yml"
103
109
  - Gemfile
110
+ - LICENSE
104
111
  - README.md
105
112
  - Rakefile
113
+ - gemfiles/rack1.0.gemfile
114
+ - gemfiles/rack2.0.gemfile
106
115
  - lib/schmobile.rb
107
116
  - lib/schmobile/filters.rb
108
117
  - lib/schmobile/filters/is_mobile_param.rb
@@ -111,39 +120,37 @@ files:
111
120
  - lib/schmobile/middleware.rb
112
121
  - lib/schmobile/request_extension.rb
113
122
  - lib/schmobile/user_agents.rb
123
+ - lib/schmobile/version.rb
114
124
  - schmobile.gemspec
115
- - test/helper.rb
116
- - test/test_filters.rb
117
- - test/test_middleware.rb
118
- - test/test_rack_request.rb
119
- - test/test_user_agents.rb
125
+ - spec/action_dispatch_request_spec.rb
126
+ - spec/filters_spec.rb
127
+ - spec/middleware_spec.rb
128
+ - spec/rack_request_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/support/request_helper.rb
131
+ - spec/user_agents_spec.rb
120
132
  homepage: http://github.com/zendesk/schmobile
121
133
  licenses:
122
134
  - Apache License Version 2.0
135
+ metadata: {}
123
136
  post_install_message:
124
137
  rdoc_options: []
125
138
  require_paths:
126
139
  - lib
127
140
  required_ruby_version: !ruby/object:Gem::Requirement
128
- none: false
129
141
  requirements:
130
- - - ! '>='
142
+ - - ">="
131
143
  - !ruby/object:Gem::Version
132
144
  version: '0'
133
145
  required_rubygems_version: !ruby/object:Gem::Requirement
134
- none: false
135
146
  requirements:
136
- - - ! '>='
147
+ - - ">="
137
148
  - !ruby/object:Gem::Version
138
149
  version: '0'
139
150
  requirements: []
140
151
  rubyforge_project:
141
- rubygems_version: 1.8.23
152
+ rubygems_version: 2.4.5.1
142
153
  signing_key:
143
- specification_version: 3
154
+ specification_version: 4
144
155
  summary: A mobile user agent detection Rack middleware.
145
- test_files:
146
- - test/test_filters.rb
147
- - test/test_middleware.rb
148
- - test/test_rack_request.rb
149
- - test/test_user_agents.rb
156
+ test_files: []
@@ -1,62 +0,0 @@
1
- require 'helper'
2
-
3
- class TestFukters < Test::Unit::TestCase
4
-
5
- context "Schmobile::Filters" do
6
- context "#apply" do
7
- setup do
8
- @request = stub(:user_agent => msie8)
9
- @request.stubs(:params).returns({})
10
- @session = {}
11
- @request.stubs(:session).returns(@session)
12
- end
13
-
14
- context "on a pristine request" do
15
- should "return true if the session is already mobile" do
16
- Schmobile::UserAgents.expects(:is_mobile_agent?).never
17
- @session[Schmobile::IS_MOBILE] = true
18
- assert Schmobile::Filters.apply(@request)
19
- end
20
-
21
- should "return true if there's an is_mobile parameter with value true" do
22
- Schmobile::UserAgents.expects(:is_mobile_agent?).never
23
- @request.stubs(:params).returns({ "is_mobile" => "true" })
24
- assert Schmobile::Filters.apply(@request)
25
- end
26
-
27
- should "return false if there's an is_mobile parameter with value false" do
28
- Schmobile::UserAgents.expects(:is_mobile_agent?).never
29
- @request.stubs(:params).returns({ "is_mobile" => "false" })
30
- assert !Schmobile::Filters.apply(@request)
31
- end
32
-
33
- should "defer to user agent check if there's no session or parameter" do
34
- Schmobile::UserAgents.expects(:is_mobile_agent?).once
35
- assert !Schmobile::Filters.apply(@request)
36
- end
37
- end
38
-
39
- context "on a request with mobile session set to false" do
40
- should "allow to change the session value with a parameter" do
41
- Schmobile::UserAgents.expects(:is_mobile_agent?).never
42
- @request.session[Schmobile::IS_MOBILE] = false
43
- assert !Schmobile::Filters.apply(@request)
44
- @request.stubs(:params).returns({ "is_mobile" => "true" })
45
- assert Schmobile::Filters.apply(@request)
46
- assert @request.session[Schmobile::IS_MOBILE]
47
- end
48
- end
49
-
50
- context "on a request with mobile session set to true" do
51
- should "allow to change the session value with a parameter" do
52
- Schmobile::UserAgents.expects(:is_mobile_agent?).never
53
- @request.session[Schmobile::IS_MOBILE] = true
54
- assert Schmobile::Filters.apply(@request)
55
- @request.stubs(:params).returns({ "is_mobile" => "false" })
56
- assert !Schmobile::Filters.apply(@request)
57
- assert !@request.session[Schmobile::IS_MOBILE]
58
- end
59
- end
60
- end
61
- end
62
- end
@@ -1,69 +0,0 @@
1
- require 'helper'
2
-
3
- class TestMiddleware < Test::Unit::TestCase
4
-
5
- context "Schmobile::Middleware" do
6
- setup do
7
- @app = Class.new { def call(app); true; end }.new
8
- @rack = Schmobile::Middleware.new(@app)
9
- end
10
-
11
- should "return an HTTP permanent redirect when given a redirect path and used by a mobile client" do
12
- @rack = Schmobile::Middleware.new(@app, :redirect_to => "/hello")
13
- Rack::Request.any_instance.expects(:is_mobile?).returns(true)
14
-
15
- assert_equal [301, { "Location"=>"/hello" }, []], @rack.call(environment)
16
- end
17
-
18
- context "with no redirect_to" do
19
- should "not redirect mobile traffic" do
20
- Rack::Request.any_instance.expects(:params).returns({})
21
- @app.expects(:call)
22
- @rack.call(environment("HTTP_USER_AGENT" => iphone))
23
- end
24
-
25
- should "query the request object to determine if this is a mobile request" do
26
- Rack::Request.any_instance.expects(:is_mobile?).returns(true)
27
- @rack.call(environment("HTTP_USER_AGENT" => iphone))
28
- end
29
- end
30
-
31
- context "with redirect_to" do
32
- setup do
33
- @rack = Schmobile::Middleware.new(@app, :redirect_to => "/wonderland")
34
- end
35
-
36
- context "#redirect?" do
37
- should "return true when not on mobile path" do
38
- assert @rack.redirect?(request("PATH_INFO" => "/somewhere"))
39
- end
40
-
41
- should "return true on base path" do
42
- assert @rack.redirect?(request("PATH_INFO" => "/"))
43
- end
44
-
45
- should "return false when already on path" do
46
- assert !@rack.redirect?(request("PATH_INFO" => "/wonderland"))
47
- assert !@rack.redirect?(request("PATH_INFO" => "/wonderland/more/stuff"))
48
- end
49
-
50
- should "return false when :if resolves to false" do
51
- @rack = Schmobile::Middleware.new(@app, :redirect_to => "/wonderland", :redirect_if => Proc.new { |request| false })
52
- assert !@rack.redirect?(request("PATH_INFO" => "/somewhere"))
53
- end
54
- end
55
-
56
- context "#redirect" do
57
- should "interpolate the argument string xxxx" do
58
- @rack = Schmobile::Middleware.new(@app, :redirect_to => "/wonderland", :redirect_with => "/{{path}}")
59
- assert_equal "/wonderland/wiffle", @rack.redirect_location(request("PATH_INFO" => "wiffle"))
60
- end
61
-
62
- should "interpolate a multipart argument string" do
63
- @rack = Schmobile::Middleware.new(@app, :redirect_to => "/wonderland/", :redirect_with => "{{path}}/lemurs/{{path}}")
64
- assert_equal "/wonderland/wiffle/lemurs/wiffle", @rack.redirect_location(request("PATH_INFO" => "wiffle"))
65
- end
66
- end
67
- end
68
- end
69
- end
@@ -1,82 +0,0 @@
1
- require 'helper'
2
-
3
- class TestRackRequest < Test::Unit::TestCase
4
-
5
- context "Rack::Request" do
6
- context "#is_device?" do
7
- should "match devices as part of the user agent case insensitively" do
8
- assert request("HTTP_USER_AGENT" => ipad).is_device?("ipad")
9
- assert !request("HTTP_USER_AGENT" => ipad).is_device?("blackberry")
10
- assert request("HTTP_USER_AGENT" => blackberry).is_device?("blackberry")
11
- assert !request("HTTP_USER_AGENT" => iphone).is_device?("blackberry")
12
- assert request("HTTP_USER_AGENT" => iphone).is_device?("iphone")
13
- assert !request("HTTP_USER_AGENT" => msie6).is_device?("iphone")
14
- end
15
- end
16
-
17
- context "#is_mobile?" do
18
- should "only call the filter chain once" do
19
- Schmobile::Filters.expects(:apply).once.returns(false)
20
- one_request = request
21
- 3.times { one_request.is_mobile? }
22
- end
23
-
24
- should "re-call the filter chain once reset" do
25
- Schmobile::Filters.expects(:apply).twice.returns(false)
26
- one_request = request
27
- 3.times { one_request.is_mobile? }
28
- one_request.toggle_mobile_session!
29
- 3.times { one_request.is_mobile? }
30
- end
31
-
32
- context "without params" do
33
- setup do
34
- Rack::Request.any_instance.stubs(:params).returns({})
35
- end
36
-
37
- should "not detect a regular browser as mobile" do
38
- assert !request.is_mobile?
39
- assert !request("HTTP_USER_AGENT" => nil).is_mobile?
40
-
41
- [ :msie6, :msie8, :opera, :chrome ].each do |browser|
42
- agent = self.send(browser)
43
- assert !request("HTTP_USER_AGENT" => agent).is_mobile?
44
- end
45
- end
46
-
47
- should "detect mobile units as mobile" do
48
- [ :ipod, :iphone, :android, :blackberry, :samsung, :palm, :htc_touch, :danger ].each do |phone|
49
- agent = self.send(phone)
50
- assert request("HTTP_USER_AGENT" => agent).is_mobile?
51
- end
52
- end
53
-
54
- should "not detect an ipad as a mobile browser" do
55
- assert !request("HTTP_USER_AGENT" => ipad).is_mobile?
56
- end
57
-
58
- should "return false when forced in the session" do
59
- Rack::Request.any_instance.stubs(:session).returns({ Schmobile::IS_MOBILE => false })
60
- assert !request("HTTP_USER_AGENT" => iphone).is_mobile?
61
- end
62
-
63
- should "return true when forced in the session" do
64
- Rack::Request.any_instance.stubs(:session).returns({ Schmobile::IS_MOBILE => true })
65
- assert request.is_mobile?
66
- end
67
- end
68
-
69
- context "with params" do
70
- should "return false when forced via a request parameter" do
71
- Rack::Request.any_instance.stubs(:params).returns({ Schmobile::IS_MOBILE => "false" })
72
- assert !request("HTTP_USER_AGENT" => iphone).is_mobile?
73
- end
74
-
75
- should "return true when forced via a request parameter" do
76
- Rack::Request.any_instance.stubs(:params).returns({ Schmobile::IS_MOBILE => "true" })
77
- assert request.is_mobile?
78
- end
79
- end
80
- end
81
- end
82
- end
@@ -1,41 +0,0 @@
1
- require 'helper'
2
-
3
- class TestUserAgents < Test::Unit::TestCase
4
-
5
- context "Schmobile::UserAgents" do
6
- context "#remove_user_agent_pattern" do
7
- should "allow removal of a user agent" do
8
- assert Schmobile::UserAgents.is_mobile_agent?("novarra")
9
- Schmobile::UserAgents.remove_user_agent_pattern("novarra")
10
- assert !Schmobile::UserAgents.is_mobile_agent?("novarra")
11
- end
12
- end
13
-
14
- context "#add_user_agent_pattern" do
15
- should "allow adding a user agent" do
16
- assert !Schmobile::UserAgents.is_mobile_agent?("wibble")
17
- Schmobile::UserAgents.add_user_agent_pattern("wibble")
18
- assert Schmobile::UserAgents.is_mobile_agent?("wibble")
19
- end
20
-
21
- should "allow changing behavior for a non-mobile agent" do
22
- assert !Schmobile::UserAgents.is_mobile_agent?("ipad")
23
- Schmobile::UserAgents.add_user_agent_pattern("ipad")
24
- assert !Schmobile::UserAgents.is_mobile_agent?("ipad")
25
- Schmobile::UserAgents.remove_non_mobile_user_agent_pattern("ipad")
26
- assert Schmobile::UserAgents.is_mobile_agent?("ipad")
27
- Schmobile::UserAgents.add_non_mobile_user_agent_pattern("ipad")
28
- end
29
- end
30
-
31
- context "#is_mobile_agent?" do
32
- should "return false for common browsers" do
33
- [ :msie6, :msie8, :opera, :chrome ].each do |browser|
34
- agent = self.send(browser)
35
- assert !Schmobile::UserAgents.is_mobile_agent?(agent), "#{browser} should not detect as mobile, #{agent}"
36
- end
37
- end
38
- end
39
- end
40
-
41
- end