accept_headers 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,203 +1,199 @@
1
1
  require_relative "spec_helper"
2
2
 
3
- module AcceptHeaders
4
- describe MediaType do
5
- subject do
6
- AcceptHeaders::MediaType
7
- end
3
+ describe AcceptHeaders::MediaType do
4
+ subject do
5
+ AcceptHeaders::MediaType
6
+ end
8
7
 
9
- it "defaults type to *" do
10
- subject.new.type.must_equal '*'
11
- end
8
+ it "defaults type to *" do
9
+ subject.new.type.must_equal '*'
10
+ end
12
11
 
13
- it "defaults subtype to *" do
14
- subject.new('text').subtype.must_equal '*'
15
- end
12
+ it "defaults subtype to *" do
13
+ subject.new('text').subtype.must_equal '*'
14
+ end
16
15
 
17
- it "strips and downcases the type" do
18
- subject.new("\t\nTEXt\s\r", '*').type.must_equal "text"
19
- end
16
+ it "strips and downcases the type" do
17
+ subject.new("\t\nTEXt\s\r", '*').type.must_equal "text"
18
+ end
20
19
 
21
- it "strips and downcases the subtype" do
22
- subject.new("text", "\s\npLAIn\r\t").subtype.must_equal "plain"
23
- end
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
- it "sets subtype to * if value passed in is nil and type is *" do
26
- subject.new('*', nil).subtype.must_equal '*'
27
- end
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
- it "strips the keys and values in the params hash" do
30
- subject.new('*', '*', params: { "\s\nLEVEL\r\t" => "\t\nX\s\n"}).params['LEVEL'].must_equal 'X'
31
- end
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
- it "optionally supports a q argument" do
34
- subject.new('text', 'html', q: 0.8).q.must_equal 0.8
35
- end
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
- it "optionally supports a params argument" do
38
- subject.new('text', 'html', params: { 'level' => '1' }).params['level'].must_equal '1'
39
- end
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
- it "compares on q value all other values remaining equal" do
42
- subject.new(q: 0.514).must_be :>, subject.new(q: 0.1)
43
- subject.new(q: 0).must_be :<, subject.new(q: 1)
44
- subject.new(q: 0.9).must_equal subject.new(q: 0.9)
45
- end
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
- it "compares on subtype then type all other values remaining equal" do
48
- subject.new('text', 'html').must_be :>, subject.new('text', '*')
49
- subject.new('*', '*').must_be :<, subject.new('text', '*')
50
- end
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
- it "raises an InvalidQError if q can't be converted to a float" do
53
- e = -> do
54
- subject.new('text', 'html', q: 'a')
55
- end.must_raise MediaType::InvalidQError
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
- e.message.must_equal 'invalid value for Float(): "a"'
56
+ e.message.must_match INVALID_FLOAT_PATTERN
58
57
 
59
- subject.new('text', 'html', q: '1')
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
- it "raises an InvalidQError unless q value is between 0 and 1" do
63
- [-1.0, -0.1, 1.1].each do |q|
64
- e = -> do
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
- e.message.must_equal "q must be between 0 and 1"
69
- end
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
- subject.new('text', 'html', q: 1)
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
- it "raises an InvalidQError if q has more than a precision of 3" do
76
- e = -> do
77
- subject.new('text', 'html', q: 0.1234)
78
- end.must_raise MediaType::InvalidQError
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
- e.message.must_equal "q must be at most 3 decimal places"
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
- subject.new('text', 'html', q: 0.123)
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 "converts to hash" do
86
- subject.new('text', 'html').to_h.must_equal({
87
- type: 'text',
88
- subtype: 'html',
89
- q: 1.0,
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 "converts to string" do
95
- s = subject.new('text', 'html', q: 0.9, params: { 'level' => '1' }).to_s
96
- s.must_equal "text/html;q=0.9;level=1"
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 "outputs the media range" do
100
- subject.new('text', 'html', params: { 'level' => '1' }).media_range.must_equal "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
- describe "#accept?" do
104
- it "accepted if the type and subtype are the same" do
105
- a = subject.new('text', 'html')
106
- a.accept?('text/html').must_equal true
107
- b = subject.new('text', 'html', q: 0.001)
108
- b.accept?('text/html').must_equal true
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
- it "accepted if the type is the same and the other subtype is *" do
112
- a = subject.new('text', '*')
113
- a.accept?('text/html').must_equal true
114
- b = subject.new('text', '*', q: 0.9)
115
- b.accept?('text/html').must_equal true
116
- end
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
- it "accepted if the type and subtype are *" do
119
- a = subject.new('*')
120
- a.accept?('text/html').must_equal true
121
- b = subject.new('*', q: 0.1)
122
- b.accept?('text/html').must_equal true
123
- end
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
- it "not accepted if the type and subtype don't match" do
126
- a = subject.new('text', 'html')
127
- a.accept?('application/json').must_equal false
128
- b = subject.new('text', 'html', q: 0.2)
129
- b.accept?('application/json').must_equal false
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 "not accepted if the type doesn't match" do
133
- a = subject.new('text', 'plain')
134
- a.accept?('application/plain').must_equal false
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 "not accepted if the subtype doesn't match" do
140
- a = subject.new('text', 'html')
141
- a.accept?('text/plain').must_equal false
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 accepted if q is 0" do
184
+ it "not rejected if the type and subtype don't match" do
147
185
  a = subject.new('text', 'html', q: 0)
148
- a.accept?('text/html').must_equal false
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
- # TODO: test *
158
- it "not accepted if..." do
159
- a = subject.new('text', 'plain')
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
- describe "#reject?" do
165
- describe "given q is 0" do
166
- it "rejected if the type and subtype are the same" do
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
@@ -20,6 +20,8 @@ require "pry"
20
20
 
21
21
  require_relative "../lib/accept_headers"
22
22
 
23
+ INVALID_FLOAT_PATTERN = /\Ainvalid \w+ for Float(?:\(\)\: |\z)/
24
+
23
25
  class Minitest::Spec
24
26
  def chrome
25
27
  {
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.6
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-17 00:00:00.000000000 Z
11
+ date: 2014-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler