Skip to content

Commit 32786bf

Browse files
committed
Fix tests / documentation.
1 parent 3334ca2 commit 32786bf

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

lib/protocol/http/header/accept.rb

+10
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ class Accept < Array
3030

3131
# A single entry in the Accept: header, which includes a mime type and associated parameters. A media range can include wild cards, but a media type is a specific type and subtype.
3232
MediaRange = Struct.new(:type, :subtype, :parameters) do
33+
# Create a new media range.
34+
#
35+
# @parameter type [String] the type of the media range.
36+
# @parameter subtype [String] the subtype of the media range.
37+
# @parameter parameters [Hash] the parameters associated with the media range.
3338
def initialize(type, subtype = "*", parameters = {})
3439
super(type, subtype, parameters)
3540
end
3641

42+
# Compare the media range with another media range or a string, based on the quality factor.
3743
def <=> other
3844
other.quality_factor <=> self.quality_factor
3945
end
@@ -46,12 +52,16 @@ def <=> other
4652
end.join
4753
end
4854

55+
# The string representation of the media range, including the type, subtype, and any parameters.
4956
def to_s
5057
"#{type}/#{subtype}#{parameters_string}"
5158
end
5259

5360
alias to_str to_s
5461

62+
# The quality factor associated with the media range, which is used to determine the order of preference.
63+
#
64+
# @returns [Float] the quality factor, which defaults to 1.0 if not specified.
5565
def quality_factor
5666
parameters.fetch("q", 1.0).to_f
5767
end

test/protocol/http/header/accept.rb

+20-32
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,12 @@
1111
expect(media_range.quality_factor).to be == 1.0
1212
end
1313

14-
with "#===" do
15-
let(:media_range) {subject.new("text", "plain")}
16-
17-
it "can compare with bare string" do
18-
expect(media_range).to be === "text/plain"
19-
end
20-
21-
it "can compare with media range" do
22-
expect(media_range).to be === media_range
23-
end
24-
end
25-
2614
with "#to_s" do
2715
it "can convert to string" do
2816
media_range = subject.new("text", "plain", {"q" => "0.5"})
2917
expect(media_range.to_s).to be == "text/plain;q=0.5"
3018
end
3119
end
32-
33-
with "#split" do
34-
it "can split media range" do
35-
media_range = subject.new("text", "plain", {"q" => "0.5"})
36-
type, subtype = media_range.split
37-
expect(type).to be == "text"
38-
expect(subtype).to be == "plain"
39-
end
40-
end
4120
end
4221

4322
describe Protocol::HTTP::Header::Accept do
@@ -48,13 +27,23 @@
4827
it "can parse media ranges" do
4928
expect(header.length).to be == 3
5029

51-
expect(media_ranges[0].range_string).to be == "text/plain"
52-
expect(media_ranges[0].quality_factor).to be == 1.0
53-
54-
expect(media_ranges[1].range_string).to be == "text/html"
55-
expect(media_ranges[1].quality_factor).to be == 0.5
30+
expect(media_ranges[0]).to have_attributes(
31+
type: be == "text",
32+
subtype: be == "plain",
33+
quality_factor: be == 1.0
34+
)
5635

57-
expect(media_ranges[2].range_string).to be == "text/xml"
36+
expect(media_ranges[1]).to have_attributes(
37+
type: be == "text",
38+
subtype: be == "html",
39+
quality_factor: be == 0.5
40+
)
41+
42+
expect(media_ranges[2]).to have_attributes(
43+
type: be == "text",
44+
subtype: be == "xml",
45+
quality_factor: be == 0.25
46+
)
5847
end
5948

6049
it "can convert to string" do
@@ -70,20 +59,19 @@
7059

7160
with "text/html;q=0.25, text/xml;q=0.5, text/plain" do
7261
it "should order based on quality factor" do
73-
expect(media_ranges.collect(&:range_string)).to be == %w{text/plain text/xml text/html}
62+
expect(media_ranges.collect(&:to_s)).to be == %w{text/plain text/xml;q=0.5 text/html;q=0.25}
7463
end
7564
end
7665

7766
with "text/html, text/plain;q=0.8, text/xml;q=0.6, application/json" do
7867
it "should order based on quality factor" do
79-
expect(media_ranges.collect(&:range_string)).to be == %w{text/html application/json text/plain text/xml}
68+
expect(media_ranges.collect(&:to_s)).to be == %w{text/html application/json text/plain;q=0.8 text/xml;q=0.6}
8069
end
8170
end
8271

83-
with "*/*;q=0" do
72+
with "*/*" do
8473
it "should accept wildcard media range" do
85-
expect(media_ranges[0].range_string).to be == "*/*"
86-
expect(media_ranges[0].quality_factor).to be == 0
74+
expect(media_ranges[0].to_s).to be == "*/*"
8775
end
8876
end
8977

0 commit comments

Comments
 (0)