2016-04-11 16:05:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
get '/latest?base=USD'
|
|
|
|
json['base'].must_equal 'USD'
|
|
|
|
end
|
2012-11-21 15:46:15 +01:00
|
|
|
|
|
|
|
it 'filters symbols' do
|
|
|
|
get '/latest?symbols=USD'
|
|
|
|
json['rates'].keys.must_equal %w(USD)
|
|
|
|
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
|
|
|
|
2016-04-24 18:08:21 +02:00
|
|
|
it 'returns a last modified header' do
|
|
|
|
%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
|
2016-04-24 16:39:08 +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-04-24 17:19:36 +02:00
|
|
|
refute_empty headers['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
|
2016-05-09 12:50:56 +02:00
|
|
|
%w(/ /latest /2012-11-20).each do |path|
|
|
|
|
header 'Origin', '*'
|
|
|
|
options path
|
|
|
|
refute_empty headers['Access-Control-Allow-Methods']
|
|
|
|
end
|
2016-04-24 17:19:36 +02:00
|
|
|
end
|
2012-11-21 01:10:18 +01:00
|
|
|
end
|