add status to the update_pod callback

This commit is contained in:
Benjamin Neff 2016-01-26 04:15:42 +01:00
parent 530b534c42
commit f4f25e8663
3 changed files with 34 additions and 6 deletions

View file

@ -168,6 +168,11 @@ module DiasporaFederation
# After the xml was parsed and processed the gem calls this callback to persist the entity
# @param [DiasporaFederation::Entity] entity the received entity after processing
#
# update_pod
# Update the pod status
# @param [String] url the pod url
# @param [Symbol, Integer] status the error as {Symbol} or the http-status as {Integer} if it was :ok
#
# @see Callbacks#on
#
# @example

View file

@ -61,9 +61,9 @@ module DiasporaFederation
# @param [Typhoeus::Request] request
def prepare_request(request)
request.on_complete do |response|
success = response.success?
DiasporaFederation.callbacks.trigger(:update_pod, pod_url(response.effective_url), success)
DiasporaFederation.callbacks.trigger(:update_pod, pod_url(response.effective_url), status(response))
success = response.success?
log_line = "success=#{success} sender=#{@sender_id} obj=#{@obj_str} url=#{response.effective_url} " \
"message=#{response.return_code} code=#{response.response_code} time=#{response.total_time}"
if success
@ -82,6 +82,10 @@ module DiasporaFederation
def pod_url(url)
URI.parse(url).tap {|uri| uri.path = "/" }.to_s
end
def status(res)
res.return_code == :ok ? res.response_code : res.return_code
end
end
end
end

View file

@ -34,7 +34,7 @@ module DiasporaFederation
describe "#send" do
let(:response) {
Typhoeus::Response.new(
code: 200,
code: 202,
body: "",
time: 0.2,
effective_url: url.sub("http://", "https://"),
@ -68,9 +68,28 @@ module DiasporaFederation
expect(hydra_wrapper.send).to eq([url2])
end
it "calls the update_pod callback for all responses with effective_url and success-status" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(:update_pod, "https://example.org/", true)
expect(DiasporaFederation.callbacks).to receive(:trigger).with(:update_pod, "http://example.com/", false)
it "calls the update_pod callback for all responses with effective_url and status" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(:update_pod, "https://example.org/", 202)
expect(DiasporaFederation.callbacks).to receive(:trigger)
.with(:update_pod, "http://example.com/", :couldnt_resolve_host)
hydra_wrapper.send
end
it "calls the update_pod callback with http status code when there was no error" do
expect(DiasporaFederation.callbacks).to receive(:trigger).with(:update_pod, "https://example.org/", 202)
expect(DiasporaFederation.callbacks).to receive(:trigger).with(:update_pod, "http://example.net/", 404)
allow(DiasporaFederation.callbacks).to receive(:trigger)
not_found = Typhoeus::Response.new(
code: 404,
body: "",
time: 0.2,
effective_url: "http://example.net/",
return_code: :ok
)
Typhoeus.stub("http://example.net/receive/not_found").and_return(not_found)
hydra_wrapper.insert_job("http://example.net/receive/not_found", xml)
hydra_wrapper.send
end