Skip to content

Commit

Permalink
Unit tests for content_encoding handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Apr 11, 2022
1 parent 08428ff commit 183e69a
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions spec/sneakers/content_encoding_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
require 'spec_helper'
require 'gzip_helper'
require 'sneakers/content_encoding'
require 'stringio'
require 'zlib'

# Simple gzip encoder/decoder for testing
def gzip_encode(s)
io = StringIO.new('w')
w = Zlib::GzipWriter.new(io)
w.write(s)
w.close
io.string
end

def gzip_decode(s)
Zlib::GzipReader.new(StringIO.new(s, 'rb')).read
end

describe Sneakers::ContentEncoding do
after do
Expand All @@ -12,22 +26,22 @@
Sneakers::ContentEncoding.register(
content_encoding: 'gzip',
encoder: ->(_) {},
decoder: ->(payload) { gzip_decompress(payload) },
decoder: ->(payload) { gzip_decode(payload) },
)

Sneakers::ContentEncoding.decode(gzip_compress('foobar'), 'gzip').must_equal('foobar')
Sneakers::ContentEncoding.decode(gzip_encode('foobar'), 'gzip').must_equal('foobar')
end
end

describe '.encode' do
it 'uses the given encoder' do
Sneakers::ContentEncoding.register(
content_encoding: 'gzip',
encoder: ->(payload) { gzip_compress(payload) },
encoder: ->(payload) { gzip_encode(payload) },
decoder: ->(_) {},
)

gzip_decompress(Sneakers::ContentEncoding.encode('foobar', 'gzip')).must_equal('foobar')
gzip_decode(Sneakers::ContentEncoding.encode('foobar', 'gzip')).must_equal('foobar')
end

it 'passes the payload through by default' do
Expand All @@ -51,12 +65,12 @@
it 'provides a mechnism to register a given encoding' do
Sneakers::ContentEncoding.register(
content_encoding: 'gzip',
encoder: ->(payload) { gzip_compress(payload) },
decoder: ->(payload) { gzip_decompress(payload) },
encoder: ->(payload) { gzip_encode(payload) },
decoder: ->(payload) { gzip_decode(payload) },
)

ce = Sneakers::ContentEncoding
ce.decode(ce.encode('hello world', 'gzip'), 'gzip').must_equal('hello world')
ct = Sneakers::ContentEncoding
ct.decode(ct.encode('hello world', 'gzip'), 'gzip').must_equal('hello world')
end

it 'requires a content encoding' do
Expand Down

0 comments on commit 183e69a

Please sign in to comment.