Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Ruby <2.3 base64 decoding workaround #9

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/web_push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ def encode64(bytes)
end

def decode64(str)
# For Ruby < 2.3, Base64.urlsafe_decode64 strict decodes and will raise errors if encoded value is not properly padded
# Implementation: http://ruby-doc.org/stdlib-2.3.0/libdoc/base64/rdoc/Base64.html#method-i-urlsafe_decode64
str = str.ljust((str.length + 3) & ~3, '=') if !str.end_with?('=') && str.length % 4 != 0

Base64.urlsafe_decode64(str)
end

Expand Down
12 changes: 12 additions & 0 deletions spec/web_push_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
expect(WebPush::VERSION).not_to be nil
end

describe '.decode64' do
let(:a_padded_key) { "YWJjZGU=" }

it 'urlsafe decodes padded base64 string' do
expect(WebPush.decode64(a_padded_key)).to eq("abcde")
end

it 'urlsafe decodes unpadded base64 string' do
expect(WebPush.decode64(a_padded_key.delete("="))).to eq("abcde")
end
end

shared_examples 'web push protocol standard error handling' do
it 'raises InvalidSubscription if the API returns a 404 Error' do
stub_request(:post, expected_endpoint)
Expand Down