accept_headers 0.0.6 → 0.0.7
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.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/CHANGELOG.md +6 -0
- data/README.md +22 -8
- data/lib/accept_headers/language.rb +1 -1
- data/lib/accept_headers/media_type/negotiator.rb +15 -15
- data/lib/accept_headers/media_type.rb +12 -12
- data/lib/accept_headers/version.rb +1 -1
- data/spec/encoding_spec.rb +108 -102
- data/spec/language_spec.rb +161 -153
- data/spec/media_type/negotiator_spec.rb +9 -9
- data/spec/media_type_spec.rb +162 -156
- data/spec/spec_helper.rb +2 -0
- metadata +2 -2
data/spec/media_type_spec.rb
CHANGED
@@ -1,203 +1,199 @@
|
|
1
1
|
require_relative "spec_helper"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
3
|
+
describe AcceptHeaders::MediaType do
|
4
|
+
subject do
|
5
|
+
AcceptHeaders::MediaType
|
6
|
+
end
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
it "defaults type to *" do
|
9
|
+
subject.new.type.must_equal '*'
|
10
|
+
end
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
it "defaults subtype to *" do
|
13
|
+
subject.new('text').subtype.must_equal '*'
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
it "strips and downcases the type" do
|
17
|
+
subject.new("\t\nTEXt\s\r", '*').type.must_equal "text"
|
18
|
+
end
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
it "strips and downcases the subtype" do
|
21
|
+
subject.new("text", "\s\npLAIn\r\t").subtype.must_equal "plain"
|
22
|
+
end
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
it "sets subtype to * if value passed in is nil and type is *" do
|
25
|
+
subject.new('*', nil).subtype.must_equal '*'
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
it "strips only the keys in the extensions hash" do
|
29
|
+
subject.new('*', '*', extensions: { "\s\nLEVEL\r\t" => "\t\nX\s\n"}).extensions['LEVEL'].must_equal "\t\nX\s\n"
|
30
|
+
end
|
32
31
|
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
it "optionally supports a q argument" do
|
33
|
+
subject.new('text', 'html', q: 0.8).q.must_equal 0.8
|
34
|
+
end
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
it "optionally supports a extensions argument" do
|
37
|
+
subject.new('text', 'html', extensions: { 'level' => '1' }).extensions['level'].must_equal '1'
|
38
|
+
end
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
40
|
+
it "compares on q value all other values remaining equal" do
|
41
|
+
subject.new(q: 0.514).must_be :>, subject.new(q: 0.1)
|
42
|
+
subject.new(q: 0).must_be :<, subject.new(q: 1)
|
43
|
+
subject.new(q: 0.9).must_equal subject.new(q: 0.9)
|
44
|
+
end
|
46
45
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
46
|
+
it "compares on subtype then type all other values remaining equal" do
|
47
|
+
subject.new('text', 'html').must_be :>, subject.new('text', '*')
|
48
|
+
subject.new('*', '*').must_be :<, subject.new('text', '*')
|
49
|
+
end
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
it "raises an InvalidQError if q can't be converted to a float" do
|
52
|
+
e = -> do
|
53
|
+
subject.new('text', 'html', q: 'a')
|
54
|
+
end.must_raise AcceptHeaders::MediaType::InvalidQError
|
56
55
|
|
57
|
-
|
56
|
+
e.message.must_match INVALID_FLOAT_PATTERN
|
58
57
|
|
59
|
-
|
58
|
+
subject.new('text', 'html', q: '1')
|
59
|
+
end
|
60
|
+
|
61
|
+
it "raises an InvalidQError unless q value is between 0 and 1" do
|
62
|
+
[-1.0, -0.1, 1.1].each do |q|
|
63
|
+
e = -> do
|
64
|
+
subject.new('text', 'html', q: q)
|
65
|
+
end.must_raise AcceptHeaders::MediaType::InvalidQError
|
66
|
+
|
67
|
+
e.message.must_equal "q must be between 0 and 1"
|
60
68
|
end
|
61
69
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
subject.new('text', 'html', q: q)
|
66
|
-
end.must_raise MediaType::InvalidQError
|
70
|
+
subject.new('text', 'html', q: 1)
|
71
|
+
subject.new('text', 'html', q: 0)
|
72
|
+
end
|
67
73
|
|
68
|
-
|
69
|
-
|
74
|
+
it "raises an InvalidQError if q has more than a precision of 3" do
|
75
|
+
e = -> do
|
76
|
+
subject.new('text', 'html', q: 0.1234)
|
77
|
+
end.must_raise AcceptHeaders::MediaType::InvalidQError
|
70
78
|
|
71
|
-
|
72
|
-
subject.new('text', 'html', q: 0)
|
73
|
-
end
|
79
|
+
e.message.must_equal "q must be at most 3 decimal places"
|
74
80
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
81
|
+
subject.new('text', 'html', q: 0.123)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "converts to hash" do
|
85
|
+
subject.new('text', 'html').to_h.must_equal({
|
86
|
+
type: 'text',
|
87
|
+
subtype: 'html',
|
88
|
+
q: 1.0,
|
89
|
+
extensions: {}
|
90
|
+
})
|
91
|
+
end
|
79
92
|
|
80
|
-
|
93
|
+
it "converts to string" do
|
94
|
+
s = subject.new('text', 'html', q: 0.9, extensions: { 'level' => '1' }).to_s
|
95
|
+
s.must_equal "text/html;q=0.9;level=1"
|
96
|
+
end
|
97
|
+
|
98
|
+
it "outputs the media range" do
|
99
|
+
subject.new('text', 'html', extensions: { 'level' => '1' }).media_range.must_equal "text/html"
|
100
|
+
end
|
81
101
|
|
82
|
-
|
102
|
+
describe "#accept?" do
|
103
|
+
it "accepted if the type and subtype are the same" do
|
104
|
+
a = subject.new('text', 'html')
|
105
|
+
a.accept?('text/html').must_equal true
|
106
|
+
b = subject.new('text', 'html', q: 0.001)
|
107
|
+
b.accept?('text/html').must_equal true
|
83
108
|
end
|
84
109
|
|
85
|
-
it "
|
86
|
-
subject.new('text', '
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
params: {}
|
91
|
-
})
|
110
|
+
it "accepted if the type is the same and the other subtype is *" do
|
111
|
+
a = subject.new('text', '*')
|
112
|
+
a.accept?('text/html').must_equal true
|
113
|
+
b = subject.new('text', '*', q: 0.9)
|
114
|
+
b.accept?('text/html').must_equal true
|
92
115
|
end
|
93
116
|
|
94
|
-
it "
|
95
|
-
|
96
|
-
|
117
|
+
it "accepted if the type and subtype are *" do
|
118
|
+
a = subject.new('*')
|
119
|
+
a.accept?('text/html').must_equal true
|
120
|
+
b = subject.new('*', q: 0.1)
|
121
|
+
b.accept?('text/html').must_equal true
|
97
122
|
end
|
98
123
|
|
99
|
-
it "
|
100
|
-
subject.new('text', 'html'
|
124
|
+
it "not accepted if the type and subtype don't match" do
|
125
|
+
a = subject.new('text', 'html')
|
126
|
+
a.accept?('application/json').must_equal false
|
127
|
+
b = subject.new('text', 'html', q: 0.2)
|
128
|
+
b.accept?('application/json').must_equal false
|
101
129
|
end
|
102
130
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
end
|
131
|
+
it "not accepted if the type doesn't match" do
|
132
|
+
a = subject.new('text', 'plain')
|
133
|
+
a.accept?('application/plain').must_equal false
|
134
|
+
b = subject.new('text', 'plain', q: 0.4)
|
135
|
+
b.accept?('application/json').must_equal false
|
136
|
+
end
|
110
137
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
138
|
+
it "not accepted if the subtype doesn't match" do
|
139
|
+
a = subject.new('text', 'html')
|
140
|
+
a.accept?('text/plain').must_equal false
|
141
|
+
b = subject.new('text', 'html', q: 0.6)
|
142
|
+
b.accept?('text/plain').must_equal false
|
143
|
+
end
|
117
144
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
145
|
+
it "not accepted if q is 0" do
|
146
|
+
a = subject.new('text', 'html', q: 0)
|
147
|
+
a.accept?('text/html').must_equal false
|
148
|
+
a.accept?('text/plain').must_equal false
|
149
|
+
a.accept?('application/plain').must_equal false
|
150
|
+
b = subject.new('text', '*', q: 0)
|
151
|
+
b.accept?('text/html').must_equal false
|
152
|
+
c = subject.new('*', q: 0)
|
153
|
+
c.accept?('text/html').must_equal false
|
154
|
+
end
|
155
|
+
|
156
|
+
it "not accepted compared against nil" do
|
157
|
+
subject.new('text', 'html').accept?(nil).must_equal false
|
158
|
+
end
|
159
|
+
|
160
|
+
# TODO: test *
|
161
|
+
it "not accepted if..." do
|
162
|
+
a = subject.new('text', 'plain')
|
163
|
+
a.accept?('*').must_equal false
|
164
|
+
end
|
165
|
+
end
|
124
166
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
167
|
+
describe "#reject?" do
|
168
|
+
describe "given q is 0" do
|
169
|
+
it "rejected if the type and subtype are the same" do
|
170
|
+
a = subject.new('text', 'html', q: 0)
|
171
|
+
a.reject?('text/html').must_equal true
|
130
172
|
end
|
131
173
|
|
132
|
-
it "
|
133
|
-
a = subject.new('text', '
|
134
|
-
a.
|
135
|
-
b = subject.new('text', 'plain', q: 0.4)
|
136
|
-
b.accept?('application/json').must_equal false
|
174
|
+
it "rejected if the type is the same and the other subtype is *" do
|
175
|
+
a = subject.new('text', '*', q: 0)
|
176
|
+
a.reject?('text/html').must_equal true
|
137
177
|
end
|
138
178
|
|
139
|
-
it "
|
140
|
-
a = subject.new('
|
141
|
-
a.
|
142
|
-
b = subject.new('text', 'html', q: 0.6)
|
143
|
-
b.accept?('text/plain').must_equal false
|
179
|
+
it "rejected if the type and subtype are *" do
|
180
|
+
a = subject.new('*', q: 0)
|
181
|
+
a.reject?('text/html').must_equal true
|
144
182
|
end
|
145
183
|
|
146
|
-
it "not
|
184
|
+
it "not rejected if the type and subtype don't match" do
|
147
185
|
a = subject.new('text', 'html', q: 0)
|
148
|
-
a.
|
149
|
-
a.accept?('text/plain').must_equal false
|
150
|
-
a.accept?('application/plain').must_equal false
|
151
|
-
b = subject.new('text', '*', q: 0)
|
152
|
-
b.accept?('text/html').must_equal false
|
153
|
-
c = subject.new('*', q: 0)
|
154
|
-
c.accept?('text/html').must_equal false
|
186
|
+
a.reject?('application/json').must_equal false
|
155
187
|
end
|
156
188
|
|
157
|
-
|
158
|
-
|
159
|
-
a
|
160
|
-
a.accept?('*').must_equal false
|
189
|
+
it "not rejected if the type doesn't match" do
|
190
|
+
a = subject.new('text', 'plain', q: 0)
|
191
|
+
a.reject?('application/plain').must_equal false
|
161
192
|
end
|
162
|
-
end
|
163
193
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
a = subject.new('text', 'html', q: 0)
|
168
|
-
a.reject?('text/html').must_equal true
|
169
|
-
end
|
170
|
-
|
171
|
-
it "rejected if the type is the same and the other subtype is *" do
|
172
|
-
a = subject.new('text', '*', q: 0)
|
173
|
-
a.reject?('text/html').must_equal true
|
174
|
-
end
|
175
|
-
|
176
|
-
it "rejected if the type and subtype are *" do
|
177
|
-
a = subject.new('*', q: 0)
|
178
|
-
a.reject?('text/html').must_equal true
|
179
|
-
end
|
180
|
-
|
181
|
-
it "not rejected if the type and subtype don't match" do
|
182
|
-
a = subject.new('text', 'html', q: 0)
|
183
|
-
a.reject?('application/json').must_equal false
|
184
|
-
end
|
185
|
-
|
186
|
-
it "not rejected if the type doesn't match" do
|
187
|
-
a = subject.new('text', 'plain', q: 0)
|
188
|
-
a.reject?('application/plain').must_equal false
|
189
|
-
end
|
190
|
-
|
191
|
-
it "not rejected if the subtype doesn't match" do
|
192
|
-
a = subject.new('text', 'html', q: 0)
|
193
|
-
a.reject?('text/plain').must_equal false
|
194
|
-
end
|
195
|
-
|
196
|
-
# TODO: test *
|
197
|
-
it "not rejected if..." do
|
198
|
-
a = subject.new('text', 'plain', q: 0)
|
199
|
-
a.reject?('*').must_equal false
|
200
|
-
end
|
194
|
+
it "not rejected if the subtype doesn't match" do
|
195
|
+
a = subject.new('text', 'html', q: 0)
|
196
|
+
a.reject?('text/plain').must_equal false
|
201
197
|
end
|
202
198
|
|
203
199
|
it "not rejected if q > 0" do
|
@@ -210,6 +206,16 @@ module AcceptHeaders
|
|
210
206
|
c = subject.new('*', q: 1)
|
211
207
|
c.reject?('text/html').must_equal false
|
212
208
|
end
|
209
|
+
|
210
|
+
it "not rejected compared against nil" do
|
211
|
+
subject.new('text', 'html').reject?(nil).must_equal false
|
212
|
+
end
|
213
|
+
|
214
|
+
# TODO: test *
|
215
|
+
it "not rejected if..." do
|
216
|
+
a = subject.new('text', 'plain', q: 0)
|
217
|
+
a.reject?('*').must_equal false
|
218
|
+
end
|
213
219
|
end
|
214
220
|
end
|
215
221
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: accept_headers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jack Chu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|