2016-04-11 16:05:15 +02:00
|
|
|
# frozen_string_literal: true
|
2017-06-14 22:16:21 +02:00
|
|
|
|
2018-04-16 17:19:15 +02:00
|
|
|
require_relative '../helper'
|
2012-11-21 01:10:18 +01:00
|
|
|
require 'rack/test'
|
2018-04-16 17:19:15 +02:00
|
|
|
require 'web/server'
|
2012-11-21 01:10:18 +01:00
|
|
|
|
2018-04-16 17:19:15 +02:00
|
|
|
describe 'the server' do
|
2012-11-21 01:10:18 +01:00
|
|
|
include Rack::Test::Methods
|
|
|
|
|
2020-05-08 15:50:09 +02:00
|
|
|
let(:app) { Web::Server.freeze }
|
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
|
|
|
|
2020-05-02 20:38:36 +02:00
|
|
|
it 'returns link to docs' do
|
2014-06-19 13:31:35 +02:00
|
|
|
get '/'
|
2020-05-02 20:38:36 +02:00
|
|
|
_(json['docs']).wont_be_nil
|
2014-06-19 13:31:35 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 14:39:25 +02:00
|
|
|
it 'returns latest quotes' do
|
|
|
|
get '/latest'
|
2020-05-02 18:08:47 +02:00
|
|
|
_(last_response).must_be :ok?
|
2012-11-21 01:10:18 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets base currency' do
|
2018-10-10 14:39:25 +02:00
|
|
|
get '/latest'
|
2018-03-08 02:05:19 +01:00
|
|
|
res = Oj.load(last_response.body)
|
2018-10-10 14:39:25 +02:00
|
|
|
get '/latest?from=USD'
|
2020-05-02 18:08:47 +02: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
|
2018-10-10 14:39:25 +02:00
|
|
|
get '/latest?amount=10'
|
2020-05-02 18:08:47 +02:00
|
|
|
_(json['rates']['USD']).must_be :>, 10
|
2016-10-12 00:37:29 +02:00
|
|
|
end
|
|
|
|
|
2012-11-21 15:46:15 +01:00
|
|
|
it 'filters symbols' do
|
2018-10-10 14:39:25 +02:00
|
|
|
get '/latest?to=USD'
|
2020-05-02 18:08:47 +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'
|
2020-05-02 18:08:47 +02:00
|
|
|
_(json['rates']).wont_be :empty?
|
|
|
|
_(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'
|
2020-05-02 18:08:47 +02:00
|
|
|
_(json['rates']).wont_be :empty?
|
2012-11-21 15:59:39 +01:00
|
|
|
end
|
2014-10-07 13:08:28 +02:00
|
|
|
|
2018-07-05 21:19:37 +02:00
|
|
|
it 'returns an ETag' do
|
2018-10-10 14:39:25 +02:00
|
|
|
%w[/latest /2012-11-20].each do |path|
|
2016-04-24 18:08:21 +02:00
|
|
|
get path
|
2020-05-02 18:08:47 +02:00
|
|
|
_(headers['ETag']).wont_be_nil
|
2016-04-24 18:08:21 +02:00
|
|
|
end
|
2015-09-15 13:01:16 +02:00
|
|
|
end
|
|
|
|
|
2023-03-01 15:28:18 +01:00
|
|
|
it 'returns a cache control header' do
|
|
|
|
%w[/latest /2012-11-20].each do |path|
|
|
|
|
get path
|
|
|
|
_(headers['Cache-Control']).wont_be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-07 13:08:28 +02:00
|
|
|
it 'allows cross-origin requests' do
|
2018-10-10 14:39:25 +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
|
2023-02-28 17:49:48 +01:00
|
|
|
|
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
|
2018-10-10 14:39:25 +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
|
2023-02-28 17:49:48 +01:00
|
|
|
|
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
|
2018-10-10 14:39:25 +02:00
|
|
|
get '/latest?from=GBP&to=USD&amount=100'
|
2020-05-02 18:08:47 +02:00
|
|
|
_(json['rates']['USD']).must_be :>, 100
|
2016-09-22 18:53:31 +02:00
|
|
|
end
|
2018-04-16 17:19:15 +02:00
|
|
|
|
|
|
|
it 'returns rates for a given period' do
|
|
|
|
get '/2010-01-01..2010-12-31'
|
2020-05-02 18:08:47 +02:00
|
|
|
_(json['start_date']).wont_be :empty?
|
|
|
|
_(json['end_date']).wont_be :empty?
|
|
|
|
_(json['rates']).wont_be :empty?
|
2018-04-16 17:19:15 +02:00
|
|
|
end
|
2018-09-18 14:16:10 +02:00
|
|
|
|
|
|
|
it 'returns rates when given period does not include end date' do
|
|
|
|
get '/2010-01-01..'
|
2020-05-02 18:08:47 +02:00
|
|
|
_(json['start_date']).wont_be :empty?
|
|
|
|
_(json['end_date']).wont_be :empty?
|
|
|
|
_(json['rates']).wont_be :empty?
|
2018-09-18 14:16:10 +02:00
|
|
|
end
|
2018-10-10 14:39:25 +02:00
|
|
|
|
|
|
|
it 'returns currencies' do
|
|
|
|
get '/currencies'
|
2020-05-02 18:08:47 +02:00
|
|
|
_(json['USD']).must_equal 'United States Dollar'
|
2018-10-10 14:39:25 +02:00
|
|
|
end
|
2019-12-24 15:18:10 +01:00
|
|
|
|
|
|
|
it 'handles JSONP' do
|
|
|
|
get '/latest?callback=foo'
|
2020-05-08 15:50:09 +02:00
|
|
|
_(last_response.body).must_be :start_with?, '/**/foo'
|
2019-12-24 15:18:10 +01:00
|
|
|
end
|
2020-05-02 20:38:36 +02:00
|
|
|
|
2020-05-08 15:50:09 +02:00
|
|
|
it 'sets charset to utf-8' do
|
2020-05-02 20:38:36 +02:00
|
|
|
get '/currencies'
|
2020-05-08 15:50:09 +02:00
|
|
|
_(last_response.headers['content-type']).must_be :end_with?, 'charset=utf-8'
|
2020-05-02 20:38:36 +02:00
|
|
|
end
|
2012-11-21 01:10:18 +01:00
|
|
|
end
|