From 3e9dba01ee99103c9904fa1252aa7caaea1ee901 Mon Sep 17 00:00:00 2001 From: Tiago Date: Tue, 10 Oct 2023 04:31:08 +0100 Subject: [PATCH] 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 --- .../api_client_httpx_partial.mustache | 17 ++++++----------- .../ruby-httpx/lib/openapi_client/api_client.rb | 17 ++++++----------- .../ruby-httpx/lib/petstore/api_client.rb | 17 ++++++----------- .../petstore/ruby-httpx/spec/custom/pet_spec.rb | 6 +++--- 4 files changed, 21 insertions(+), 36 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/ruby-client/api_client_httpx_partial.mustache b/modules/openapi-generator/src/main/resources/ruby-client/api_client_httpx_partial.mustache index a5201fa2ffb..537593df118 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/api_client_httpx_partial.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/api_client_httpx_partial.mustache @@ -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 diff --git a/samples/client/echo_api/ruby-httpx/lib/openapi_client/api_client.rb b/samples/client/echo_api/ruby-httpx/lib/openapi_client/api_client.rb index 9adce447ee7..e3e9c1e0f1d 100644 --- a/samples/client/echo_api/ruby-httpx/lib/openapi_client/api_client.rb +++ b/samples/client/echo_api/ruby-httpx/lib/openapi_client/api_client.rb @@ -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 diff --git a/samples/client/petstore/ruby-httpx/lib/petstore/api_client.rb b/samples/client/petstore/ruby-httpx/lib/petstore/api_client.rb index 8b688192ef1..fe5e25ab387 100644 --- a/samples/client/petstore/ruby-httpx/lib/petstore/api_client.rb +++ b/samples/client/petstore/ruby-httpx/lib/petstore/api_client.rb @@ -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 diff --git a/samples/client/petstore/ruby-httpx/spec/custom/pet_spec.rb b/samples/client/petstore/ruby-httpx/spec/custom/pet_spec.rb index ae2285d7425..f8a08557130 100644 --- a/samples/client/petstore/ruby-httpx/spec/custom/pet_spec.rb +++ b/samples/client/petstore/ruby-httpx/spec/custom/pet_spec.rb @@ -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