Sha256: 087737a65d3a916e4af3f8e9e819e5a024756ea2be84163ddff7194c48c5caf4

Contents?: true

Size: 1.93 KB

Versions: 13

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

RSpec.describe HTTP::Response::Parser do
  subject(:parser) { described_class.new }
  let(:raw_response) do
    "HTTP/1.1 200 OK\r\nContent-Length: 2\r\nContent-Type: application/json\r\nMyHeader: val\r\nEmptyHeader: \r\n\r\n{}"
  end
  let(:expected_headers) do
    {
      "Content-Length" => "2",
      "Content-Type"   => "application/json",
      "MyHeader"       => "val",
      "EmptyHeader"    => ""
    }
  end
  let(:expected_body) { "{}" }

  before do
    parts.each { |part| subject.add(part) }
  end

  context "whole response in one part" do
    let(:parts) { [raw_response] }

    it "parses headers" do
      expect(subject.headers.to_h).to eq(expected_headers)
    end

    it "parses body" do
      expect(subject.read(expected_body.size)).to eq(expected_body)
    end
  end

  context "response in many parts" do
    let(:parts) { raw_response.chars }

    it "parses headers" do
      expect(subject.headers.to_h).to eq(expected_headers)
    end

    it "parses body" do
      expect(subject.read(expected_body.size)).to eq(expected_body)
    end
  end

  context "when got 100 Continue response" do
    let :raw_response do
      "HTTP/1.1 100 Continue\r\n\r\n" \
        "HTTP/1.1 200 OK\r\n" \
        "Content-Length: 12\r\n\r\n" \
        "Hello World!"
    end

    context "when response is feeded in one part" do
      let(:parts) { [raw_response] }

      it "skips to next non-info response" do
        expect(subject.status_code).to eq(200)
        expect(subject.headers).to eq("Content-Length" => "12")
        expect(subject.read(12)).to eq("Hello World!")
      end
    end

    context "when response is feeded in many parts" do
      let(:parts) { raw_response.chars }

      it "skips to next non-info response" do
        expect(subject.status_code).to eq(200)
        expect(subject.headers).to eq("Content-Length" => "12")
        expect(subject.read(12)).to eq("Hello World!")
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
direct7-0.0.18 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/spec/lib/http/response/parser_spec.rb
direct7-0.0.17 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/spec/lib/http/response/parser_spec.rb
direct7-0.0.16 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/spec/lib/http/response/parser_spec.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/http-5.2.0/spec/lib/http/response/parser_spec.rb
direct7-0.0.13 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/spec/lib/http/response/parser_spec.rb
direct7-0.0.12 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/spec/lib/http/response/parser_spec.rb
http-5.2.0 spec/lib/http/response/parser_spec.rb
direct7-0.0.11 vendor/bundle/ruby/2.7.0/gems/http-5.1.1/spec/lib/http/response/parser_spec.rb
http-5.1.1 spec/lib/http/response/parser_spec.rb
http-5.1.0 spec/lib/http/response/parser_spec.rb
http-5.0.4 spec/lib/http/response/parser_spec.rb
http-5.0.3 spec/lib/http/response/parser_spec.rb
http-5.0.2 spec/lib/http/response/parser_spec.rb