Best Webmock_ruby code snippet using HTTPMethods.setup
httmultiparty_spec.rb
Source:httmultiparty_spec.rb
...42 describe '#post' do43 it 'should respond to post' do44 expect(klass).to respond_to :post45 end46 it 'should setup new request with Net::HTTP::Post' do47 expect(HTTParty::Request).to receive(:new) \48 .with(Net::HTTP::Post, anything, anything) \49 .and_return(double('mock response', perform: nil))50 klass.post('http://example.com/', {})51 end52 describe 'when :query contains a file' do53 let(:query) { { somefile: somefile } }54 it 'should setup new request with Net::HTTP::Post::Multipart' do55 expect(HTTParty::Request).to receive(:new) \56 .with(HTTMultiParty::MultipartPost, anything, anything) \57 .and_return(double('mock response', perform: nil))58 klass.post('http://example.com/', query: query)59 end60 end61 describe 'when :body contains a file' do62 let(:body) { { somefile: somefile } }63 it 'should setup new request with Net::HTTP::Post::Multipart' do64 expect(HTTParty::Request).to receive(:new) \65 .with(HTTMultiParty::MultipartPost, anything, anything) \66 .and_return(double('mock response', perform: nil))67 klass.post('http://example.com/', body: body)68 end69 end70 describe 'with default_params' do71 let(:body) { { somefile: somefile } }72 it 'should include default_params also' do73 klass.tap do |c|74 c.instance_eval { default_params(token: 'fake') }75 end76 FakeWeb.register_uri(:post, 'http://example.com?token=fake', body: 'hello world')77 klass.post('http://example.com', body: body)78 end79 end80 end81 describe '#patch' do82 it 'should respond to patch' do83 expect(klass).to respond_to :patch84 end85 it 'should setup new request with Net::HTTP::Patch' do86 expect(HTTParty::Request).to receive(:new) \87 .with(Net::HTTP::Patch, anything, anything) \88 .and_return(double('mock response', perform: nil))89 klass.patch('http://example.com/', {})90 end91 describe 'when :query contains a file' do92 let(:query) { { somefile: somefile } }93 it 'should setup new request with Net::HTTP::Patch::Multipart' do94 expect(HTTParty::Request).to receive(:new) \95 .with(HTTMultiParty::MultipartPatch, anything, anything) \96 .and_return(double('mock response', perform: nil))97 klass.patch('http://example.com/', query: query)98 end99 end100 describe 'when :body contains a file' do101 let(:body) { { somefile: somefile } }102 it 'should setup new request with Net::HTTP::Patch::Multipart' do103 expect(HTTParty::Request).to receive(:new) \104 .with(HTTMultiParty::MultipartPatch, anything, anything) \105 .and_return(double('mock response', perform: nil))106 klass.patch('http://example.com/', body: body)107 end108 end109 describe 'with default_params' do110 let(:body) { { somefile: somefile } }111 it 'should include default_params also' do112 klass.tap do |c|113 c.instance_eval { default_params(token: 'fake') }114 end115 FakeWeb.register_uri(:patch, 'http://example.com?token=fake', body: 'hello world')116 klass.patch('http://example.com', body: body)...
http_connection.rb
Source:http_connection.rb
1module EventMachine2 module HTTPMethods3 def get options = {}, &blk; setup_request(:get, options, &blk); end4 def head options = {}, &blk; setup_request(:head, options, &blk); end5 def delete options = {}, &blk; setup_request(:delete, options, &blk); end6 def put options = {}, &blk; setup_request(:put, options, &blk); end7 def post options = {}, &blk; setup_request(:post, options, &blk); end8 def patch options = {}, &blk; setup_request(:patch, options, &blk); end9 def options options = {}, &blk; setup_request(:options, options, &blk); end10 end11 class HttpStubConnection < Connection12 include Deferrable13 attr_reader :parent14 def parent=(p)15 @parent = p16 @parent.conn = self17 end18 def receive_data(data)19 @parent.receive_data data20 end21 def connection_completed22 @parent.connection_completed23 end24 def unbind(reason=nil)25 @parent.unbind(reason)26 end27 end28 class HttpConnection29 include HTTPMethods30 include Socksify31 attr_reader :deferred32 attr_accessor :error, :connopts, :uri, :conn33 def initialize34 @deferred = true35 @middleware = []36 end37 def conn=(c)38 @conn = c39 @deferred = false40 end41 def activate_connection(client)42 begin43 EventMachine.bind_connect(@connopts.bind, @connopts.bind_port, @connopts.host, @connopts.port, HttpStubConnection) do |conn|44 post_init45 @deferred = false46 @conn = conn47 conn.parent = self48 conn.pending_connect_timeout = @connopts.connect_timeout49 conn.comm_inactivity_timeout = @connopts.inactivity_timeout50 end51 finalize_request(client)52 rescue EventMachine::ConnectionError => e53 #54 # Currently, this can only fire on initial connection setup55 # since #connect is a synchronous method. Hence, rescue the exception,56 # and return a failed deferred which fail any client request at next57 # tick. We fail at next tick to keep a consistent API when the newly58 # created HttpClient is failed. This approach has the advantage to59 # remove a state check of @deferred_status after creating a new60 # HttpRequest. The drawback is that users may setup a callback which we61 # know won't be used.62 #63 # Once there is async-DNS, then we'll iterate over the outstanding64 # client requests and fail them in order.65 #66 # Net outcome: failed connection will invoke the same ConnectionError67 # message on the connection deferred, and on the client deferred.68 #69 EM.next_tick{client.close(e.message)}70 end71 end72 def setup_request(method, options = {}, c = nil)73 c ||= HttpClient.new(self, HttpClientOptions.new(@uri, options, method))74 @deferred ? activate_connection(c) : finalize_request(c)75 c76 end77 def finalize_request(c)78 @conn.callback { c.connection_completed }79 middleware.each do |m|80 c.callback &m.method(:response) if m.respond_to?(:response)81 end82 @clients.push c83 end84 def middleware85 [HttpRequest.middleware, @middleware].flatten86 end...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!