diff options
| author | Robert | 2017-08-01 14:44:43 +0200 | 
|---|---|---|
| committer | Robert | 2017-08-01 16:17:16 +0200 | 
| commit | ee75bd1e579ab366eb6cac938f50e7786536472b (patch) | |
| tree | 6b23231644522b945f29e57fc72b743ab41c93c6 /spec/services | |
| parent | f899be88e2b476bae67b5b5bfe2f4a1f7458b24e (diff) | |
| download | chouette-core-ee75bd1e579ab366eb6cac938f50e7786536472b.tar.bz2 | |
Refs: #3507@2h; CR Step 4
- Added tests for HTTPService (regression)
- Removed some dead code (Workbench model spec, Import factory)
- Changed front_end_host to rails_host in config
Diffstat (limited to 'spec/services')
| -rw-r--r-- | spec/services/http_service_spec.rb | 74 | 
1 files changed, 74 insertions, 0 deletions
| diff --git a/spec/services/http_service_spec.rb b/spec/services/http_service_spec.rb new file mode 100644 index 000000000..8c8af480c --- /dev/null +++ b/spec/services/http_service_spec.rb @@ -0,0 +1,74 @@ +RSpec.describe HTTPService do + +  subject{ described_class } + +  %i{host params path result}.each do |param| +    let(param){ double(param) } +  end +  let( :token ){ SecureRandom.hex } + +  let( :faraday_connection ){ double('faraday_connection') } +  let( :headers ){ {} } + + +  context 'get_resource' do +    let( :params ){ double('params') } + +    it 'sets authorization and returns result' do +      expect(Faraday).to receive(:new).with(url: host).and_yield(faraday_connection) +      expect(faraday_connection).to receive(:adapter).with(Faraday.default_adapter) +      expect(faraday_connection).to receive(:headers).and_return headers +      expect(faraday_connection).to receive(:get).with(path, params).and_return(result) + +      expect(subject.get_resource(host: host, path: path, token: token, params: params)).to eq(result) +      expect(headers['Authorization']).to eq( "Token token=#{token.inspect}" ) +    end +  end + +  context 'post_resource' do +    %i{as_name  mime_type name upload_io value}.each do | param | +      let( param ){ double(param) } +    end + +    let( :upload_list ){ [value, mime_type, as_name] } + +    it 'sets authorization and posts data' do +      expect(Faraday::UploadIO).to receive(:new).with(*upload_list).and_return upload_io +      expect(params).to receive(:update).with(name => upload_io) + +      expect(Faraday).to receive(:new).with(url: host).and_yield(faraday_connection) +      expect(faraday_connection).to receive(:adapter).with(Faraday.default_adapter) +      expect(faraday_connection).to receive(:headers).and_return headers +      expect(faraday_connection).to receive(:request).with(:multipart) +      expect(faraday_connection).to receive(:request).with(:url_encoded) + +      expect(faraday_connection).to receive(:post).with(path, params).and_return(result) + +      expect(subject.post_resource( +        host: host, +        path: path, +        token: token, +        params: params, +        upload: {name => upload_list} )).to eq(result) +      expect(headers['Authorization']).to eq( "Token token=#{token.inspect}" ) +    end + +  end + +  context 'get_json_resource' do + +    let( :content ){ SecureRandom.hex } + +    it 'delegates an parses the response' do +      expect_it.to receive(:get_resource) +        .with(host: host, path: path, token: token, params: params) +        .and_return(double(body: {content: content}.to_json, status: 200)) + +      expect( subject.get_json_resource( +        host: host, +        path: path, +        token: token, +        params: params) ).to eq('content' => content) +    end +  end +end | 
