Skip to content

Commit

Permalink
test Request class
Browse files Browse the repository at this point in the history
  • Loading branch information
NARKOZ committed May 11, 2014
1 parent 3bf0363 commit 672a8d5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/gitlab/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,30 @@ def validate(response)
response.parsed_response
end

# Sets a private_token parameter for requests.
# @raise [Error::MissingCredentials] if private_token not set.
def set_private_token_param(options, path=nil)
# session doesn't require private_token param
return if path == '/session'

raise Error::MissingCredentials.new("Please set a private_token for user") unless @private_token
private_token_param = {:private_token => @private_token}
options[:query] = options[:query] ? options[:query].merge(private_token_param) : private_token_param
end

# Sets a base_uri and default_params for requests.
# @raise [Error::MissingCredentials] if endpoint not set.
def set_request_defaults(endpoint, private_token, sudo=nil)
raise Error::MissingCredentials.new("Please set an endpoint to API") unless endpoint
@private_token = private_token

self.class.base_uri endpoint
self.class.default_params :sudo => sudo unless sudo.nil?
self.class.default_params :sudo => sudo
self.class.default_params.delete(:sudo) if sudo.nil?
end

private

# Sets a private_token parameter for requests.
# @raise [Error::MissingCredentials] if private_token not set.
def set_private_token_param(options, path=nil)
# session doesn't require private_token param
return if path == '/session'

raise Error::MissingCredentials.new("Please set a private_token for user") unless @private_token
private_token_param = {:private_token => @private_token}
options[:query] = options[:query] ? options[:query].merge(private_token_param) : private_token_param
end

def error_message(response)
"Server responded with code #{response.code}, message: #{response.parsed_response.message}. " \
"Request URI: #{response.request.base_uri}#{response.request.path}"
Expand Down
5 changes: 5 additions & 0 deletions spec/gitlab/objectified_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
@oh = Gitlab::ObjectifiedHash.new @hash
end

it "should objectify hash" do
@oh.a.should == @hash[:a]
@oh.b.should == @hash[:b]
end

describe "#to_hash" do
it "should return an original hash" do
@oh.to_hash.should == @hash
Expand Down
48 changes: 48 additions & 0 deletions spec/gitlab/request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'

describe Gitlab::Request do
it { should respond_to :get }
it { should respond_to :post }
it { should respond_to :put }
it { should respond_to :delete }

describe ".default_options" do
it "should have default values" do
default_options = Gitlab::Request.default_options
default_options.should be_a Hash
default_options[:parser].should be_a Proc
default_options[:format].should == :json
default_options[:headers].should == {'Accept' => 'application/json'}
default_options[:default_params].should be_nil
end
end

describe ".parse" do
it "should return ObjectifiedHash" do
body = JSON.unparse({a: 1, b: 2})
Gitlab::Request.parse(body).should be_an Gitlab::ObjectifiedHash
end
end

describe "#set_request_defaults" do
context "when endpoint is not set" do
it "should raise Error::MissingCredentials" do
expect {
Gitlab::Request.new.set_request_defaults(nil, 1234000)
}.to raise_error(Gitlab::Error::MissingCredentials, 'Please set an endpoint to API')
end
end

context "when endpoint is set" do
it "should set base_uri" do
Gitlab::Request.new.set_request_defaults('http://rabbit-hole.example.org', 1234000)
Gitlab::Request.base_uri.should == "http://rabbit-hole.example.org"
end

it "should set default_params" do
Gitlab::Request.new.set_request_defaults('http://rabbit-hole.example.org', 1234000, 'sudoer')
Gitlab::Request.default_params.should == {:sudo => 'sudoer'}
end
end
end
end

0 comments on commit 672a8d5

Please sign in to comment.