Created
December 20, 2012 11:11
-
-
Save xslim/4344714 to your computer and use it in GitHub Desktop.
RSpec Remote API testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApiHelper | |
def login(user = "username", password = "") | |
do_post "/login", {:username => user, :password => password} | |
end | |
def check_http_code(response) | |
response.status.should be(200) | |
end | |
def check_api_code(json) | |
json["response"]["StatusCode"].should be(200) | |
end | |
def do_post(url, json) | |
heads = { | |
'Content-Type' => "application/json" | |
} | |
heads['Cookie'] = @cookie if @cookie | |
headers(heads) | |
post url, {}, json.to_json | |
@cookie = last_response['set-cookie'].split('; ')[0] if last_response['set-cookie'] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
source 'http://gems.github.com' | |
gem 'rspec' | |
gem 'remote_http_testing' | |
gem 'net-http-spy' | |
gem 'mechanize' | |
gem "ZenTest" | |
# Beautifying | |
gem 'awesome_print' | |
gem 'httpclient' | |
gem 'redgreen' | |
gem 'pry' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
describe "IHLP Move Login behaviour", :type => :request do | |
def login_request(username, password) | |
do_post "/Login", {:username => username, | |
:password => password} | |
end | |
it "should login" do | |
login_request("mgr", "") | |
check_http_code (last_response) | |
check_api_code (json_response) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
describe "Main API", :type => :request do | |
before(:each) do | |
host = 'http://api.example.com/api' | |
end | |
it "should request datetimePOC" do | |
do_post "/datetimePOC", {:cachetime => Time.now} | |
end | |
it "AssignGuidToItem should fail with a wrong guid" do | |
login | |
do_post "/AssignGuidToItem", {:id => 54, :guid => 20} | |
response = last_response | |
resp = json_response | |
response.status.should be(500) | |
resp["Message"].should eq("Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).") | |
end | |
it "should request GetCITypesList" do | |
login | |
do_post "/GetCITypesList", {} | |
response = last_response | |
resp = json_response | |
response.status.should be(200) | |
end | |
it "should request a new RequestService" do | |
login | |
do_post "/GetNewObject", {:ObjectType => "RequestService"} | |
response = last_response | |
resp = json_response | |
response.status.should be(200) | |
end | |
it "should request a new ItemPC" do | |
login | |
do_post "/GetNewObject", {:ObjectType => "ItemPC"} | |
response = last_response | |
resp = json_response | |
response.status.should be(200) | |
end | |
it "should request a new RequestIncident" do | |
login | |
do_post "/GetNewObject", {:ObjectType => "RequestIncident"} | |
response = last_response | |
resp = json_response | |
response.status.should be(200) | |
end | |
it "should request an object with guid" do | |
login | |
# Guid to test 5b5bb30fe6a34a2abbc380b8ee540846 | |
do_post "/GetObjectByGuid", {:guid => "5b5bb30f-e6a3-4a2a-bbc3-80b8ee540846"} | |
response = last_response | |
resp = json_response | |
#puts resp.to_json | |
response.status.should be(200) | |
end | |
it "should request an object with id" do | |
login | |
do_post "/GetObject", {:id => 145} | |
response = last_response | |
resp = json_response | |
response.status.should be(200) | |
ap resp | |
end | |
it "should request a file object" do | |
#login | |
#do_post "/GetFile", {:fileId => 147} | |
#resp = json_response | |
#puts resp.to_json | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "bundler/setup" | |
require "rspec" | |
require "remote_http_testing" | |
require 'ap' | |
require 'net-http-spy' | |
require "helpers/api_helper" | |
Net::HTTP.http_logger_options = {:verbose => true} if Net::HTTP.respond_to?(:http_logger_options) | |
spec_dir = File.dirname(__FILE__) | |
$LOAD_PATH.unshift(spec_dir) | |
# Require all ruby files in the 'support' folder | |
Dir[File.join(spec_dir, "support/**/*.rb")].each {|f| require f} | |
module RemoteHttpTesting | |
def server | |
"http://api.example.com/api/" | |
end | |
def response | |
last_response | |
end | |
def headers(value={}) | |
self.headers_for_request = value | |
end | |
# For debug | |
# alias_method :old_create_request, :create_request | |
# def create_request(url, http_method, params = {}, request_body = nil) | |
# request = old_create_request(url, http_method, params, request_body) | |
# binding.pry | |
# request | |
# end | |
end | |
module Net | |
class HTTPResponse | |
def status | |
self.code.to_i | |
end | |
end | |
end | |
RSpec.configure do |config| | |
config.color_enabled = true | |
config.formatter = :documentation | |
config.include RemoteHttpTesting | |
config.include IspocHelper | |
end | |
def md5(value) | |
Digest::MD5.hexdigest(value) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment