Sha256: 4d2ee5658c92c6b694c15ba1a1af80e599e4f903efe5395699916f350d60829b

Contents?: true

Size: 1.75 KB

Versions: 36

Compression:

Stored size: 1.75 KB

Contents

require 'core/spec_helper'

describe ZendeskAPI::Middleware::Request::EncodeJson do
  let(:app) do
    ZendeskAPI::Middleware::Request::EncodeJson.new(lambda {|x| x})
  end

  let(:response) { app.call({ :request_headers => {} }.merge(env)) }

  context 'with a nil body' do
    let(:env) {{ :body => nil }}

    it 'should not return json' do
      expect(response[:body]).to be_nil
    end
  end

  context 'with an empty body' do
    let(:env) {{ :body => '' }}

    it 'should not return json' do
      expect(response[:body]).to eq('')
    end
  end

  context 'with a proper mime type' do
    context 'empty' do
      let(:env) {{ :body => { :a => :b } }}

      it 'encodes json' do
        expect(response[:body]).to eq(JSON.dump(:a => :b))
      end

      it 'sets the content type' do
        expect(response[:request_headers]['Content-Type']).to eq('application/json')
      end
    end

    context 'application/json' do
      let(:env) {{
        :body => { :a => :b },
        :request_headers => {
          'Content-Type' => 'application/json'
        }
      }}

      it 'encodes json' do
        expect(response[:body]).to eq(JSON.dump(:a => :b))
      end

      it 'keeps the content type' do
        expect(response[:request_headers]['Content-Type']).to eq('application/json')
      end
    end

    context 'application/json; encoding=utf-8' do
      let(:env) {{
        :body => { :a => :b },
        :request_headers => {
          'Content-Type' => 'application/json; encoding=utf-8'
        }
      }}

      it 'encodes json' do
        expect(response[:body]).to eq(JSON.dump(:a => :b))
      end

      it 'keeps the content type' do
        expect(response[:request_headers]['Content-Type']).to eq('application/json; encoding=utf-8')
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
zendesk_api-1.13.1 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.12.1 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.12.0 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.11.7 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.11.6 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.11.5 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.11.4 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.11.3 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.11.2 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.11.1 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.11.0 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.10.0 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.9.6 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.9.5 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.9.4 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.9.3 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.9.2 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.9.1 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.9.0 spec/core/middleware/request/encode_json_spec.rb
zendesk_api-1.8.0 spec/core/middleware/request/encode_json_spec.rb