frankfurter/spec/api_spec.rb

89 lines
1.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2017-06-14 22:16:21 +02:00
2012-11-21 01:10:18 +01:00
require_relative 'helper'
require 'rack/test'
2014-03-14 13:08:48 +01:00
require 'api'
2012-11-21 01:10:18 +01:00
2014-03-14 13:08:48 +01:00
describe 'the API' do
2012-11-21 01:10:18 +01:00
include Rack::Test::Methods
let(:app) { Sinatra::Application }
let(:json) { Oj.load(last_response.body) }
2014-10-07 13:08:28 +02:00
let(:headers) { last_response.headers }
2012-11-21 01:10:18 +01:00
2014-06-19 13:31:35 +02:00
it 'describes itself' do
get '/'
last_response.must_be :ok?
end
2015-08-30 12:54:13 +02:00
it 'returns latest quotes' do
2012-11-21 01:10:18 +01:00
get '/latest'
2012-11-21 17:11:47 +01:00
last_response.must_be :ok?
2012-11-21 01:10:18 +01:00
end
it 'sets base currency' do
get '/latest'
res = Oj.load(last_response.body)
2012-11-21 01:10:18 +01:00
get '/latest?base=USD'
json.wont_equal res
2012-11-21 01:10:18 +01:00
end
2012-11-21 15:46:15 +01:00
2016-10-12 00:37:29 +02:00
it 'sets base amount' do
get '/latest?amount=10'
json['rates']['USD'].must_be :>, 10
end
2012-11-21 15:46:15 +01:00
it 'filters symbols' do
get '/latest?symbols=USD'
2017-06-14 22:16:21 +02:00
json['rates'].keys.must_equal %w[USD]
2012-11-21 15:46:15 +01:00
end
2015-08-30 12:54:13 +02:00
it 'returns historical quotes' do
get '/2012-11-20'
json['rates'].wont_be :empty?
2014-03-17 11:15:05 +01:00
json['date'].must_equal '2012-11-20'
end
it 'works around holidays' do
get '/2010-01-01'
json['rates'].wont_be :empty?
end
2014-10-07 13:08:28 +02:00
2017-01-09 02:30:24 +01:00
it 'returns a cache control header' do
2017-06-14 22:16:21 +02:00
%w[/ /latest /2012-11-20].each do |path|
2017-01-09 02:30:24 +01:00
get path
headers['Cache-Control'].wont_be_nil
end
end
it 'returns a last modified header' do
2017-06-14 22:16:21 +02:00
%w[/latest /2012-11-20].each do |path|
get path
headers['Last-Modified'].wont_be_nil
end
2015-09-15 13:01:16 +02:00
end
2014-10-07 13:08:28 +02:00
it 'allows cross-origin requests' do
2017-06-14 22:16:21 +02:00
%w[/ /latest /2012-11-20].each do |path|
header 'Origin', '*'
get path
2016-06-08 15:49:43 +02:00
assert headers.key?('Access-Control-Allow-Methods')
end
2014-10-07 13:08:28 +02:00
end
it 'responds to preflight requests' do
2017-06-14 22:16:21 +02:00
%w[/ /latest /2012-11-20].each do |path|
header 'Origin', '*'
2016-06-08 15:49:43 +02:00
header 'Access-Control-Request-Method', 'GET'
header 'Access-Control-Request-Headers', 'Content-Type'
options path
2016-06-08 15:49:43 +02:00
assert headers.key?('Access-Control-Allow-Methods')
end
end
2016-10-12 00:37:29 +02:00
it 'converts an amount' do
get '/latest?from=GBP&to=USD&amount=100'
json['rates']['USD'].must_be :>, 100
end
2012-11-21 01:10:18 +01:00
end