From 2776ff61cb15972c307faa9530a956ac81584dff Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 15 May 2024 16:21:57 +0200 Subject: [PATCH] Add simple rspec tests to json_endpoint (#105) * Add simple rspec tests to json_endpoint * Move json_endpoint_spec to spec folder * Remove async/debug from import --------- Co-authored-by: a.chuzhynov --- spec/consul/async/json_endpoint_spec.rb | 55 +++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 spec/consul/async/json_endpoint_spec.rb diff --git a/spec/consul/async/json_endpoint_spec.rb b/spec/consul/async/json_endpoint_spec.rb new file mode 100644 index 0000000..28d06ae --- /dev/null +++ b/spec/consul/async/json_endpoint_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'rspec' +require 'spec_helper' +require 'consul/async/json_endpoint' +require 'webmock/rspec' + +RSpec.describe Consul::Async do + context 'default parameters' do + it 'request 200' do + mock_url = 'http://perfectly.working.url' + conf = Consul::Async::JSONConfiguration.new(url: mock_url) + default_value = '[]' + + json_endpoint = nil + response_body = %w[a b] + stub_request(:get, mock_url) + .to_return(body: response_body.to_json, status: 200) + EM.run_block do + json_endpoint = Consul::Async::JSONEndpoint.new(conf, mock_url, default_value) + end + expect(json_endpoint.ready?).to eq(true) + expect(json_endpoint.last_result.data).to eq(response_body.to_json) + end + + it 'request 500' do + mock_url = 'http://error.working.url' + conf = Consul::Async::JSONConfiguration.new(url: mock_url) + default_value = '' + + json_endpoint = nil + stub_request(:get, mock_url) + .to_return(body: '', status: 500) + EM.run_block do + json_endpoint = Consul::Async::JSONEndpoint.new(conf, mock_url, default_value) + end + expect(json_endpoint.ready?).to_not eq(true) + expect(json_endpoint.last_result.retry_in).to be_positive + end + + it 'on timeout' do + mock_url = 'http://not.working.url' + conf = Consul::Async::JSONConfiguration.new(url: mock_url) + default_value = '' + + stub_request(:get, mock_url).to_timeout + json_endpoint = nil + EM.run_block do + json_endpoint = Consul::Async::JSONEndpoint.new(conf, mock_url, default_value, enforce_json_200: true) + end + expect(json_endpoint.ready?).to_not eq(true) + expect(json_endpoint.last_result.retry_in).to be_positive + end + end +end