Sha256: dc1a3ef9dc2e15060173e74cda4f7ea131ce7afbecef456d599ea7160f970e2d

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

require 'spec_helper'
require 'pact/mock_service/request_decorator'
require 'pact/consumer_contract/request'


module Pact
  module MockService
    describe RequestDecorator do

      let(:options) { {} }
      let(:body) { {some: "bod"} }
      let(:headers) { {some: "header"} }
      let(:request_params) do
        {
          method: :get,
          query: "param=foo",
          headers: headers,
          path: "/",
          body: body
        }
      end

      let(:request) { Pact::Request::Expected.from_hash(request_params) }

      subject { RequestDecorator.new(request) }

      describe "#to_json" do

        let(:parsed_json) { JSON.parse subject.to_json, symbolize_names: true}

        it "renders the keys in a meaningful order" do
          expect(subject.to_json).to match(/method.*path.*query.*headers.*body/)
        end

        context "with a body specified as a Hash" do
          it "serialises the body as a Hash" do
            expect(parsed_json[:body]).to eq body
          end
        end

        context "with a body specified as a Hash containing a Pact::Term" do
          let(:body) { { some: Pact::Term.new(generate: 'apple', matcher: /a/) } }

          it "serialises the Pact::Term to Ruby specific JSON that is not compatible with pact-specification 1.0.0" do
            expect(subject.to_json).to include "Pact::Term"
          end
        end
      end

      describe "#as_json" do
        context "without options" do
          it "does not include the options key" do
            expect(subject.as_json.key?(:options)).to be false
          end
        end

        context "with options" do
          let(:request_params) do
            {
              method: :get,
              path: "/",
              options: options
            }
          end

          let(:options) { {:opts => 'blah'} }

          it "includes the options in the request hash" do
            expect(subject.as_json[:options]).to eq options
          end
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pact-mock_service-0.2.2 spec/lib/pact/mock_service/request_decorator_spec.rb
pact-mock_service-0.2.1 spec/lib/pact/mock_service/request_decorator_spec.rb
pact-mock_service-0.2.0 spec/lib/pact/mock_service/request_decorator_spec.rb
pact-mock_service-0.1.0 spec/lib/pact/mock_service/request_decorator_spec.rb