File: response_spec.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (106 lines) | stat: -rw-r--r-- 3,865 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
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
require 'spec_helper'
require 'puppet_spec/files'
require 'puppet_spec/handler'
require 'puppet/network/http'

describe Puppet::Network::HTTP::Response do
  include PuppetSpec::Files

  let(:handler) { PuppetSpec::Handler.new }
  let(:response) { {} }
  let(:subject) { described_class.new(handler, response) }
  let(:body_utf8) { JSON.dump({ "foo" => "bar"}).encode('UTF-8') }
  let(:body_shift_jis) { [130, 174].pack('C*').force_encoding(Encoding::Shift_JIS) }
  let(:invalid_shift_jis) { "\xC0\xFF".force_encoding(Encoding::Shift_JIS) }

  context "when passed a response body" do
    it "passes the status code and body to the handler" do
      expect(handler).to receive(:set_response).with(response, body_utf8, 200)

      subject.respond_with(200, 'application/json', body_utf8)
    end

    it "accepts a File body" do
      file = tmpfile('response_spec')
      expect(handler).to receive(:set_response).with(response, file, 200)

      subject.respond_with(200, 'application/octet-stream', file)
    end
  end

  context "when passed a content type" do
    it "accepts a mime string" do
      expect(handler).to receive(:set_content_type).with(response, 'application/json; charset=utf-8')

      subject.respond_with(200, 'application/json', body_utf8)
    end

    it "accepts a format object" do
      formatter = Puppet::Network::FormatHandler.format(:json)
      expect(handler).to receive(:set_content_type).with(response, 'application/json; charset=utf-8')

      subject.respond_with(200, formatter, body_utf8)
    end
  end

  context "when resolving charset" do
    context "with binary content" do
      it "omits the charset" do
        body_binary = [0xDEADCAFE].pack('L')

        formatter = Puppet::Network::FormatHandler.format(:binary)
        expect(handler).to receive(:set_content_type).with(response, 'application/octet-stream')

        subject.respond_with(200, formatter, body_binary)
      end
    end

    context "with text/plain content" do
      let(:formatter) { Puppet::Network::FormatHandler.format(:s) }

      it "sets the charset to UTF-8 for content already in that format" do
        body_pem = "BEGIN CERTIFICATE".encode('UTF-8')

        expect(handler).to receive(:set_content_type).with(response, 'text/plain; charset=utf-8')

        subject.respond_with(200, formatter, body_pem)
      end

      it "encodes the content to UTF-8 for content not already in UTF-8" do
        expect(handler).to receive(:set_content_type).with(response, 'text/plain; charset=utf-8')
        expect(handler).to receive(:set_response).with(response, body_shift_jis.encode('utf-8'), 200)

        subject.respond_with(200, formatter, body_shift_jis)
      end

      it "raises an exception if transcoding fails" do
        expect {
          subject.respond_with(200, formatter, invalid_shift_jis)
        }.to raise_error(EncodingError, /"\\xFF" on Shift_JIS/)
      end
    end

    context "with application/json content" do
      let(:formatter) { Puppet::Network::FormatHandler.format(:json) }

      it "sets the charset to UTF-8 for content already in that format" do
        expect(handler).to receive(:set_content_type).with(response, 'application/json; charset=utf-8')

        subject.respond_with(200, formatter, body_utf8)
      end

      it "encodes the content to UTF-8 for content not already in UTF-8" do
        expect(handler).to receive(:set_content_type).with(response, 'application/json; charset=utf-8')
        expect(handler).to receive(:set_response).with(response, body_shift_jis.encode('utf-8'), 200)

        subject.respond_with(200, formatter, body_shift_jis)
      end

      it "raises an exception if transcoding fails" do
        expect {
          subject.respond_with(200, formatter, invalid_shift_jis)
        }.to raise_error(EncodingError, /"\\xFF" on Shift_JIS/)
      end
    end
  end
end