Skip to content

Commit

Permalink
improve tests readability
Browse files Browse the repository at this point in the history
  • Loading branch information
akabishau committed Sep 17, 2024
1 parent 17e1cf0 commit 1c2b287
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions spec/services/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
require_relative "../../app/services/http_client"

describe HttpClient do
let(:base_url) { "https://example.com/api/" }
let(:base_url) { "https://example.com/" }
let(:endpoint) { "api/endpoint" }
let(:url) { "#{base_url}#{endpoint}" }
let(:params) { { param1: "value1", param2: "value2" } }
let(:response_body) { { "key" => "value" } }
let(:retries) { 2 }

# Define the webmock stub for GET requests
let(:get_stub_request) { stub_request(:get, url).with(query: params) }

# dynamically create a class for testing
let(:api_client_class) do
Class.new do
Expand Down Expand Up @@ -44,63 +51,55 @@
end

describe "#get" do
let(:endpoint) { "endpoint" }
let(:params) { { param1: "value1", param2: "value2" } }
let(:response_body) { { "key" => "value" } }

context "successful request" do
it "parses and returns JSON response" do
# Stub the HTTP request using WebMock
stub_request(:get, "#{base_url}#{endpoint}")
.with(query: params)
.to_return(
status: 200,
body: response_body.to_json, # simulate a JSON response
headers: { "Content-Type": "application/json" }
)
get_stub_request.to_return(
status: 200,
body: response_body.to_json, # simulate a JSON response
headers: { "Content-Type": "application/json" } # ensure JSON parsing
)

response = api_client.get(endpoint, params)
expect(response).to eq(response_body)
end
end

context "handling errors" do
it "when a 4xx error occur, raises a Faraday::ClientError" do
stub_request(:get, "#{base_url}#{endpoint}")
.with(query: params)
.to_return(status: 404, body: 'Not Found')
it "raises a Faraday::ClientError after no retries for 4xx errors" do
get_stub_request.to_return(status: 403, body: 'Not Found')

expect {
api_client.get(endpoint, params)
}.to raise_error(Faraday::ClientError)

# no retries for client errors
expect(WebMock).to have_requested(:get, url)
.with(query: params)
.times(1) # initial request only
end

it "when a 5xx error occurs, raises a Faraday::ServerError after retries" do
stub_request(:get, "#{base_url}#{endpoint}")
.with(query: params)
.to_return(status: 500, body: 'Internal Server Error')
it "raises a Faraday::ServerError after retries for 5xx errors" do
get_stub_request.to_return(status: 500, body: 'Internal Server Error')

expect {
api_client.get(endpoint, params)
}.to raise_error(Faraday::ServerError)

expect(WebMock).to have_requested(:get, "#{base_url}#{endpoint}")
expect(WebMock).to have_requested(:get, url)
.with(query: params)
.times(1 + retries) # initial request + retries
end
end

context "when a request times out" do
it "retries the request and raises Faraday::TimeoutError after retries" do
stub_request(:get, "#{base_url}#{endpoint}")
.with(query: params)
.to_timeout
get_stub_request.to_timeout

expect {
api_client.get(endpoint, params)
}.to raise_error(Faraday::ConnectionFailed) # final error after retries

expect(WebMock).to have_requested(:get, "#{base_url}#{endpoint}")
expect(WebMock).to have_requested(:get, url)
.with(query: params)
.times(1 + retries) # initial request + retries
end
Expand Down

0 comments on commit 1c2b287

Please sign in to comment.