File: writer_spec.rb

package info (click to toggle)
ruby-http 1.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 520 kB
  • ctags: 322
  • sloc: ruby: 3,934; makefile: 8
file content (89 lines) | stat: -rw-r--r-- 2,585 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
# coding: utf-8

RSpec.describe HTTP::Request::Writer do
  let(:io)          { StringIO.new }
  let(:body)        { "" }
  let(:headers)     { HTTP::Headers.new }
  let(:headerstart) { "GET /test HTTP/1.1" }

  subject(:writer)  { described_class.new(io, body, headers, headerstart) }

  describe "#initalize" do
    context "when body is nil" do
      let(:body) { nil }

      it "does not raise an error" do
        expect { writer }.not_to raise_error
      end
    end

    context "when body is a string" do
      let(:body) { "string body" }

      it "does not raise an error" do
        expect { writer }.not_to raise_error
      end
    end

    context "when body is an Enumerable" do
      let(:body) { %w(bees cows) }

      it "does not raise an error" do
        expect { writer }.not_to raise_error
      end
    end

    context "when body is not string, enumerable or nil" do
      let(:body) { 123 }

      it "raises an error" do
        expect { writer }.to raise_error(HTTP::RequestError)
      end
    end
  end

  describe "#stream" do
    context "when body is Enumerable" do
      let(:body)    { %w(bees cows) }
      let(:headers) { HTTP::Headers.coerce "Transfer-Encoding" => "chunked" }

      it "writes a chunked request from an Enumerable correctly" do
        writer.stream
        expect(io.string).to end_with "\r\n4\r\nbees\r\n4\r\ncows\r\n0\r\n\r\n"
      end

      it "writes Transfer-Encoding header only once" do
        writer.stream
        expect(io.string).to start_with "#{headerstart}\r\nTransfer-Encoding: chunked\r\n\r\n"
      end

      context "when Transfer-Encoding not set" do
        let(:headers) { HTTP::Headers.new }
        specify { expect { writer.stream }.to raise_error(HTTP::RequestError) }
      end

      context "when Transfer-Encoding is not chunked" do
        let(:headers) { HTTP::Headers.coerce "Transfer-Encoding" => "gzip" }
        specify { expect { writer.stream }.to raise_error(HTTP::RequestError) }
      end
    end

    context "when body is a unicode String" do
      let(:body) { "Привет, мир!" }

      it "properly calculates Content-Length if needed" do
        writer.stream
        expect(io.string).to start_with "#{headerstart}\r\nContent-Length: 21\r\n\r\n"
      end

      context "when Content-Length explicitly set" do
        let(:headers) { HTTP::Headers.coerce "Content-Length" => 12 }

        it "keeps given value" do
          writer.stream
          expect(io.string).to start_with "#{headerstart}\r\nContent-Length: 12\r\n\r\n"
        end
      end
    end
  end
end