summaryrefslogtreecommitdiff
path: root/spec/unit/http/validate_content_length_spec.rb
blob: 3b959f29f458cbf3606bf329d4e551121ad81ae3 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#
# Author:: Serdar Sutay (<serdar@chef.io>)
# Copyright:: Copyright 2014-2016, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"
require "stringio"

describe Chef::HTTP::ValidateContentLength do
  class TestClient < Chef::HTTP
    use Chef::HTTP::ValidateContentLength
  end

  let(:method) { "GET" }
  let(:url) { "http://dummy.com" }
  let(:headers) { {} }
  let(:data) { false }

  let(:request) {}
  let(:return_value) { "200" }

  # Test Variables
  let(:request_type) { :streaming }
  let(:content_length_value) { 23 }
  let(:streaming_length) { 23 }
  let(:response_body) { "Thanks for checking in." }
  let(:response_headers) {
    {
      "content-length" => content_length_value
    }
  }

  let(:response) {
    m = double("HttpResponse", :body => response_body)
    allow(m).to receive(:[]) do |key|
      response_headers[key]
    end

    m
  }

  let(:middleware) {
    client = TestClient.new(url)
    client.middlewares[0]
  }

  def run_content_length_validation
    stream_handler = middleware.stream_response_handler(response)
    middleware.handle_request(method, url, headers, data)

    case request_type
    when :streaming
      # First stream the data
      data_length = streaming_length
      while data_length > 0
        chunk_size = data_length > 10 ? 10 : data_length
        stream_handler.handle_chunk(double("Chunk", :bytesize => chunk_size))
        data_length -= chunk_size
      end

      # Finally call stream complete
      middleware.handle_stream_complete(response, request, return_value)
    when :direct
      middleware.handle_response(response, request, return_value)
    else
      raise "Unknown request_type: #{request_type}"
    end
  end

  let(:debug_stream) { StringIO.new }
  let(:debug_output) { debug_stream.string }

  before(:each) do
    @original_log_level = Chef::Log.level
    Chef::Log.level = :debug
    allow(Chef::Log).to receive(:debug) do |message|
      debug_stream.puts message
    end
  end

  after(:each) do
    Chef::Log.level = @original_log_level
  end

  describe "without response body" do
    let(:request_type) { :direct }
    let(:response_body) { "Thanks for checking in." }

    it "shouldn't raise error" do
      expect { run_content_length_validation }.not_to raise_error
    end
  end

  describe "without Content-Length header" do
    let(:response_headers) { {} }

    [ "direct", "streaming" ].each do |req_type|
      describe "when running #{req_type} request" do
        let(:request_type) { req_type.to_sym }

        it "should skip validation and log for debug" do
          run_content_length_validation
          expect(debug_output).to include("HTTP server did not include a Content-Length header in response")
        end
      end
    end
  end

  describe "with correct Content-Length header" do
    [ "direct", "streaming" ].each do |req_type|
      describe "when running #{req_type} request" do
        let(:request_type) { req_type.to_sym }

        it "should validate correctly" do
          run_content_length_validation
          expect(debug_output).to include("Content-Length validated correctly.")
        end
      end
    end
  end

  describe "with wrong Content-Length header" do
    let(:content_length_value) { 25 }
    [ "direct", "streaming" ].each do |req_type|
      describe "when running #{req_type} request" do
        let(:request_type) { req_type.to_sym }

        it "should raise ContentLengthMismatch error" do
          expect { run_content_length_validation }.to raise_error(Chef::Exceptions::ContentLengthMismatch)
        end
      end
    end
  end

  describe "when download is interrupted" do
    let(:streaming_length) { 12 }

    it "should raise ContentLengthMismatch error" do
      expect { run_content_length_validation }.to raise_error(Chef::Exceptions::ContentLengthMismatch)
    end
  end

  describe "when Transfer-Encoding & Content-Length is set" do
    let(:response_headers) {
      {
        "content-length" => content_length_value,
        "transfer-encoding" => "chunked",
      }
    }

    [ "direct", "streaming" ].each do |req_type|
      describe "when running #{req_type} request" do
        let(:request_type) { req_type.to_sym }

        it "should skip validation and log for debug" do
          run_content_length_validation
          expect(debug_output).to include("Transfer-Encoding header is set, skipping Content-Length check.")
        end
      end
    end
  end

  describe "when client is being reused" do
    before do
      run_content_length_validation
      expect(debug_output).to include("Content-Length validated correctly.")
    end

    it "should reset internal counter" do
      expect(middleware.instance_variable_get(:@content_length_counter)).to be_nil
    end

    it "should validate correctly second time" do
      run_content_length_validation
      expect(debug_output).to include("Content-Length validated correctly.")
    end
  end
end