|
| 1 | +require 'spec_helper' |
| 2 | +require 'pact_broker/models/webhook_request' |
| 3 | +require 'webmock/rspec' |
| 4 | + |
| 5 | +module PactBroker |
| 6 | + |
| 7 | + module Models |
| 8 | + |
| 9 | + describe WebhookRequest do |
| 10 | + |
| 11 | + subject { WebhookRequest.new(method: 'POST', |
| 12 | + url: 'http://example.org/hook', |
| 13 | + headers: [WebhookRequestHeader.new('Content-type', 'text/plain')], |
| 14 | + body: 'body')} |
| 15 | + |
| 16 | + describe "execute" do |
| 17 | + |
| 18 | + let!(:http_request) do |
| 19 | + stub_request(:post, "http://example.org/hook"). |
| 20 | + with(:headers => {'Content-Type'=>'text/plain'}, :body => 'body'). |
| 21 | + to_return(:status => 200, :body => "respbod", :headers => {}) |
| 22 | + end |
| 23 | + |
| 24 | + it "executes the configured request" do |
| 25 | + subject.execute |
| 26 | + expect(http_request).to have_been_made |
| 27 | + end |
| 28 | + |
| 29 | + it "logs the request" do |
| 30 | + allow(PactBroker.logger).to receive(:info) |
| 31 | + expect(PactBroker.logger).to receive(:info).with(/POST.*example.*text.*body/) |
| 32 | + subject.execute |
| 33 | + end |
| 34 | + |
| 35 | + it "logs the response" do |
| 36 | + allow(PactBroker.logger).to receive(:info) |
| 37 | + expect(PactBroker.logger).to receive(:info).with(/response.*200.*respbod/) |
| 38 | + subject.execute |
| 39 | + end |
| 40 | + |
| 41 | + context "when the request is successful" do |
| 42 | + it "returns true" do |
| 43 | + expect(subject.execute).to be true |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context "when the request is not successful" do |
| 48 | + |
| 49 | + let!(:http_request) do |
| 50 | + stub_request(:post, "http://example.org/hook"). |
| 51 | + with(:headers => {'Content-Type'=>'text/plain'}, :body => 'body'). |
| 52 | + to_return(:status => 500, :body => "An error") |
| 53 | + end |
| 54 | + |
| 55 | + it "raises an error" do |
| 56 | + expect { subject.execute }.to raise_error WebhookRequestError, /500.*An error/ |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + end |
| 61 | + |
| 62 | + end |
| 63 | + |
| 64 | + end |
| 65 | + |
| 66 | +end |
0 commit comments