Best Webmock_ruby code snippet using HTTP.readpartial
body_spec.rb
Source:body_spec.rb
2RSpec.describe HTTP::Response::Body do3 let(:connection) { double(:sequence_id => 0) }4 let(:chunks) { [String.new("Hello, "), String.new("World!")] }5 before do6 allow(connection).to receive(:readpartial) { chunks.shift }7 allow(connection).to receive(:body_completed?) { chunks.empty? }8 end9 subject(:body) { described_class.new(connection, :encoding => Encoding::UTF_8) }10 it "streams bodies from responses" do11 expect(subject.to_s).to eq("Hello, World!")12 end13 context "when body empty" do14 let(:chunks) { [String.new("")] }15 it "returns responds to empty? with true" do16 expect(subject).to be_empty17 end18 end19 describe "#readpartial" do20 context "with size given" do21 it "passes value to underlying connection" do22 expect(connection).to receive(:readpartial).with(42)23 body.readpartial 4224 end25 end26 context "without size given" do27 it "does not blows up" do28 expect { body.readpartial }.to_not raise_error29 end30 it "calls underlying connection readpartial without specific size" do31 expect(connection).to receive(:readpartial).with no_args32 body.readpartial33 end34 end35 it "returns content in specified encoding" do36 body = described_class.new(connection)37 expect(connection).to receive(:readpartial).38 and_return(String.new("content").force_encoding(Encoding::UTF_8))39 expect(body.readpartial.encoding).to eq Encoding::BINARY40 body = described_class.new(connection, :encoding => Encoding::UTF_8)41 expect(connection).to receive(:readpartial).42 and_return(String.new("content").force_encoding(Encoding::BINARY))43 expect(body.readpartial.encoding).to eq Encoding::UTF_844 end45 end46 context "when body is gzipped" do47 let(:chunks) do48 body = Zlib::Deflate.deflate("Hi, HTTP here âº")49 len = body.length50 [String.new(body[0, len / 2]), String.new(body[(len / 2)..-1])]51 end52 subject(:body) do53 inflater = HTTP::Response::Inflater.new(connection)54 described_class.new(inflater, :encoding => Encoding::UTF_8)55 end56 it "decodes body" do57 expect(subject.to_s).to eq("Hi, HTTP here âº")58 end59 describe "#readpartial" do60 it "streams decoded body" do61 [62 "Hi, HTTP ",63 "here âº",64 nil65 ].each do |part|66 expect(subject.readpartial).to eq(part)67 end68 end69 end70 end71end...
connection_spec.rb
Source:connection_spec.rb
...13 let(:connection) { HTTP::Connection.new(req, opts) }14 describe "#read_headers!" do15 before do16 connection.instance_variable_set(:@pending_response, true)17 expect(socket).to receive(:readpartial) do18 <<-RESPONSE.gsub(/^\s*\| */, "").gsub(/\n/, "\r\n")19 | HTTP/1.1 200 OK20 | Content-Type: text21 |22 RESPONSE23 end24 end25 it "reads data in parts" do26 connection.read_headers!27 expect(connection.headers).to eq("Content-Type" => "text")28 end29 end30 describe "#readpartial" do31 before do32 connection.instance_variable_set(:@pending_response, true)33 expect(socket).to receive(:readpartial) do34 <<-RESPONSE.gsub(/^\s*\| */, "").gsub(/\n/, "\r\n")35 | HTTP/1.1 200 OK36 | Content-Type: text37 |38 RESPONSE39 end40 expect(socket).to receive(:readpartial) { "1" }41 expect(socket).to receive(:readpartial) { "23" }42 expect(socket).to receive(:readpartial) { "456" }43 expect(socket).to receive(:readpartial) { "78" }44 expect(socket).to receive(:readpartial) { "9" }45 expect(socket).to receive(:readpartial) { "0" }46 expect(socket).to receive(:readpartial) { :eof }47 expect(socket).to receive(:closed?) { true }48 end49 it "reads data in parts" do50 connection.read_headers!51 buffer = String.new52 while (s = connection.readpartial(3))53 buffer << s54 end55 expect(buffer).to eq "1234567890"56 end57 end58end...
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!!