From 990575f3f8aed9f8b1e027016ce13f4b7a2b22ec Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 10 Apr 2017 15:34:28 +1000 Subject: [PATCH] Added HTML content type for request to get a specific version of a pact. As per request in https://github.com/bethesque/pact_broker/issues/82 --- lib/pact_broker/api/resources/pact.rb | 7 +++- .../pact_broker/api/resources/pact_spec.rb | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/pact_broker/api/resources/pact.rb b/lib/pact_broker/api/resources/pact.rb index caaaefd36..a1e24f4a7 100644 --- a/lib/pact_broker/api/resources/pact.rb +++ b/lib/pact_broker/api/resources/pact.rb @@ -24,7 +24,8 @@ class Pact < BaseResource include PacticipantResourceMethods def content_types_provided - [["application/json", :to_json]] + [["application/json", :to_json], + ["text/html", :to_html]] end def content_types_accepted @@ -76,6 +77,10 @@ def to_json PactBroker::Api::Decorators::PactDecorator.new(pact).to_json(user_options: { base_url: base_url }) end + def to_html + PactBroker.configuration.html_pact_renderer.call(pact) + end + def delete_resource pact_service.delete(pact_params) true diff --git a/spec/lib/pact_broker/api/resources/pact_spec.rb b/spec/lib/pact_broker/api/resources/pact_spec.rb index 438fe3344..e96a7caac 100644 --- a/spec/lib/pact_broker/api/resources/pact_spec.rb +++ b/spec/lib/pact_broker/api/resources/pact_spec.rb @@ -2,6 +2,8 @@ require 'pact_broker/api/resources/pact' require 'rack/test' require 'pact_broker/pacts/service' +require 'pact_broker/pacticipants/service' + module PactBroker::Api @@ -14,6 +16,46 @@ module Resources let(:app) { PactBroker::API } let(:json) { {some: 'json'}.to_json } + describe "GET" do + + context "Accept: text/html" do + + let(:json_content) { 'json_content' } + let(:pact) { double("pact", json_content: json_content)} + let(:html) { 'html' } + let(:pact_id_params) { {provider_name: "provider_name", consumer_name: "consumer_name", consumer_version_number: "1.2.3"} } + + before do + allow(PactBroker::Pacts::Service).to receive(:find_pact).and_return(pact) + allow(PactBroker.configuration.html_pact_renderer).to receive(:call).and_return(html) + end + + subject { get "/pacts/provider/provider_name/consumer/consumer_name/versions/1.2.3",{}, {'HTTP_ACCEPT' => "text/html"} } + + it "find the pact" do + expect(PactBroker::Pacts::Service).to receive(:find_pact).with(hash_including(pact_id_params)) + subject + end + + it "uses the configured HTML renderer" do + expect(PactBroker.configuration.html_pact_renderer).to receive(:call).with(pact) + subject + end + + it "returns a HTML body" do + subject + expect(last_response.body).to eq html + end + + it "returns a content type of HTML" do + subject + expect(last_response.headers['Content-Type']).to eq 'text/html;charset=utf-8' + end + + end + end + + shared_examples "an update endpoint" do |http_method| subject { self.send http_method, "/pacts/provider/Provider/consumer/Consumer/version/1.2", json, {'CONTENT_TYPE' => "application/json"} ; last_response }