Sha256: a924841ff2087c3288c07eb0823a11fa4cecc8f79865eb1e79e8003ad064d71f

Contents?: true

Size: 1.7 KB

Versions: 8

Compression:

Stored size: 1.7 KB

Contents

require 'rails_helper'

describe LHC::Error do
  context 'to_s' do
    let(:invalid) { "in\xc3lid".force_encoding('ASCII-8BIT') }
    let(:valid) { "vælid" }

    context 'check assumptions' do
      it 'joining raises an error' do
        expect { [valid, invalid].join }.to raise_error Encoding::CompatibilityError
      end
      it 'interpolation raises an error' do
        expect { "#{valid} #{invalid}" }.to raise_error Encoding::CompatibilityError
      end
      it 'to_json on an array raises an error' do
        expect { [valid, invalid].to_json }.to raise_error Encoding::UndefinedConversionError
      end

      it 'to_s on a hash does not raise an error' do
        expect { { valid: valid, invalid: invalid }.to_s }.not_to raise_error
      end

      it 'to_json on a hash does raise an error' do
        expect { { valid: valid, invalid: invalid }.to_json }.to raise_error Encoding::UndefinedConversionError
      end
    end

    it 'invalid body, valid message' do
      stub_request(:get, 'http://local.ch')
        .to_return(status: 200, body: "{ text : '#{invalid}' }")
      response = LHC.get('http://local.ch')
      expect { LHC::Error.new(valid, response).to_s }.not_to raise_error # Encoding::CompatibilityError
    end

    it 'valid body, invalid message' do
      stub_request(:get, 'http://local.ch')
        .to_return(status: 200, body: "{ text : '#{valid}' }")
      response = LHC.get('http://local.ch')
      expect { LHC::Error.new(invalid, response).to_s }.not_to raise_error # Encoding::CompatibilityError
    end
    # the other cases cannot be tested (for example what happens if the headers contain invalid data)
    # because the mocking framework triggers the encoding error already
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lhc-5.0.3 spec/error/to_s_spec.rb
lhc-5.0.2 spec/error/to_s_spec.rb
lhc-5.0.1 spec/error/to_s_spec.rb
lhc-5.0.0 spec/error/to_s_spec.rb
lhc-4.0.2 spec/error/to_s_spec.rb
lhc-4.0.1 spec/error/to_s_spec.rb
lhc-4.0.0 spec/error/to_s_spec.rb
lhc-3.8.1 spec/error/to_s_spec.rb