obscenity 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -1
- data/lib/obscenity.rb +5 -1
- data/lib/obscenity/base.rb +13 -12
- data/lib/obscenity/rspec_matcher.rb +5 -0
- data/lib/obscenity/version.rb +1 -1
- data/obscenity.gemspec +3 -2
- data/test/test_base.rb +32 -27
- data/test/test_obscenity.rb +10 -9
- data/test/test_rack.rb +22 -14
- data/test/test_version.rb +1 -1
- metadata +4 -3
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Obscenity [![Build Status](
|
1
|
+
# Obscenity [![Build Status](https://secure.travis-ci.org/tjackiw/obscenity.png)](http://travis-ci.org/tjackiw/obscenity)
|
2
2
|
|
3
3
|
Obscenity is a profanity filter gem for Ruby/Rubinius, Rails (through ActiveModel), and Rack middleware.
|
4
4
|
|
@@ -52,6 +52,7 @@ The following methods are available to use with Obscenity:
|
|
52
52
|
- :garbled : Replaces profane words with $@!#%
|
53
53
|
- :stars : Replaces profane words with '*' up to the word's length
|
54
54
|
- :vowels : Replaces the vowels in the profane word with '*'
|
55
|
+
- :nonconsonants : Replaces non consonants with '*'
|
55
56
|
- "custom string" : Replaces the profane word with the custom string
|
56
57
|
|
57
58
|
Example:
|
@@ -101,6 +102,9 @@ Obscenity.replacement(:stars).sanitize("text with shit")
|
|
101
102
|
Obscenity.replacement(:vowels).sanitize("text with shit")
|
102
103
|
=> "text with sh*t"
|
103
104
|
|
105
|
+
Obscenity.replacement(:nonconsonants).sanitize('Oh 5hit')
|
106
|
+
=> "Oh *h*t"
|
107
|
+
|
104
108
|
Obscenity.replacement("[censored]").sanitize("text with shit")
|
105
109
|
=> "text with [censored]"
|
106
110
|
```
|
@@ -259,6 +263,17 @@ use Rack::Obscenity, sanitize: { params: [:foo, :bar], replacement: "[censored]"
|
|
259
263
|
# Sanitizes all params and replaces their values using :garbled
|
260
264
|
use Rack::Obscenity, sanitize: { replacement: :garbled }
|
261
265
|
```
|
266
|
+
### Test Helpers
|
267
|
+
|
268
|
+
Obscenity currently provides test helpers for RSpec only, but we have plans to add helpers to Shoulda as well.
|
269
|
+
|
270
|
+
#### RSpec Matcher
|
271
|
+
|
272
|
+
A `be_profane` matcher is available for RSpec. Its usage is very simple:
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
user.username.should_not be_profane
|
276
|
+
```
|
262
277
|
|
263
278
|
## Contributing to obscenity
|
264
279
|
|
data/lib/obscenity.rb
CHANGED
@@ -3,6 +3,10 @@ require 'obscenity/config'
|
|
3
3
|
require 'obscenity/base'
|
4
4
|
require 'obscenity/version'
|
5
5
|
|
6
|
+
if defined?(::RSpec)
|
7
|
+
require 'obscenity/rspec_matcher'
|
8
|
+
end
|
9
|
+
|
6
10
|
module Obscenity extend self
|
7
11
|
|
8
12
|
attr_accessor :config
|
@@ -33,4 +37,4 @@ module Obscenity extend self
|
|
33
37
|
|
34
38
|
|
35
39
|
end
|
36
|
-
|
40
|
+
|
data/lib/obscenity/base.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
1
|
module Obscenity
|
2
2
|
class Base
|
3
3
|
class << self
|
4
|
-
|
4
|
+
|
5
5
|
def blacklist
|
6
6
|
@blacklist ||= set_list_content(Obscenity.config.blacklist)
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def blacklist=(value)
|
10
10
|
@blacklist = value == :default ? set_list_content(Obscenity::Config.new.blacklist) : value
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def whitelist
|
14
14
|
@whitelist ||= set_list_content(Obscenity.config.whitelist)
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def whitelist=(value)
|
18
18
|
@whitelist = value == :default ? set_list_content(Obscenity::Config.new.whitelist) : value
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
def profane?(text)
|
22
22
|
return(false) unless text.to_s.size >= 3
|
23
23
|
blacklist.each do |foul|
|
@@ -25,7 +25,7 @@ module Obscenity
|
|
25
25
|
end
|
26
26
|
false
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def sanitize(text)
|
30
30
|
return(text) unless text.to_s.size >= 3
|
31
31
|
blacklist.each do |foul|
|
@@ -34,12 +34,12 @@ module Obscenity
|
|
34
34
|
@scoped_replacement = nil
|
35
35
|
text
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def replacement(chars)
|
39
39
|
@scoped_replacement = chars
|
40
40
|
self
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
def offensive(text)
|
44
44
|
words = []
|
45
45
|
return(words) unless text.to_s.size >= 3
|
@@ -48,17 +48,18 @@ module Obscenity
|
|
48
48
|
end
|
49
49
|
words.uniq
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
def replace(word)
|
53
53
|
content = @scoped_replacement || Obscenity.config.replacement
|
54
54
|
case content
|
55
55
|
when :vowels then word.gsub(/[aeiou]/i, '*')
|
56
56
|
when :stars then '*' * word.size
|
57
|
+
when :nonconsonants then word.gsub(/[^bcdfghjklmnpqrstvwxyz]/i, '*')
|
57
58
|
when :default, :garbled then '$@!#%'
|
58
59
|
else content
|
59
60
|
end
|
60
61
|
end
|
61
|
-
|
62
|
+
|
62
63
|
private
|
63
64
|
def set_list_content(list)
|
64
65
|
case list
|
@@ -67,7 +68,7 @@ module Obscenity
|
|
67
68
|
else []
|
68
69
|
end
|
69
70
|
end
|
70
|
-
|
71
|
+
|
71
72
|
end
|
72
73
|
end
|
73
|
-
end
|
74
|
+
end
|
data/lib/obscenity/version.rb
CHANGED
data/obscenity.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "obscenity"
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Thiago Jackiw"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-11-22"
|
13
13
|
s.description = " Obscenity is a profanity filter gem for Ruby/Rubinius, Rails (through ActiveModel), and Rack middleware "
|
14
14
|
s.email = "tjackiw@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
|
|
33
33
|
"lib/obscenity/config.rb",
|
34
34
|
"lib/obscenity/error.rb",
|
35
35
|
"lib/obscenity/rack.rb",
|
36
|
+
"lib/obscenity/rspec_matcher.rb",
|
36
37
|
"lib/obscenity/version.rb",
|
37
38
|
"obscenity.gemspec",
|
38
39
|
"test/helper.rb",
|
data/test/test_base.rb
CHANGED
@@ -25,7 +25,7 @@ class TestBase < Test::Unit::TestCase
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
context "#whitelist" do
|
30
30
|
context "without custom config" do
|
31
31
|
setup { Obscenity::Base.whitelist = :default }
|
@@ -41,11 +41,11 @@ class TestBase < Test::Unit::TestCase
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
context "#profane?" do
|
46
46
|
context "without whitelist" do
|
47
47
|
context "without custom config" do
|
48
|
-
setup {
|
48
|
+
setup {
|
49
49
|
Obscenity::Base.blacklist = :default
|
50
50
|
Obscenity::Base.whitelist = :default
|
51
51
|
}
|
@@ -66,7 +66,7 @@ class TestBase < Test::Unit::TestCase
|
|
66
66
|
end
|
67
67
|
context "with whitelist" do
|
68
68
|
context "without custom blacklist config" do
|
69
|
-
setup {
|
69
|
+
setup {
|
70
70
|
Obscenity::Base.blacklist = :default
|
71
71
|
Obscenity::Base.whitelist = ['biatch']
|
72
72
|
}
|
@@ -77,7 +77,7 @@ class TestBase < Test::Unit::TestCase
|
|
77
77
|
end
|
78
78
|
end
|
79
79
|
context "with custom blacklist/whitelist config" do
|
80
|
-
setup {
|
80
|
+
setup {
|
81
81
|
Obscenity::Base.blacklist = ['ass', 'word']
|
82
82
|
Obscenity::Base.whitelist = ['word']
|
83
83
|
}
|
@@ -89,11 +89,11 @@ class TestBase < Test::Unit::TestCase
|
|
89
89
|
end
|
90
90
|
end
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
context "#sanitize" do
|
94
94
|
context "without whitelist" do
|
95
95
|
context "without custom config" do
|
96
|
-
setup {
|
96
|
+
setup {
|
97
97
|
Obscenity::Base.blacklist = :default
|
98
98
|
Obscenity::Base.whitelist = :default
|
99
99
|
}
|
@@ -112,7 +112,7 @@ class TestBase < Test::Unit::TestCase
|
|
112
112
|
end
|
113
113
|
context "with whitelist" do
|
114
114
|
context "without custom blacklist config" do
|
115
|
-
setup {
|
115
|
+
setup {
|
116
116
|
Obscenity::Base.blacklist = :default
|
117
117
|
Obscenity::Base.whitelist = ['biatch']
|
118
118
|
}
|
@@ -122,7 +122,7 @@ class TestBase < Test::Unit::TestCase
|
|
122
122
|
end
|
123
123
|
end
|
124
124
|
context "with custom blacklist/whitelist config" do
|
125
|
-
setup {
|
125
|
+
setup {
|
126
126
|
Obscenity::Base.blacklist = ['clown', 'biatch']
|
127
127
|
Obscenity::Base.whitelist = ['biatch']
|
128
128
|
}
|
@@ -133,11 +133,11 @@ class TestBase < Test::Unit::TestCase
|
|
133
133
|
end
|
134
134
|
end
|
135
135
|
end
|
136
|
-
|
136
|
+
|
137
137
|
context "#replacement" do
|
138
138
|
context "without whitelist" do
|
139
139
|
context "without custom config" do
|
140
|
-
setup {
|
140
|
+
setup {
|
141
141
|
Obscenity::Base.blacklist = :default
|
142
142
|
Obscenity::Base.whitelist = :default
|
143
143
|
}
|
@@ -145,16 +145,18 @@ class TestBase < Test::Unit::TestCase
|
|
145
145
|
assert_equal "Yo ********, sup", Obscenity::Base.replacement(:stars).sanitize('Yo assclown, sup')
|
146
146
|
assert_equal "Yo $@!#%, sup", Obscenity::Base.replacement(:garbled).sanitize('Yo assclown, sup')
|
147
147
|
assert_equal "Yo *sscl*wn, sup", Obscenity::Base.replacement(:vowels).sanitize('Yo assclown, sup')
|
148
|
+
assert_equal "Oh, *h*t!", Obscenity::Base.replacement(:nonconsonants).sanitize('Oh, 5hit!')
|
148
149
|
assert_equal "Yo [censored], sup", Obscenity::Base.replacement('[censored]').sanitize('Yo assclown, sup')
|
149
150
|
assert_equal "Hello World", Obscenity::Base.replacement(:default).sanitize('Hello World')
|
150
151
|
end
|
151
152
|
end
|
152
153
|
context "with custom blacklist config" do
|
153
|
-
setup { Obscenity::Base.blacklist = ['ass', 'word'] }
|
154
|
+
setup { Obscenity::Base.blacklist = ['ass', 'word', 'w0rd'] }
|
154
155
|
should "sanitize and return a clean text based on a custom list" do
|
155
156
|
assert_equal "Yo ****, sup", Obscenity::Base.replacement(:stars).sanitize('Yo word, sup')
|
156
157
|
assert_equal "Yo $@!#%, sup", Obscenity::Base.replacement(:garbled).sanitize('Yo word, sup')
|
157
158
|
assert_equal "Yo w*rd, sup", Obscenity::Base.replacement(:vowels).sanitize('Yo word, sup')
|
159
|
+
assert_equal "Yo w*rd, sup", Obscenity::Base.replacement(:nonconsonants).sanitize('Yo w0rd, sup')
|
158
160
|
assert_equal "Yo [censored], sup", Obscenity::Base.replacement('[censored]').sanitize('Yo word, sup')
|
159
161
|
assert_equal "Hello World", Obscenity::Base.replacement(:default).sanitize('Hello World')
|
160
162
|
end
|
@@ -162,7 +164,7 @@ class TestBase < Test::Unit::TestCase
|
|
162
164
|
end
|
163
165
|
context "with whitelist" do
|
164
166
|
context "without custom blacklist config" do
|
165
|
-
setup {
|
167
|
+
setup {
|
166
168
|
Obscenity::Base.blacklist = :default
|
167
169
|
Obscenity::Base.whitelist = ['biatch']
|
168
170
|
}
|
@@ -170,12 +172,13 @@ class TestBase < Test::Unit::TestCase
|
|
170
172
|
assert_equal "Yo ********, sup", Obscenity::Base.replacement(:stars).sanitize('Yo assclown, sup')
|
171
173
|
assert_equal "Yo $@!#%, sup", Obscenity::Base.replacement(:garbled).sanitize('Yo assclown, sup')
|
172
174
|
assert_equal "Yo *sscl*wn, sup", Obscenity::Base.replacement(:vowels).sanitize('Yo assclown, sup')
|
175
|
+
assert_equal "What an *r**", Obscenity::Base.replacement(:nonconsonants).sanitize('What an ar5e')
|
173
176
|
assert_equal "Yo [censored], sup", Obscenity::Base.replacement('[censored]').sanitize('Yo assclown, sup')
|
174
177
|
assert_equal "Yo biatch, sup", Obscenity::Base.replacement(:default).sanitize('Yo biatch, sup')
|
175
178
|
end
|
176
179
|
end
|
177
180
|
context "with custom blacklist/whitelist config" do
|
178
|
-
setup {
|
181
|
+
setup {
|
179
182
|
Obscenity::Base.blacklist = ['clown', 'biatch']
|
180
183
|
Obscenity::Base.whitelist = ['biatch']
|
181
184
|
}
|
@@ -183,6 +186,7 @@ class TestBase < Test::Unit::TestCase
|
|
183
186
|
assert_equal "Yo *****, sup", Obscenity::Base.replacement(:stars).sanitize('Yo clown, sup')
|
184
187
|
assert_equal "Yo $@!#%, sup", Obscenity::Base.replacement(:garbled).sanitize('Yo clown, sup')
|
185
188
|
assert_equal "Yo cl*wn, sup", Obscenity::Base.replacement(:vowels).sanitize('Yo clown, sup')
|
189
|
+
assert_equal "Yo cl*wn, sup", Obscenity::Base.replacement(:nonconsonants).sanitize('Yo clown, sup')
|
186
190
|
assert_equal "Yo [censored], sup", Obscenity::Base.replacement('[censored]').sanitize('Yo clown, sup')
|
187
191
|
assert_equal "Yo biatch, sup", Obscenity::Base.replacement(:default).sanitize('Yo biatch, sup')
|
188
192
|
assert_equal "Yo assclown, sup", Obscenity::Base.replacement(:default).sanitize('Yo assclown, sup')
|
@@ -190,11 +194,11 @@ class TestBase < Test::Unit::TestCase
|
|
190
194
|
end
|
191
195
|
end
|
192
196
|
end
|
193
|
-
|
197
|
+
|
194
198
|
context "#offensive" do
|
195
199
|
context "without whitelist" do
|
196
200
|
context "without custom config" do
|
197
|
-
setup {
|
201
|
+
setup {
|
198
202
|
Obscenity::Base.blacklist = :default
|
199
203
|
Obscenity::Base.whitelist = :default
|
200
204
|
}
|
@@ -213,7 +217,7 @@ class TestBase < Test::Unit::TestCase
|
|
213
217
|
end
|
214
218
|
context "with whitelist" do
|
215
219
|
context "without custom blacklist config" do
|
216
|
-
setup {
|
220
|
+
setup {
|
217
221
|
Obscenity::Base.blacklist = :default
|
218
222
|
Obscenity::Base.whitelist = ['biatch']
|
219
223
|
}
|
@@ -223,7 +227,7 @@ class TestBase < Test::Unit::TestCase
|
|
223
227
|
end
|
224
228
|
end
|
225
229
|
context "with custom blacklist/whitelist config" do
|
226
|
-
setup {
|
230
|
+
setup {
|
227
231
|
Obscenity::Base.blacklist = ['clown', 'biatch']
|
228
232
|
Obscenity::Base.whitelist = ['biatch']
|
229
233
|
}
|
@@ -234,20 +238,21 @@ class TestBase < Test::Unit::TestCase
|
|
234
238
|
end
|
235
239
|
end
|
236
240
|
end
|
237
|
-
|
241
|
+
|
238
242
|
context "#replace" do
|
239
243
|
should "replace the given word by the given replacement method" do
|
240
244
|
[
|
241
|
-
[:vowels,
|
242
|
-
[:
|
243
|
-
[:
|
244
|
-
[:
|
245
|
-
[
|
246
|
-
[
|
245
|
+
[:vowels, {original: "Oh 5hit", clean: "Oh 5h*t"}],
|
246
|
+
[:nonconsonants, {original: "Oh 5hit", clean: "Oh *h*t"}],
|
247
|
+
[:stars, {original: "Oh 5hit", clean: "Oh ****"}],
|
248
|
+
[:garbled, {original: "Oh 5hit", clean: "Oh $@!#%"}],
|
249
|
+
[:default, {original: "Oh 5hit", clean: "Oh $@!#%"}],
|
250
|
+
["[censored]", {original: "Oh 5hit", clean: "Oh [censored]"}],
|
251
|
+
[nil, {original: "Oh 5hit", clean: "Oh $@!#%"}]
|
247
252
|
].each do |replacement_method, content|
|
248
|
-
assert_equal content[:clean], Obscenity::Base.replacement(replacement_method).sanitize(content[:original])
|
253
|
+
assert_equal content[:clean], Obscenity::Base.replacement(replacement_method).sanitize(content[:original]), "(replacement should match for #{replacement_method})"
|
249
254
|
end
|
250
255
|
end
|
251
256
|
end
|
252
|
-
|
257
|
+
|
253
258
|
end
|
data/test/test_obscenity.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestObscenity < Test::Unit::TestCase
|
4
|
-
|
4
|
+
|
5
5
|
context "#respond_to?" do
|
6
6
|
should "respond to methods and attributes" do
|
7
7
|
[:configure, :config, :profane?, :sanitize, :offensive, :replacement].each do |field|
|
@@ -9,7 +9,7 @@ class TestObscenity < Test::Unit::TestCase
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
# More comprehensive test in test_config.rb
|
14
14
|
context "#configure" do
|
15
15
|
should "accept a configuration block " do
|
@@ -18,17 +18,17 @@ class TestObscenity < Test::Unit::TestCase
|
|
18
18
|
config.blacklist = :default
|
19
19
|
config.replacement = :garbled
|
20
20
|
end
|
21
|
-
}
|
21
|
+
}
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
# More comprehensive test in test_config.rb
|
26
26
|
context "#config" do
|
27
27
|
should "return the current config object" do
|
28
28
|
assert_not_nil Obscenity.config
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
# More comprehensive test in test_base.rb
|
33
33
|
context "#profane?" do
|
34
34
|
should "validate the profanity of the given content" do
|
@@ -36,7 +36,7 @@ class TestObscenity < Test::Unit::TestCase
|
|
36
36
|
assert !Obscenity.profane?('Hello world')
|
37
37
|
end
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
# More comprehensive test in test_base.rb
|
41
41
|
context "#sanitize" do
|
42
42
|
should "sanitize the given content" do
|
@@ -44,7 +44,7 @@ class TestObscenity < Test::Unit::TestCase
|
|
44
44
|
assert_equal "Hello world", Obscenity.sanitize('Hello world')
|
45
45
|
end
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
# More comprehensive test in test_base.rb
|
49
49
|
context "#offensive" do
|
50
50
|
should "return the offensive words for the given content" do
|
@@ -52,17 +52,18 @@ class TestObscenity < Test::Unit::TestCase
|
|
52
52
|
assert_equal [], Obscenity.offensive('Hello world')
|
53
53
|
end
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
# More comprehensive test in test_base.rb
|
57
57
|
context "#replacement" do
|
58
58
|
should "sanitize the given content based on the given replacement" do
|
59
59
|
assert_equal "Yo, check that $@!#% out", Obscenity.replacement(:garbled).sanitize('Yo, check that ass out')
|
60
60
|
assert_equal "Yo, check that $@!#% out", Obscenity.replacement(:default).sanitize('Yo, check that ass out')
|
61
61
|
assert_equal "Yo, check that *ss out", Obscenity.replacement(:vowels).sanitize('Yo, check that ass out')
|
62
|
+
assert_equal "Yo, check that *h*t out", Obscenity.replacement(:nonconsonants).sanitize('Yo, check that 5hit out')
|
62
63
|
assert_equal "Yo, check that *** out", Obscenity.replacement(:stars).sanitize('Yo, check that ass out')
|
63
64
|
assert_equal "Yo, check that [censored] out", Obscenity.replacement("[censored]").sanitize('Yo, check that ass out')
|
64
65
|
assert_equal "Hello world", Obscenity.sanitize('Hello world')
|
65
66
|
end
|
66
67
|
end
|
67
|
-
|
68
|
+
|
68
69
|
end
|
data/test/test_rack.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rack/mock'
|
|
3
3
|
require 'obscenity/rack'
|
4
4
|
|
5
5
|
class TestRack < Test::Unit::TestCase
|
6
|
-
|
6
|
+
|
7
7
|
context "Rack::Obscenity" do
|
8
8
|
setup do
|
9
9
|
@env = {}
|
@@ -12,37 +12,37 @@ class TestRack < Test::Unit::TestCase
|
|
12
12
|
@headers = { 'Content-Type' => 'text/plain' }
|
13
13
|
@app = lambda { |env| [@status, @headers, [@body]] }
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def mock_env(options = {})
|
17
17
|
@env = Rack::MockRequest.env_for('/', options)
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def middleware(options = {})
|
21
21
|
Rack::Obscenity.new(@app, options)
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def get(params = {})
|
25
25
|
{ 'QUERY_STRING' => Rack::Utils.build_query(params) }
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def get_response_params
|
29
29
|
Rack::Utils.parse_query(@env['QUERY_STRING'], "&")
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def post(params = {})
|
33
33
|
{ 'rack.input' => StringIO.new(Rack::Utils.build_query(params)) }
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
def post_response_params
|
37
37
|
Rack::Utils.parse_query(@env['rack.input'].read, "&")
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
def assert_success_response(status, headers, body)
|
41
41
|
assert_equal @status, status
|
42
42
|
assert_equal @headers, headers
|
43
43
|
assert_equal [@body], body
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
context "default configuration" do
|
47
47
|
should "not evaluate the profanity of parameters" do
|
48
48
|
app = middleware
|
@@ -50,7 +50,7 @@ class TestRack < Test::Unit::TestCase
|
|
50
50
|
assert_success_response status, headers, body
|
51
51
|
end
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
context "rejecting requests" do
|
55
55
|
should "not reject if parameter values don't contain profanity" do
|
56
56
|
app = middleware(reject: true)
|
@@ -74,7 +74,7 @@ class TestRack < Test::Unit::TestCase
|
|
74
74
|
|
75
75
|
should "reject if given parameter values contain profanity" do
|
76
76
|
app = middleware(reject: { params: [:foo] })
|
77
|
-
[ get(foo: 'ass', baz: 'shit'),
|
77
|
+
[ get(foo: 'ass', baz: 'shit'),
|
78
78
|
post(foo: 'ass').merge(get(foo: 'nice', baz: 'shit'))
|
79
79
|
].each do |options|
|
80
80
|
status, headers, body = app.call(mock_env(options))
|
@@ -117,7 +117,7 @@ class TestRack < Test::Unit::TestCase
|
|
117
117
|
assert_equal [''], body
|
118
118
|
end
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
context "sanitizing requests" do
|
122
122
|
should "not sanitize if parameter values don't contain profanity" do
|
123
123
|
app = middleware(sanitize: true)
|
@@ -135,7 +135,7 @@ class TestRack < Test::Unit::TestCase
|
|
135
135
|
assert_equal 'bar', request_params['foo']
|
136
136
|
assert_equal '$@!#%', request_params['baz']
|
137
137
|
end
|
138
|
-
|
138
|
+
|
139
139
|
should "sanitize if POST parameter values contain profanity" do
|
140
140
|
app = middleware(sanitize: true)
|
141
141
|
status, headers, body = app.call(mock_env(post(foo: 'bar', baz: 'ass')))
|
@@ -161,7 +161,7 @@ class TestRack < Test::Unit::TestCase
|
|
161
161
|
request_params = get_response_params
|
162
162
|
assert_equal '$@!#%', request_params['foo']
|
163
163
|
end
|
164
|
-
|
164
|
+
|
165
165
|
should "sanitize the title using the :garbled replacement" do
|
166
166
|
app = middleware(sanitize: { replacement: :garbled })
|
167
167
|
status, headers, body = app.call(mock_env(get(foo: 'ass')))
|
@@ -186,6 +186,14 @@ class TestRack < Test::Unit::TestCase
|
|
186
186
|
assert_equal '*ss', request_params['foo']
|
187
187
|
end
|
188
188
|
|
189
|
+
should "sanitize the title using the :nonconsonants replacement" do
|
190
|
+
app = middleware(sanitize: { replacement: :nonconsonants })
|
191
|
+
status, headers, body = app.call(mock_env(get(foo: '5hit')))
|
192
|
+
assert_success_response status, headers, body
|
193
|
+
request_params = get_response_params
|
194
|
+
assert_equal '*h*t', request_params['foo']
|
195
|
+
end
|
196
|
+
|
189
197
|
should "sanitize the title using a custom replacement" do
|
190
198
|
app = middleware(sanitize: { replacement: "[censored]" })
|
191
199
|
status, headers, body = app.call(mock_env(get(foo: 'text with ass')))
|
data/test/test_version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: obscenity
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: shoulda
|
@@ -148,6 +148,7 @@ files:
|
|
148
148
|
- lib/obscenity/config.rb
|
149
149
|
- lib/obscenity/error.rb
|
150
150
|
- lib/obscenity/rack.rb
|
151
|
+
- lib/obscenity/rspec_matcher.rb
|
151
152
|
- lib/obscenity/version.rb
|
152
153
|
- obscenity.gemspec
|
153
154
|
- test/helper.rb
|
@@ -173,7 +174,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
173
174
|
version: '0'
|
174
175
|
segments:
|
175
176
|
- 0
|
176
|
-
hash:
|
177
|
+
hash: -2450424540235026340
|
177
178
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
178
179
|
none: false
|
179
180
|
requirements:
|