2016-04-11 16:05:15 +02:00
|
|
|
# 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 }
|
2016-05-09 00:03:54 +02:00
|
|
|
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
|
2018-03-08 02:05:19 +01:00
|
|
|
get '/latest'
|
|
|
|
res = Oj.load(last_response.body)
|
2012-11-21 01:10:18 +01:00
|
|
|
get '/latest?base=USD'
|
2018-03-08 02:05:19 +01:00
|
|
|
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
|
2012-11-21 15:59:39 +01:00
|
|
|
|
2015-08-30 12:54:13 +02:00
|
|
|
it 'returns historical quotes' do
|
2012-11-21 15:59:39 +01:00
|
|
|
get '/2012-11-20'
|
|
|
|
json['rates'].wont_be :empty?
|
2014-03-17 11:15:05 +01:00
|
|
|
json['date'].must_equal '2012-11-20'
|
2012-11-21 15:59:39 +01:00
|
|
|
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|
|
2016-04-24 18:08:21 +02:00
|
|
|
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|
|
2016-05-09 12:50:56 +02:00
|
|
|
header 'Origin', '*'
|
2016-04-24 16:39:08 +02:00
|
|
|
get path
|
2016-06-08 15:49:43 +02:00
|
|
|
assert headers.key?('Access-Control-Allow-Methods')
|
2016-04-24 16:39:08 +02:00
|
|
|
end
|
2014-10-07 13:08:28 +02:00
|
|
|
end
|
2016-04-24 17:19:36 +02:00
|
|
|
|
|
|
|
it 'responds to preflight requests' do
|
2017-06-14 22:16:21 +02:00
|
|
|
%w[/ /latest /2012-11-20].each do |path|
|
2016-05-09 12:50:56 +02:00
|
|
|
header 'Origin', '*'
|
2016-06-08 15:49:43 +02:00
|
|
|
header 'Access-Control-Request-Method', 'GET'
|
|
|
|
header 'Access-Control-Request-Headers', 'Content-Type'
|
2016-05-09 12:50:56 +02:00
|
|
|
options path
|
2016-06-08 15:49:43 +02:00
|
|
|
assert headers.key?('Access-Control-Allow-Methods')
|
2016-05-09 12:50:56 +02:00
|
|
|
end
|
2016-04-24 17:19:36 +02:00
|
|
|
end
|
2016-09-22 18:53:31 +02:00
|
|
|
|
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
|
2016-09-22 18:53:31 +02:00
|
|
|
end
|
2012-11-21 01:10:18 +01:00
|
|
|
end
|