Sha256: 6bbc3e2a736599174cd7c69b2f7cba9af34d2910d9491966af155a65a995cc0c

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

require 'pact/consumer/rspec'

Pact.service_consumer "Standalone Consumer" do
  has_pact_with "Standalone Provider" do
    mock_service :standalone_service do
      port 1238
    end
  end
end

class StandaloneClient

  def initialize base_url
    @base_url = base_url
  end

  def call
    uri = URI("#{@base_url}/something")
    post_req = Net::HTTP::Post.new(uri.path)
    post_req['Content-Type'] = "application/json"
    post_req.body = {a: "not matching body"}.to_json
    response = Net::HTTP.start(uri.hostname, uri.port) do |http|
      http.request post_req
    end
    JSON.parse(response.body)
  end

end

describe StandaloneClient, pact: true do

  subject { StandaloneClient.new("http://localhost:1238") }

  describe "call" do

    let(:expected_body) { {a: "body"} }
    let(:expected_headers) { {'Content-Type' => "application/hal+json"} }

    before do
      standalone_service.
        upon_receiving("a request to create something").with(method: 'post', path: '/something', headers: expected_headers, body: expected_body).
        will_respond_with(status: 200, headers: {}, body: {a: 'response body'})

      standalone_service.
        upon_receiving("a request to create something else").with(method: 'post', path: '/something-else', headers: expected_headers, body: expected_body).
        will_respond_with(status: 200, headers: {}, body: {a: 'response body'})
    end

    it "will fail and display a helpful message" do
      subject.call
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pact-1.1.0 spec/standalone/consumer_fail_test.rb
pact-1.1.0.rc5 spec/standalone/consumer_fail_test.rb
pact-1.1.0.rc4 spec/standalone/consumer_fail_test.rb
pact-1.1.0.rc3 spec/standalone/consumer_fail_test.rb