github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/ruby-client/api_client_spec.mustache (about)

     1  require 'spec_helper'
     2  
     3  describe {{moduleName}}::ApiClient do
     4    context 'initialization' do
     5      context 'URL stuff' do
     6        context 'host' do
     7          it 'removes http from host' do
     8            {{moduleName}}.configure { |c| c.host = 'http://example.com' }
     9            expect({{moduleName}}::Configuration.default.host).to eq('example.com')
    10          end
    11  
    12          it 'removes https from host' do
    13            {{moduleName}}.configure { |c| c.host = 'https://wookiee.com' }
    14            expect({{moduleName}}::ApiClient.default.config.host).to eq('wookiee.com')
    15          end
    16  
    17          it 'removes trailing path from host' do
    18            {{moduleName}}.configure { |c| c.host = 'hobo.com/v4' }
    19            expect({{moduleName}}::Configuration.default.host).to eq('hobo.com')
    20          end
    21        end
    22  
    23        context 'base_path' do
    24          it "prepends a slash to base_path" do
    25            {{moduleName}}.configure { |c| c.base_path = 'v4/dog' }
    26            expect({{moduleName}}::Configuration.default.base_path).to eq('/v4/dog')
    27          end
    28  
    29          it "doesn't prepend a slash if one is already there" do
    30            {{moduleName}}.configure { |c| c.base_path = '/v4/dog' }
    31            expect({{moduleName}}::Configuration.default.base_path).to eq('/v4/dog')
    32          end
    33  
    34          it "ends up as a blank string if nil" do
    35            {{moduleName}}.configure { |c| c.base_path = nil }
    36            expect({{moduleName}}::Configuration.default.base_path).to eq('')
    37          end
    38        end
    39      end
    40    end
    41  
    42  {{^isFaraday}}
    43    describe 'params_encoding in #build_request' do
    44      let(:config) { {{moduleName}}::Configuration.new }
    45      let(:api_client) { {{moduleName}}::ApiClient.new(config) }
    46  
    47      it 'defaults to nil' do
    48        expect({{moduleName}}::Configuration.default.params_encoding).to eq(nil)
    49        expect(config.params_encoding).to eq(nil)
    50  
    51        request = api_client.build_request(:get, '/test')
    52        expect(request.options[:params_encoding]).to eq(nil)
    53      end
    54  
    55      it 'can be customized' do
    56        config.params_encoding = :multi
    57        request = api_client.build_request(:get, '/test')
    58        expect(request.options[:params_encoding]).to eq(:multi)
    59      end
    60    end
    61  
    62    describe 'timeout in #build_request' do
    63      let(:config) { {{moduleName}}::Configuration.new }
    64      let(:api_client) { {{moduleName}}::ApiClient.new(config) }
    65  
    66      it 'defaults to 0' do
    67        expect({{moduleName}}::Configuration.default.timeout).to eq(0)
    68        expect(config.timeout).to eq(0)
    69  
    70        request = api_client.build_request(:get, '/test')
    71        expect(request.options[:timeout]).to eq(0)
    72      end
    73  
    74      it 'can be customized' do
    75        config.timeout = 100
    76        request = api_client.build_request(:get, '/test')
    77        expect(request.options[:timeout]).to eq(100)
    78      end
    79    end
    80  
    81  {{/isFaraday}}
    82    describe '#deserialize' do
    83      it "handles Array<Integer>" do
    84        api_client = {{moduleName}}::ApiClient.new
    85        headers = { 'Content-Type' => 'application/json' }
    86        response = double('response', headers: headers, body: '[12, 34]')
    87        data = api_client.deserialize(response, 'Array<Integer>')
    88        expect(data).to be_instance_of(Array)
    89        expect(data).to eq([12, 34])
    90      end
    91  
    92      it 'handles Array<Array<Integer>>' do
    93        api_client = {{moduleName}}::ApiClient.new
    94        headers = { 'Content-Type' => 'application/json' }
    95        response = double('response', headers: headers, body: '[[12, 34], [56]]')
    96        data = api_client.deserialize(response, 'Array<Array<Integer>>')
    97        expect(data).to be_instance_of(Array)
    98        expect(data).to eq([[12, 34], [56]])
    99      end
   100  
   101      it 'handles Hash<String, String>' do
   102        api_client = {{moduleName}}::ApiClient.new
   103        headers = { 'Content-Type' => 'application/json' }
   104        response = double('response', headers: headers, body: '{"message": "Hello"}')
   105        data = api_client.deserialize(response, 'Hash<String, String>')
   106        expect(data).to be_instance_of(Hash)
   107        expect(data).to eq(:message => 'Hello')
   108      end
   109    end
   110  
   111    describe "#object_to_hash" do
   112      it 'ignores nils and includes empty arrays' do
   113        # uncomment below to test object_to_hash for model
   114        # api_client = {{moduleName}}::ApiClient.new
   115        # _model = {{moduleName}}::ModelName.new
   116        # update the model attribute below
   117        # _model.id = 1
   118        # update the expected value (hash) below
   119        # expected = {id: 1, name: '', tags: []}
   120        # expect(api_client.object_to_hash(_model)).to eq(expected)
   121      end
   122    end
   123  
   124    describe '#build_collection_param' do
   125      let(:param) { ['aa', 'bb', 'cc'] }
   126      let(:api_client) { {{moduleName}}::ApiClient.new }
   127  
   128      it 'works for csv' do
   129        expect(api_client.build_collection_param(param, :csv)).to eq('aa,bb,cc')
   130      end
   131  
   132      it 'works for ssv' do
   133        expect(api_client.build_collection_param(param, :ssv)).to eq('aa bb cc')
   134      end
   135  
   136      it 'works for tsv' do
   137        expect(api_client.build_collection_param(param, :tsv)).to eq("aa\tbb\tcc")
   138      end
   139  
   140      it 'works for pipes' do
   141        expect(api_client.build_collection_param(param, :pipes)).to eq('aa|bb|cc')
   142      end
   143  
   144      it 'works for multi' do
   145        expect(api_client.build_collection_param(param, :multi)).to eq(['aa', 'bb', 'cc'])
   146      end
   147  
   148      it 'fails for invalid collection format' do
   149        expect { api_client.build_collection_param(param, :INVALID) }.to raise_error(RuntimeError, 'unknown collection format: :INVALID')
   150      end
   151    end
   152  
   153    describe '#json_mime?' do
   154      let(:api_client) { {{moduleName}}::ApiClient.new }
   155  
   156      it 'works' do
   157        expect(api_client.json_mime?(nil)).to eq false
   158        expect(api_client.json_mime?('')).to eq false
   159  
   160        expect(api_client.json_mime?('application/json')).to eq true
   161        expect(api_client.json_mime?('application/json; charset=UTF8')).to eq true
   162        expect(api_client.json_mime?('APPLICATION/JSON')).to eq true
   163  
   164        expect(api_client.json_mime?('application/xml')).to eq false
   165        expect(api_client.json_mime?('text/plain')).to eq false
   166        expect(api_client.json_mime?('application/jsonp')).to eq false
   167      end
   168    end
   169  
   170    describe '#select_header_accept' do
   171      let(:api_client) { {{moduleName}}::ApiClient.new }
   172  
   173      it 'works' do
   174        expect(api_client.select_header_accept(nil)).to be_nil
   175        expect(api_client.select_header_accept([])).to be_nil
   176  
   177        expect(api_client.select_header_accept(['application/json'])).to eq('application/json')
   178        expect(api_client.select_header_accept(['application/xml', 'application/json; charset=UTF8'])).to eq('application/json; charset=UTF8')
   179        expect(api_client.select_header_accept(['APPLICATION/JSON', 'text/html'])).to eq('APPLICATION/JSON')
   180  
   181        expect(api_client.select_header_accept(['application/xml'])).to eq('application/xml')
   182        expect(api_client.select_header_accept(['text/html', 'application/xml'])).to eq('text/html,application/xml')
   183      end
   184    end
   185  
   186    describe '#select_header_content_type' do
   187      let(:api_client) { {{moduleName}}::ApiClient.new }
   188  
   189      it 'works' do
   190        expect(api_client.select_header_content_type(nil)).to eq('application/json')
   191        expect(api_client.select_header_content_type([])).to eq('application/json')
   192  
   193        expect(api_client.select_header_content_type(['application/json'])).to eq('application/json')
   194        expect(api_client.select_header_content_type(['application/xml', 'application/json; charset=UTF8'])).to eq('application/json; charset=UTF8')
   195        expect(api_client.select_header_content_type(['APPLICATION/JSON', 'text/html'])).to eq('APPLICATION/JSON')
   196        expect(api_client.select_header_content_type(['application/xml'])).to eq('application/xml')
   197        expect(api_client.select_header_content_type(['text/plain', 'application/xml'])).to eq('text/plain')
   198      end
   199    end
   200  
   201    describe '#sanitize_filename' do
   202      let(:api_client) { {{moduleName}}::ApiClient.new }
   203  
   204      it 'works' do
   205        expect(api_client.sanitize_filename('sun')).to eq('sun')
   206        expect(api_client.sanitize_filename('sun.gif')).to eq('sun.gif')
   207        expect(api_client.sanitize_filename('../sun.gif')).to eq('sun.gif')
   208        expect(api_client.sanitize_filename('/var/tmp/sun.gif')).to eq('sun.gif')
   209        expect(api_client.sanitize_filename('./sun.gif')).to eq('sun.gif')
   210        expect(api_client.sanitize_filename('..\sun.gif')).to eq('sun.gif')
   211        expect(api_client.sanitize_filename('\var\tmp\sun.gif')).to eq('sun.gif')
   212        expect(api_client.sanitize_filename('c:\var\tmp\sun.gif')).to eq('sun.gif')
   213        expect(api_client.sanitize_filename('.\sun.gif')).to eq('sun.gif')
   214      end
   215    end
   216  end