Last active
October 19, 2018 16:37
-
-
Save serradura/8dde578826dfa0f408ddda2803c156cc to your computer and use it in GitHub Desktop.
Example of how to make APIs tests (end-to-end) using Ruby.
This file contains hidden or 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
# Runtime dependency: | |
# gem install bundler --no-ri --no-rdoc | |
# How to run: | |
# ruby example_1.rb | |
require "bundler/inline" | |
gemfile do | |
source "https://rubygems.org" | |
gem "u-test" , "0.9.0" | |
gem "http" , "~> 4.0.0" # https://github.com/httprb/http/wiki | |
gem "json-schema", "~> 2.8.1" # https://github.com/ruby-json-schema/json-schema | |
end | |
# https://dog.ceo/dog-api/ | |
class TestFetchRandomImage < Microtest::Test | |
def test_response_schema | |
schema = { | |
"type" => "object", | |
"required" => ["status", "message"], | |
"properties" => { | |
"status" => {"type" => "string"}, | |
"message" => {"type" => "string"} | |
} | |
} | |
response = HTTP.get("https://dog.ceo/api/breeds/image/random") | |
assert response.code == 200 | |
assert JSON::Validator.validate(schema, response.parse) | |
end | |
end | |
class TestFetchBreedImages < Microtest::Test | |
def test_response_schema | |
schema = { | |
"type" => "object", | |
"required" => ["status", "message"], | |
"properties" => { | |
"status" => {"type" => "string"}, | |
"message" => { | |
"type": "array", | |
"items": { "type": "string" } | |
} | |
} | |
} | |
random_breed = fetch_random_breed | |
response = HTTP.get("https://dog.ceo/api/breed/#{random_breed}/images") | |
assert response.code == 200 | |
assert JSON::Validator.validate(schema, response.parse) | |
end | |
private | |
def fetch_random_breed | |
response = HTTP.get("https://dog.ceo/api/breeds/image/random") | |
message = response.parse['message'] | |
message.match(/breeds\/(.*)\/.*/)[1] | |
end | |
end | |
Microtest.call |
This file contains hidden or 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
# Runtime dependency: | |
# gem install bundler --no-ri --no-rdoc | |
# How to run: | |
# ruby example_2.rb | |
# or | |
# PRINT_OUTPUTS=t ruby example_2.rb | |
require "bundler/inline" | |
gemfile do | |
source "https://rubygems.org" | |
gem "u-test" , "0.9.0" | |
gem "awesome_print", "~> 1.8.0" | |
gem "http" , "~> 4.0.0" # https://github.com/httprb/http/wiki | |
gem "json-schema", "~> 2.8.1" # https://github.com/ruby-json-schema/json-schema | |
end | |
class ReportTestCase < Microtest::Test | |
def setup_all | |
print_output("Test case: #{self.class.name}\n") { |msg| puts msg.white } | |
end | |
def setup(test_method) | |
print_output("Performing: #{test_method}\n") { |msg| puts msg.pale } | |
end | |
def print_output(*values) | |
return if String(ENV['PRINT_OUTPUTS']).strip.empty? | |
values.each { |value| block_given? ? yield(value) : ap(value) } | |
end | |
end | |
class TestDogCEOAPI < ReportTestCase # https://dog.ceo/dog-api/ | |
def fetch_dog(params: []) | |
url = "https://dog.ceo/api/#{params.join("/")}" | |
{ url: url, response: HTTP.get(url) } | |
end | |
def fetch_random_image | |
fetch_dog(params: ["breeds", "image", "random"]) | |
end | |
def fetch_images_by_breed(name) | |
fetch_dog(params: ["breed", name, "images"]) | |
end | |
end | |
class TestFetchRandomImage < TestDogCEOAPI | |
def test_response_schema | |
schema = { | |
"type" => "object", | |
"required" => ["status", "message"], | |
"properties" => { | |
"status" => {"type" => "string"}, | |
"message" => {"type" => "string"} | |
} | |
} | |
result = fetch_random_image | |
json = result[:response].parse | |
assert result[:response].code == 200 | |
assert JSON::Validator.validate(schema, json) | |
print_output(result[:url], json) | |
end | |
end | |
class TestFetchBreedImages < TestDogCEOAPI | |
def test_response_schema | |
schema = { | |
"type" => "object", | |
"required" => ["status", "message"], | |
"properties" => { | |
"status" => {"type" => "string"}, | |
"message" => { | |
"type": "array", | |
"items": { "type": "string" } | |
} | |
} | |
} | |
print_output("Fetching a random dog breed image...") | |
random_breed_data = fetch_random_image[:response].parse | |
print_output(random_breed_data) | |
random_breed = extract_breed(random_breed_data['message']) | |
print_output("Fetching all breed images...") | |
result = fetch_images_by_breed(random_breed) | |
json = result[:response].parse | |
assert result[:response].code == 200 | |
assert JSON::Validator.validate(schema, json) | |
print_output(result[:url], json) | |
end | |
private | |
def extract_breed(message) | |
message.match(/breeds\/(.*)\/.*/)[1] | |
end | |
end | |
Microtest.call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment