# frozen_string_literal: truebeginrequire'manticore'rescueLoadError# manticore not foundendifdefined?(Manticore)moduleWebMockmoduleHttpLibAdaptersclassManticoreAdapter<HttpLibAdapteradapter_for:manticoreOriginalManticoreClient=Manticore::Clientdefself.enable!Manticore.send(:remove_const,:Client)Manticore.send(:const_set,:Client,WebMockManticoreClient)Manticore.instance_variable_set(:@manticore_facade,WebMockManticoreClient.new)enddefself.disable!Manticore.send(:remove_const,:Client)Manticore.send(:const_set,:Client,OriginalManticoreClient)Manticore.instance_variable_set(:@manticore_facade,OriginalManticoreClient.new)endclassStubbedTimeoutResponse<Manticore::StubbedResponsedefcall@handlers[:failure].call(Manticore::ConnectTimeout.new("Too slow (mocked timeout)"))endendclassWebMockManticoreClient<Manticore::Clientdefrequest(klass,url,options={},&block)super(klass,WebMock::Util::URI.normalize_uri(url).to_s,format_options(options))endprivatedefformat_options(options)returnoptionsunlessheaders=options[:headers]options.merge(headers: join_array_values(headers))enddefjoin_array_values(headers)headers.reduce({})do|h,(k,v)|v=v.join(', ')ifv.is_a?(Array)h.merge(k=>v)endenddefresponse_object_for(request,context,&block)request_signature=generate_webmock_request_signature(request,context)WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)ifwebmock_response=registered_response_for(request_signature)webmock_response.raise_error_if_anymanticore_response=generate_manticore_response(webmock_response)manticore_response.on_successdoWebMock::CallbackRegistry.invoke_callbacks({lib: :manticore,real_request: false},request_signature,webmock_response)endelsifreal_request_allowed?(request_signature.uri)manticore_response=Manticore::Response.new(self,request,context,&block)manticore_response.on_completedo|completed_response|webmock_response=generate_webmock_response(completed_response)WebMock::CallbackRegistry.invoke_callbacks({lib: :manticore,real_request: true},request_signature,webmock_response)endelseraiseWebMock::NetConnectNotAllowedError.new(request_signature)endmanticore_responseenddefregistered_response_for(request_signature)WebMock::StubRegistry.instance.response_for_request(request_signature)enddefreal_request_allowed?(uri)WebMock.net_connect_allowed?(uri)enddefgenerate_webmock_request_signature(request,context)method=request.method.downcaseuri=request.uri.to_sbody=read_body(request)headers=split_array_values(request.headers)ifcontext.get_credentials_provider&&credentials=context.get_credentials_provider.get_credentials(AuthScope::ANY)headers['Authorization']=WebMock::Util::Headers.basic_auth_header(credentials.get_user_name,credentials.get_password)endWebMock::RequestSignature.new(method,uri,{body: body,headers: headers})enddefread_body(request)ifrequest.respond_to?(:entity)&&!request.entity.nil?Manticore::EntityConverter.new.read_entity(request.entity)endenddefsplit_array_values(headers=[])headers.each_with_object({})do|(k,v),h|h[k]=casevwhen/,/thenv.split(',').map(&:strip)elsevendendenddefgenerate_manticore_response(webmock_response)ifwebmock_response.should_timeoutStubbedTimeoutResponse.newelseManticore::StubbedResponse.stub(code: webmock_response.status[0],body: webmock_response.body,headers: webmock_response.headers,cookies: {})endenddefgenerate_webmock_response(manticore_response)webmock_response=WebMock::Response.newwebmock_response.status=[manticore_response.code,manticore_response.message]webmock_response.headers=manticore_response.headers# The attempt to read the body could fail if manticore is used in a streaming modewebmock_response.body=beginmanticore_response.bodyrescue::Manticore::StreamClosedExceptionnilendwebmock_responseendendendendendend