Fix ruby httpx test (#16765)

* fix setup of body params (may be nil)

* fix httpx adapter issues

treating DNS resolution errors as ConnectionFailed; removing multipart header set by the openapi-generated code, as it does not contain boundary, and interferes with the generation from httpx, which appropriately deals with mime-types already
This commit is contained in:
Tiago 2023-10-10 04:31:08 +01:00 committed by GitHub
parent 23c5db602f
commit 3e9dba01ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 36 deletions

View File

@ -19,7 +19,7 @@
Net::HTTP::STATUS_CODES.fetch(response.status, "HTTP Error (#{response.status})")
rescue HTTPX::TimeoutError
fail ApiError.new('Connection timed out')
rescue HTTPX::ConnectionError
rescue HTTPX::ConnectionError, HTTPX::ResolveError
fail ApiError.new('Connection failed')
end
@ -54,13 +54,11 @@
if config.debugging
config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
end
else
body_params = {}
end
req_opts = {
:headers => HTTPX::Headers.new(header_params),
**body_params
:headers => HTTPX::Headers.new(header_params)
}
req_opts.merge!(body_params) if body_params
req_opts[:params] = query_params if query_params && !query_params.empty?
session.request(http_method, url, **req_opts)
end
@ -75,14 +73,11 @@
# http form
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
header_params['Content-Type'] == 'multipart/form-data'
data = { form: form_params }
header_params.delete('Content-Type') # httpx takes care of this
{ form: form_params }
elsif body
data = body.is_a?(String) ? { body: body } : { json: body }
else
data = nil
body.is_a?(String) ? { body: body } : { json: body }
end
data
end
def session

View File

@ -65,7 +65,7 @@ module OpenapiClient
Net::HTTP::STATUS_CODES.fetch(response.status, "HTTP Error (#{response.status})")
rescue HTTPX::TimeoutError
fail ApiError.new('Connection timed out')
rescue HTTPX::ConnectionError
rescue HTTPX::ConnectionError, HTTPX::ResolveError
fail ApiError.new('Connection failed')
end
@ -100,13 +100,11 @@ module OpenapiClient
if config.debugging
config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
end
else
body_params = {}
end
req_opts = {
:headers => HTTPX::Headers.new(header_params),
**body_params
:headers => HTTPX::Headers.new(header_params)
}
req_opts.merge!(body_params) if body_params
req_opts[:params] = query_params if query_params && !query_params.empty?
session.request(http_method, url, **req_opts)
end
@ -121,14 +119,11 @@ module OpenapiClient
# http form
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
header_params['Content-Type'] == 'multipart/form-data'
data = { form: form_params }
header_params.delete('Content-Type') # httpx takes care of this
{ form: form_params }
elsif body
data = body.is_a?(String) ? { body: body } : { json: body }
else
data = nil
body.is_a?(String) ? { body: body } : { json: body }
end
data
end
def session

View File

@ -65,7 +65,7 @@ module Petstore
Net::HTTP::STATUS_CODES.fetch(response.status, "HTTP Error (#{response.status})")
rescue HTTPX::TimeoutError
fail ApiError.new('Connection timed out')
rescue HTTPX::ConnectionError
rescue HTTPX::ConnectionError, HTTPX::ResolveError
fail ApiError.new('Connection failed')
end
@ -100,13 +100,11 @@ module Petstore
if config.debugging
config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
end
else
body_params = {}
end
req_opts = {
:headers => HTTPX::Headers.new(header_params),
**body_params
:headers => HTTPX::Headers.new(header_params)
}
req_opts.merge!(body_params) if body_params
req_opts[:params] = query_params if query_params && !query_params.empty?
session.request(http_method, url, **req_opts)
end
@ -121,14 +119,11 @@ module Petstore
# http form
if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
header_params['Content-Type'] == 'multipart/form-data'
data = { form: form_params }
header_params.delete('Content-Type') # httpx takes care of this
{ form: form_params }
elsif body
data = body.is_a?(String) ? { body: body } : { json: body }
else
data = nil
body.is_a?(String) ? { body: body } : { json: body }
end
data
end
def session

View File

@ -106,7 +106,7 @@ describe "Pet" do
it "should fetch a pet object with http info" do
pet, status_code, headers = @pet_api.get_pet_by_id_with_http_info(@pet_id)
expect(status_code).to eq(200)
expect(headers['Content-Type']).to eq('application/json')
expect(headers['content-type']).to eq('application/json')
expect(pet).to be_a(Petstore::Pet)
expect(pet.id).to eq(@pet_id)
expect(pet.name).to eq("RUBY UNIT TESTING")
@ -123,8 +123,8 @@ describe "Pet" do
# skip the check as the response contains a timestamp that changes on every response
# expect(e.message).to eq("Error message: the server returns an error\nHTTP status code: 404\nResponse headers: {\"Date\"=>\"Tue, 26 Feb 2019 04:35:40 GMT\", \"Access-Control-Allow-Origin\"=>\"*\", \"Access-Control-Allow-Methods\"=>\"GET, POST, DELETE, PUT\", \"Access-Control-Allow-Headers\"=>\"Content-Type, api_key, Authorization\", \"Content-Type\"=>\"application/json\", \"Connection\"=>\"close\", \"Server\"=>\"Jetty(9.2.9.v20150224)\"}\nResponse body: {\"code\":1,\"type\":\"error\",\"message\":\"Pet not found\"}")
expect(e.response_body).to eq('{"code":1,"type":"error","message":"Pet not found"}')
expect(e.response_headers).to include('Content-Type')
expect(e.response_headers['Content-Type']).to eq('application/json')
expect(e.response_headers).to include('content-type')
expect(e.response_headers['content-type']).to eq('application/json')
end
end