Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Merge pull request #103 from mzahir/fix/unavailable
Browse files Browse the repository at this point in the history
Raise WhmConnectionError when cPanel API is unavailable
  • Loading branch information
Ruy Rocha committed Sep 22, 2015
2 parents ea84209 + c778f15 commit 12af0f3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
10 changes: 9 additions & 1 deletion lib/lumberg/format_whm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ def on_complete(env)
env[:body]
end

env[:body] = format_response body
if body =~ /cPanel operations have been temporarily suspended/
raise Lumberg::WhmConnectionError.new(body)
end

if @type == :whostmgr || response_type(body) == :whostmgr
env[:body] = format_response body
else
env[:body] = format_response JSON.parse(body)
end
end

def response_values(env)
Expand Down
1 change: 0 additions & 1 deletion lib/lumberg/whm/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,6 @@ def do_request(uri, function, params)
c.request :url_encoded
c.response :format_whm, @force_response_type, @response_key, @boolean_params
c.response :logger, create_logger_instance
c.response :json unless @force_response_type == :whostmgr
c.adapter :net_http
c.options[:timeout] = timeout if timeout
end.get(function).body
Expand Down
25 changes: 18 additions & 7 deletions spec/format_whm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

context "gzip" do
let(:gzipped_data) do
"\u001F\x8B\b\u0000\u001E¹R\u0000\u0003+\xCE\xCFMUHI,I\u0004\u0000" +
"\u001E\xE9\xC2\xD9\t\u0000\u0000\u0000"
"\x1F\x8B\b\x00\x13d\x01V\x00\x03\xABV*.MNN-.V\xB2*)*M\xD5Q\xCA" +
"\x05\xB2\x13\xD3S\x95\xAC\x94\x8A\xF3sS\x15R\x12K\x12\x95j\x01" +
"\xA0E\x91\xF9&\x00\x00\x00"
end

it "removes content encoding" do
Expand All @@ -18,13 +19,14 @@

env.should_not include "content-encoding"

env[:body][:params].should eq "some data"
env[:body][:params][:message].should eq "some data"
end
end

context "deflate" do
let(:compressed_data) do
"x\x9C+\xCE\xCFMUHI,I\x04\x00\x11\x81\x03o"
"x\x9C\xABV*.MNN-.V\xB2*)*M\xD5Q\xCA\x05\xB2\x13\xD3S\x95\xAC" +
"\x94\x8A\xF3sS\x15R\x12K\x12\x95j\x01\n\xFE\rq"
end

it "removes content encoding" do
Expand All @@ -35,19 +37,28 @@

env.should_not include "content-encoding"

env[:body][:params].should eq "some data"
env[:body][:params][:message].should eq "some data"
end
end

context "no content encoding" do
it "doesn't touch response headers" do
env = { body: "some data", response_headers: { foo: "bar" } }
env = { body: "{\"success\":true,\"message\":\"some data\"}",
response_headers: { foo: "bar" } }

subject.on_complete(env)

env[:response_headers].should include :foo

env[:body][:params].should eq "some data"
env[:body][:params][:message].should eq "some data"
end
end

context "API unavailable" do
it "raises a Lumberg::WhmConnectionError" do
env = { body: "cPanel operations have been temporarily suspended", response_headers: { foo: "bar" } }

expect { subject.on_complete(env) }.to raise_error(Lumberg::WhmConnectionError)
end
end
end
1 change: 1 addition & 0 deletions spec/whm/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ module Lumberg

before(:each) do
Net::HTTP.stub(:new) { net_http }
JSON.stub(:parse) { { } }
end

it "sets HTTP timeout when assigned" do
Expand Down

0 comments on commit 12af0f3

Please sign in to comment.