File: auto_deflate_spec.rb

package info (click to toggle)
ruby-http 4.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704 kB
  • sloc: ruby: 5,388; makefile: 9
file content (77 lines) | stat: -rw-r--r-- 2,349 bytes parent folder | download | duplicates (4)
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
# frozen_string_literal: true

RSpec.describe HTTP::Features::AutoDeflate do
  subject { HTTP::Features::AutoDeflate.new }

  it "raises error for wrong type" do
    expect { HTTP::Features::AutoDeflate.new(:method => :wrong) }.
      to raise_error(HTTP::Error) { |error|
        expect(error.message).to eq("Only gzip and deflate methods are supported")
      }
  end

  it "accepts gzip method" do
    expect(HTTP::Features::AutoDeflate.new(:method => :gzip).method).to eq "gzip"
  end

  it "accepts deflate method" do
    expect(HTTP::Features::AutoDeflate.new(:method => :deflate).method).to eq "deflate"
  end

  it "accepts string as method" do
    expect(HTTP::Features::AutoDeflate.new(:method => "gzip").method).to eq "gzip"
  end

  it "uses gzip by default" do
    expect(subject.method).to eq("gzip")
  end

  describe "#deflated_body" do
    let(:body)          { %w[bees cows] }
    let(:deflated_body) { subject.deflated_body(body) }

    context "when method is gzip" do
      subject { HTTP::Features::AutoDeflate.new(:method => :gzip) }

      it "returns object which yields gzipped content of the given body" do
        io = StringIO.new
        io.set_encoding(Encoding::BINARY)
        gzip = Zlib::GzipWriter.new(io)
        gzip.write("beescows")
        gzip.close
        gzipped = io.string

        expect(deflated_body.each.to_a.join).to eq gzipped
      end

      it "caches compressed content when size is called" do
        io = StringIO.new
        io.set_encoding(Encoding::BINARY)
        gzip = Zlib::GzipWriter.new(io)
        gzip.write("beescows")
        gzip.close
        gzipped = io.string

        expect(deflated_body.size).to eq gzipped.bytesize
        expect(deflated_body.each.to_a.join).to eq gzipped
      end
    end

    context "when method is deflate" do
      subject { HTTP::Features::AutoDeflate.new(:method => :deflate) }

      it "returns object which yields deflated content of the given body" do
        deflated = Zlib::Deflate.deflate("beescows")

        expect(deflated_body.each.to_a.join).to eq deflated
      end

      it "caches compressed content when size is called" do
        deflated = Zlib::Deflate.deflate("beescows")

        expect(deflated_body.size).to eq deflated.bytesize
        expect(deflated_body.each.to_a.join).to eq deflated
      end
    end
  end
end