url_validation 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,231 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class Record
4
- extend ActiveModel::Translation
5
- include ActiveModel::Validations
6
- attr_accessor :field
7
- end
8
-
9
- describe UrlValidator do
10
- before :each do
11
- @record = Record.new
12
- end
13
-
14
- context "[basic]" do
15
- it "should allow nil if :allow_nil is set" do
16
- @validator = UrlValidator.new(:attributes => [ :field ], :allow_nil => true)
17
- @validator.validate_each(@record, :field, nil)
18
- expect(@record.errors).to be_empty
19
- end
20
-
21
- it "should allow \"\" if :allow_blank is set" do
22
- @validator = UrlValidator.new(:attributes => [ :field ], :allow_blank => true)
23
- @validator.validate_each(@record, :field, "")
24
- expect(@record.errors).to be_empty
25
- end
26
- end
27
-
28
- context "[format]" do
29
- it "should only allow HTTP URLs if :scheme is set to 'http'" do
30
- @validator = UrlValidator.new(:attributes => [ :field ], :scheme => 'http')
31
- @validator.validate_each(@record, :field, "http://www.apple.com")
32
- expect(@record.errors).to be_empty
33
-
34
- @validator.validate_each(@record, :field, "https://www.apple.com")
35
- expect(@record.errors[:field].first).to include('invalid_url')
36
- end
37
-
38
- it "should only allow HTTP and HTTPS URLs if :scheme is set to %w( http https )" do
39
- @validator = UrlValidator.new(:attributes => [ :field ], :scheme => %w( http https ))
40
- @validator.validate_each(@record, :field, "http://www.apple.com")
41
- expect(@record.errors).to be_empty
42
- @validator.validate_each(@record, :field, "https://www.apple.com")
43
- expect(@record.errors).to be_empty
44
-
45
- @validator.validate_each(@record, :field, "ftp://www.apple.com")
46
- expect(@record.errors[:field].first).to include('invalid_url')
47
- end
48
-
49
- it "should try a default scheme if :default_scheme is set" do
50
- @validator = UrlValidator.new(:attributes => [ :field ], :scheme => 'http', :default_scheme => 'http')
51
- @validator.validate_each(@record, :field, "www.apple.com")
52
- expect(@record.errors).to be_empty
53
- end
54
-
55
- context "[HTTP(S)]" do
56
- it "should not allow garbage URLs that still somehow pass the ridiculously open-ended RFC" do
57
- @validator = UrlValidator.new(:attributes => [ :field ])
58
-
59
- [
60
- 'http:sdg.sdfg/',
61
- 'http/sdg.d',
62
- 'http:://dsfg.dsfg/',
63
- 'http//sdg..g',
64
- 'http://://sdfg.f',
65
- 'http://dsaf.com://sdg.com'
66
- ].each do |uri|
67
- @record.errors.clear
68
- @validator.validate_each(@record, :field, "www.apple.com")
69
- expect(@record.errors[:field].first).to include('invalid_url')
70
- end
71
- end
72
- end
73
- end
74
-
75
- context "[accessibility]" do
76
- context "[:check_host]" do
77
- it "should only validate if the host is accessible when :check_host is set" do
78
- @validator = UrlValidator.new(:attributes => [ :field ])
79
- @validator.validate_each(@record, :field, "http://www.invalid.tld")
80
- expect(@record.errors).to be_empty
81
-
82
- @validator = UrlValidator.new(:attributes => [ :field ], :check_host => true)
83
- @validator.validate_each(@record, :field, "http://www.invalid.tld")
84
- expect(@record.errors[:field].first).to include('url_not_accessible')
85
- end
86
-
87
- it "should not perform the accessibility check if :check_host is set to 'http' and the URL scheme is not HTTP" do
88
- @validator = UrlValidator.new(:attributes => [ :field ], :check_host => 'http')
89
- @validator.validate_each(@record, :field, "https://www.invalid.tld")
90
- expect(@record.errors).to be_empty
91
- end
92
-
93
- it "should only validate if the host is accessible when :check_host is set to 'http' and the URL scheme is HTTP" do
94
- @validator = UrlValidator.new(:attributes => [ :field ], :check_host => 'http')
95
- @validator.validate_each(@record, :field, "http://www.invalid.tld")
96
- expect(@record.errors[:field].first).to include('url_not_accessible')
97
- end
98
-
99
- it "should not perform the accessibility check if :check_host is set to %w( http https ) and the URL scheme is not HTTP(S)" do
100
- @validator = UrlValidator.new(:attributes => [ :field ], :check_host => %w( http https ), :scheme => %w( ftp http https ))
101
- @validator.validate_each(@record, :field, "ftp://www.invalid.tld")
102
- expect(@record.errors).to be_empty
103
- end
104
-
105
- it "should only validate if the host is accessible when :check_host is set to %w( http https ) and the URL scheme is HTTP(S)" do
106
- @validator = UrlValidator.new(:attributes => [ :field ], :check_host => %w( http https ))
107
- @validator.validate_each(@record, :field, "http://www.invalid.tld")
108
- expect(@record.errors[:field].first).to include('url_not_accessible')
109
-
110
- @validator = UrlValidator.new(:attributes => [ :field ], :check_host => %w( http https ))
111
- @validator.validate_each(@record, :field, "https://www.invalid.tld")
112
- expect(@record.errors[:field].first).to include('url_not_accessible')
113
- end
114
-
115
- it "should only validate the host" do
116
- @validator = UrlValidator.new(:attributes => [ :field ], :check_host => true)
117
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
118
- expect(@record.errors).to be_empty
119
- end
120
- end
121
-
122
- context "[:check_path]" do
123
- it "should not validate if the response code is equal to the Fixnum value of this option" do
124
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => 404)
125
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
126
- expect(@record.errors[:field].first).to include('url_invalid_response')
127
-
128
- @record.errors.clear
129
-
130
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => 405)
131
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
132
- expect(@record.errors[:field]).to be_empty
133
- end
134
-
135
- it "should not validate if the response code is equal to the Symbol value of this option" do
136
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => :not_found)
137
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
138
- expect(@record.errors[:field].first).to include('url_invalid_response')
139
-
140
- @record.errors.clear
141
-
142
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => :unauthorized)
143
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
144
- expect(@record.errors[:field]).to be_empty
145
- end
146
-
147
- it "should not validate if the response code is within the Range value of this option" do
148
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => 400..499)
149
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
150
- expect(@record.errors[:field].first).to include('url_invalid_response')
151
-
152
- @record.errors.clear
153
-
154
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => 500..599)
155
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
156
- expect(@record.errors[:field]).to be_empty
157
- end
158
-
159
- it "should not validate if the response code is equal to the Fixnum value contained in the Array value of this option" do
160
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ 404, 405 ])
161
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
162
- expect(@record.errors[:field].first).to include('url_invalid_response')
163
-
164
- @record.errors.clear
165
-
166
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ 405, 406 ])
167
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
168
- expect(@record.errors[:field]).to be_empty
169
- end
170
-
171
- it "should not validate if the response code is equal to the Symbol value contained in the Array value of this option" do
172
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ :not_found, :unauthorized ])
173
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
174
- expect(@record.errors[:field].first).to include('url_invalid_response')
175
-
176
- @record.errors.clear
177
-
178
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ :unauthorized, :moved_permanently ])
179
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
180
- expect(@record.errors[:field]).to be_empty
181
- end
182
-
183
- it "should not validate if the response code is equal to the Range value contained in the Array value of this option" do
184
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ 400..499, 500..599 ])
185
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
186
- expect(@record.errors[:field].first).to include('url_invalid_response')
187
-
188
- @record.errors.clear
189
-
190
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => [ 500..599, 300..399 ])
191
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
192
- expect(@record.errors[:field]).to be_empty
193
- end
194
-
195
- it "should skip validation by default" do
196
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => nil)
197
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
198
- expect(@record.errors[:field]).to be_empty
199
- end
200
-
201
- it "should not validate 4xx and 5xx response codes if the value is true" do
202
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => true)
203
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
204
- expect(@record.errors[:field].first).to include('url_invalid_response')
205
- end
206
-
207
- it "should skip validation for non-HTTP URLs" do
208
- @validator = UrlValidator.new(:attributes => [ :field ], :check_path => true, :scheme => %w( ftp http https ))
209
- @validator.validate_each(@record, :field, "ftp://ftp.sdgasdgohaodgh.com/sdgjsdg")
210
- expect(@record.errors[:field]).to be_empty
211
- end
212
- end
213
-
214
- context "[:httpi_adapter]" do
215
- it "should use the specified HTTPI adapter" do
216
- @validator = UrlValidator.new(:attributes => [ :field ], :httpi_adapter => :curl, :check_host => true)
217
- expect(HTTPI).to receive(:get).once.with(an_instance_of(HTTPI::Request), :curl).and_return(false)
218
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
219
- end
220
- end
221
-
222
- context "[:request_callback]" do
223
- it "should be yielded the HTTPI request" do
224
- called = false
225
- @validator = UrlValidator.new(:attributes => [ :field ], :check_host => true, :request_callback => lambda { |request| called = true; expect(request).to be_kind_of(HTTPI::Request) })
226
- @validator.validate_each(@record, :field, "http://www.google.com/sdgsdgf")
227
- expect(called).to eql(true)
228
- end
229
- end
230
- end
231
- end