Skip to content

Commit aafddbf

Browse files
authored
feat: ContentType can take an optional default charset (#54)
Signed-off-by: Daniel Azuma <[email protected]>
1 parent 925deae commit aafddbf

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/cloud_events/content_type.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ class ContentType
2121
# Parse the given header value.
2222
#
2323
# @param string [String] Content-Type header value in RFC 2045 format
24+
# @param default_charset [String] Optional. The charset to use if none is
25+
# specified. Defaults to `us-ascii`.
2426
#
25-
def initialize string
27+
def initialize string, default_charset: nil
2628
@string = string
2729
@media_type = "text"
2830
@subtype_base = @subtype = "plain"
2931
@subtype_format = nil
3032
@params = []
31-
@charset = "us-ascii"
33+
@charset = default_charset || "us-ascii"
3234
@error_message = nil
3335
parse consume_comments string.strip
3436
@canonical_string = "#{@media_type}/#{@subtype}" +

test/test_content_type.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@
2929
assert Ractor.shareable? content_type if defined? Ractor
3030
end
3131

32+
it "defaults to us-ascii charset" do
33+
content_type = CloudEvents::ContentType.new "application/json"
34+
assert_equal "us-ascii", content_type.charset
35+
assert Ractor.shareable? content_type if defined? Ractor
36+
end
37+
38+
it "defaults to a given charset" do
39+
content_type = CloudEvents::ContentType.new "application/json", default_charset: "utf-8"
40+
assert_equal "utf-8", content_type.charset
41+
assert Ractor.shareable? content_type if defined? Ractor
42+
end
43+
3244
it "recognizes charseet param" do
3345
content_type = CloudEvents::ContentType.new "application/json; charset=utf-8"
3446
assert_equal [["charset", "utf-8"]], content_type.params

0 commit comments

Comments
 (0)