Skip to content

Commit

Permalink
Typecast header values to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanp committed Feb 9, 2020
1 parent c2c15b7 commit 3ff22f7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pakyow-core/CHANGELOG.md
Expand Up @@ -228,6 +228,11 @@
[d55e932]: https://github.com/pakyow/pakyow/commit/d55e9320dcca51ac7d12d8eef4f7f8aaf8faaa4f
[26f586d]: https://github.com/pakyow/pakyow/commit/26f586d35c5fa0611cac6914fb2f249e3798ec79

# v1.0.4 (unreleased)

* `fix` **Typecast header values to strings.**
- Resolves an incompatibility with `protocol-http`.

# v1.0.3

* `fix` **Resolve several issues with respawns, restarts.**
Expand Down
11 changes: 10 additions & 1 deletion pakyow-core/lib/pakyow/connection.rb
Expand Up @@ -90,7 +90,7 @@ def header(key)
end

def set_header(key, value)
@headers[normalize_header(key)] = value
@headers[normalize_header(key)] = normalize_header_value(value)
end

def set_headers(headers)
Expand Down Expand Up @@ -397,6 +397,15 @@ def normalize_header(key)
key.to_s.downcase.gsub("_", "-")
end

def normalize_header_value(value)
case value
when Array
value.map(&:to_s)
else
value.to_s
end
end

DELETE_COOKIE = {
value: nil, path: nil, domain: nil, max_age: 0, expires: Time.at(0)
}.freeze
Expand Down
4 changes: 2 additions & 2 deletions pakyow-core/lib/pakyow/rack/compatibility.rb
Expand Up @@ -21,7 +21,7 @@ def fullpath
end

def request_header(key)
normalize_header_value(key, @request.get_header(normalize_header(key)))
normalize_header_key_value(key, @request.get_header(normalize_header(key)))
end

def request_header?(key)
Expand Down Expand Up @@ -56,7 +56,7 @@ def normalize_header(key)
key.to_s.upcase.gsub("-", "_")
end

def normalize_header_value(key, value)
def normalize_header_key_value(key, value)
if value && policy = Protocol::HTTP::Headers::MERGE_POLICY[key.to_s.downcase.gsub("_", "-")]
policy.new(value.to_s)
else
Expand Down
14 changes: 14 additions & 0 deletions pakyow-core/spec/unit/connection/shared_examples/headers.rb
Expand Up @@ -107,6 +107,20 @@
expect(connection.header("foo-bar")).to eq("baz")
end
end

context "passed a non-string value" do
it "typecasts to a string" do
connection.set_header("content-length", 5)
expect(connection.header("content-length")).to eq("5")
end
end

context "passed an array of non-string values" do
it "typecasts each value to a string" do
connection.set_header("content-length", [1, 2, 3])
expect(connection.header("content-length")).to eq(["1", "2", "3"])
end
end
end

describe "#set_headers" do
Expand Down

0 comments on commit 3ff22f7

Please sign in to comment.