File: accept_charset.rb

package info (click to toggle)
ruby-protocol-http 0.59.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 864 kB
  • sloc: ruby: 7,612; makefile: 4
file content (80 lines) | stat: -rw-r--r-- 2,140 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.

require "protocol/http/header/accept_charset"

describe Protocol::HTTP::Header::AcceptCharset::Charset do
	it "should have default quality_factor of 1.0" do
		charset = subject.new("utf-8", nil)
		expect(charset.quality_factor).to be == 1.0
	end
end

describe Protocol::HTTP::Header::AcceptCharset do
	let(:header) {subject.parse(description)}
	let(:charsets) {header.charsets.sort}
	
	with "utf-8, iso-8859-1;q=0.5, windows-1252;q=0.25" do
		it "can parse charsets" do
			expect(header.length).to be == 3
			
			expect(charsets[0].name).to be == "utf-8"
			expect(charsets[0].quality_factor).to be == 1.0
			
			expect(charsets[1].name).to be == "iso-8859-1"
			expect(charsets[1].quality_factor).to be == 0.5
			
			expect(charsets[2].name).to be == "windows-1252"
			expect(charsets[2].quality_factor).to be == 0.25
		end
	end
	
	with "windows-1252;q=0.25, iso-8859-1;q=0.5, utf-8" do
		it "should order based on quality factor" do
			expect(charsets.collect(&:name)).to be == %w{utf-8 iso-8859-1 windows-1252}
		end
	end
	
	with "us-ascii,iso-8859-1;q=0.8,windows-1252;q=0.6,utf-8" do
		it "should order based on quality factor" do
			expect(charsets.collect(&:name)).to be == %w{us-ascii utf-8 iso-8859-1 windows-1252}
		end
	end
	
	with "*;q=0" do
		it "should accept wildcard charset" do
			expect(charsets[0].name).to be == "*"
			expect(charsets[0].quality_factor).to be == 0
		end
	end
	
	with "utf-8, iso-8859-1;q=0.5, windows-1252;q=0.5" do
		it "should preserve relative order" do
			expect(charsets[0].name).to be == "utf-8"
			expect(charsets[1].name).to be == "iso-8859-1"
			expect(charsets[2].name).to be == "windows-1252"
		end
	end
	
	it "should not accept invalid input" do
		bad_values = [
			# Invalid quality factor:
			"utf-8;f=1",
			
			# Invalid parameter:
			"us-ascii;utf-8",
			
			# Invalid use of separator:
			";",
			
			# Empty charset (we ignore this one):
			# ","
		]
		
		bad_values.each do |value|
			expect{subject.parse(value).charsets}.to raise_exception(subject::ParseError)
		end
	end
end