Initial Implementation (#1)

This commit is contained in:
George Dietrich 2020-12-24 00:48:48 -05:00 committed by GitHub
parent 34c8539ead
commit 83eda1298c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 1082 additions and 54 deletions

View file

@ -0,0 +1,31 @@
require "./spec_helper"
struct AcceptLanguageTest < ASPEC::TestCase
@[DataProvider("accept_value_data_provider")]
def test_accept_value(header : String?, expected : String?) : Nil
ANG::AcceptLanguage.new(header).accept_value.should eq expected
end
def accept_value_data_provider : Tuple
{
{"en;q=0.7", "en"},
{"en-GB;q=0.8", "en-gb"},
{"da", "da"},
{"en-gb;q=0.8", "en-gb"},
{"es;q=0.7", "es"},
{"fr ; q= 0.1", "fr"},
}
end
@[DataProvider("header_data_provider")]
def test_get_value(header : String?, expected : String?) : Nil
ANG::AcceptLanguage.new(header).header.should eq expected
end
def header_data_provider : Tuple
{
{"en;q=0.7", "en;q=0.7"},
{"en-GB;q=0.8", "en-GB;q=0.8"},
}
end
end

43
spec/accept_match_spec.cr Normal file
View file

@ -0,0 +1,43 @@
require "./spec_helper"
struct AcceptMatchTest < ASPEC::TestCase
@[DataProvider("compare_data_provider")]
def test_compare(match1 : ANG::AcceptMatch, match2 : ANG::AcceptMatch, expected : Int32) : Nil
(match1 <=> match2).should eq expected
end
def compare_data_provider : Tuple
{
{ANG::AcceptMatch.new(1.0, 110, 1), ANG::AcceptMatch.new(1.0, 111, 1), 0},
{ANG::AcceptMatch.new(0.1, 10, 1), ANG::AcceptMatch.new(0.1, 10, 2), -1},
{ANG::AcceptMatch.new(0.5, 110, 5), ANG::AcceptMatch.new(0.5, 11, 4), 1},
{ANG::AcceptMatch.new(0.4, 110, 1), ANG::AcceptMatch.new(0.6, 111, 3), 1},
{ANG::AcceptMatch.new(0.6, 110, 1), ANG::AcceptMatch.new(0.4, 111, 3), -1},
}
end
@[DataProvider("reduce_data_provider")]
def test_reduce(matches : Hash(Int32, ANG::AcceptMatch), match : ANG::AcceptMatch, expected : Hash(Int32, ANG::AcceptMatch)) : Nil
ANG::AcceptMatch.reduce(matches, match).should eq expected
end
def reduce_data_provider : Tuple
{
{
{1 => ANG::AcceptMatch.new(1.0, 10, 1)},
ANG::AcceptMatch.new(0.5, 111, 1),
{1 => ANG::AcceptMatch.new(0.5, 111, 1)},
},
{
{1 => ANG::AcceptMatch.new(1.0, 110, 1)},
ANG::AcceptMatch.new(0.5, 11, 1),
{1 => ANG::AcceptMatch.new(1.0, 110, 1)},
},
{
{0 => ANG::AcceptMatch.new(1.0, 10, 1)},
ANG::AcceptMatch.new(0.5, 111, 1),
{0 => ANG::AcceptMatch.new(1.0, 10, 1), 1 => ANG::AcceptMatch.new(0.5, 111, 1)},
},
}
end
end

54
spec/accept_spec.cr Normal file
View file

@ -0,0 +1,54 @@
require "./spec_helper"
struct AcceptTest < ASPEC::TestCase
def test_parameters : Nil
ANG::Accept.new("foo/bar; q=1; hello=world").parameters["hello"]?.should eq "world"
end
@[DataProvider("normalized_header_data_provider")]
def test_normalized_header(header : String, expected : String) : Nil
ANG::Accept.new(header).normalized_header.should eq expected
end
def normalized_header_data_provider : Tuple
{
{"text/html ; z=y; a = b; c=d", "text/html; a=b; c=d; z=y"},
{"application/pdf; q=1; param=p", "application/pdf; param=p"},
}
end
@[DataProvider("media_range_data_provider")]
def test_media_range(header : String, expected : String) : Nil
ANG::Accept.new(header).media_range.should eq expected
end
def media_range_data_provider : Tuple
{
{"text/html;hello=world", "text/html"},
{"application/pdf", "application/pdf"},
{"application/xhtml+xml;q=0.9", "application/xhtml+xml"},
{"text/plain; q=0.5", "text/plain"},
{"text/html;level=2;q=0.4", "text/html"},
{"text/html ; level = 2 ; q = 0.4", "text/html"},
{"text/*", "text/*"},
{"text/* ;q=1 ;level=2", "text/*"},
{"*/*", "*/*"},
{"*", "*/*"},
{"*/* ; param=555", "*/*"},
{"* ; param=555", "*/*"},
{"TEXT/hTmL;leVel=2; Q=0.4", "text/html"},
}
end
@[DataProvider("header_data_provider")]
def test_accept_value(header : String, expected : String) : Nil
ANG::Accept.new(header).header.should eq expected
end
def header_data_provider : Tuple
{
{"text/html;hello=world ;q=0.5", "text/html;hello=world ;q=0.5"},
{"application/pdf", "application/pdf"},
}
end
end

View file

@ -1,7 +0,0 @@
require "./spec_helper"
describe Athena::NAMESPACE_NAME do
it "works" do
false.should eq(true)
end
end

65
spec/base_accept_spec.cr Normal file
View file

@ -0,0 +1,65 @@
require "./spec_helper"
private struct MockAccept < ANG::BaseAccept; end
struct BaseAcceptTest < ASPEC::TestCase
@[DataProvider("build_parameters_data_provider")]
def test_build_parameters_string(header : String, expected : String) : Nil
MockAccept.new(header).normalized_header.should eq expected
end
def build_parameters_data_provider : Tuple
{
{"media/type; xxx = 1.0;level=2;foo=bar", "media/type; foo=bar; level=2; xxx=1.0"},
}
end
@[DataProvider("parameters_data_provider")]
def test_parse_parameters(header : String, expected_parameters : Hash(String, String)) : Nil
accept = MockAccept.new header
parameters = accept.parameters
# TODO: Can this be improved?
if header.includes? 'q'
parameters["q"] = accept.quality.to_s
end
expected_parameters.size.should eq parameters.size
expected_parameters.each do |k, v|
parameters.has_key?(k).should be_true
parameters[k].should eq v
end
end
def parameters_data_provider : Tuple
{
{
"application/json ;q=1.0; level=2;foo= bar",
{
"q" => "1.0",
"level" => "2",
"foo" => "bar",
},
},
{
"application/json ;q = 1.0; level = 2; FOO = bAr",
{
"q" => "1.0",
"level" => "2",
"foo" => "bAr",
},
},
{
"application/json;q=1.0",
{
"q" => "1.0",
},
},
{
"application/json;foo",
{} of String => String,
},
}
end
end

View file

@ -0,0 +1,67 @@
require "./spec_helper"
struct CharsetNegotiatorTest < NegotiatorTestCase
@negotiator : ANG::CharsetNegotiator
def initialize
@negotiator = ANG::CharsetNegotiator.new
end
def test_best_unmatched_header : Nil
@negotiator.best("foo, bar, yo", {"baz"}).should be_nil
end
def test_best_ignores_missing_content : Nil
accept = @negotiator.best "en; q=0.1, fr; q=0.4, bu; q=1.0", {"en", "fr"}
accept = accept.should_not be_nil
accept.should be_a ANG::AcceptCharset
accept.charset.should eq "fr"
end
def test_best_respects_priorities : Nil
accept = @negotiator.best "foo, bar, yo", {"yo"}
accept = accept.should_not be_nil
accept.should be_a ANG::AcceptCharset
accept.charset.should eq "yo"
end
def test_best_respects_quality : Nil
accept = @negotiator.best "utf-8;q=0.5,iso-8859-1", {"iso-8859-1;q=0.3", "utf-8;q=0.9", "utf-16;q=1.0"}
accept = accept.should_not be_nil
accept.should be_a ANG::AcceptCharset
accept.charset.should eq "utf-8"
end
@[DataProvider("best_data_provider")]
def test_best(header : String, priorities : Indexable(String), expected : String?) : Nil
accept = @negotiator.best header, priorities
if accept.nil?
expected.should be_nil
else
accept.should be_a ANG::AcceptCharset
accept.header.should eq expected
end
end
def best_data_provider : Tuple
php_pear_charset = "ISO-8859-1, Big5;q=0.6,utf-8;q=0.7, *;q=0.5"
php_pear_charset2 = "ISO-8859-1, Big5;q=0.6,utf-8;q=0.7"
{
{php_pear_charset, {"utf-8", "big5", "iso-8859-1", "shift-jis"}, "iso-8859-1"},
{php_pear_charset, {"utf-8", "big5", "shift-jis"}, "utf-8"},
{php_pear_charset, {"Big5", "shift-jis"}, "Big5"},
{php_pear_charset, {"shift-jis"}, "shift-jis"},
{php_pear_charset2, {"utf-8", "big5", "iso-8859-1", "shift-jis"}, "iso-8859-1"},
{php_pear_charset2, {"utf-8", "big5", "shift-jis"}, "utf-8"},
{php_pear_charset2, {"Big5", "shift-jis"}, "Big5"},
{"utf-8;q=0.6,iso-8859-5;q=0.9", {"iso-8859-5", "utf-8"}, "iso-8859-5"},
{"en, *;q=0.9", {"fr"}, "fr"},
# Quality of source factors
{php_pear_charset, {"iso-8859-1;q=0.5", "utf-8", "utf-16;q=1.0"}, "utf-8"},
{php_pear_charset, {"iso-8859-1;q=0.8", "utf-8", "utf-16;q=1.0"}, "iso-8859-1;q=0.8"},
}
end
end

View file

@ -0,0 +1,42 @@
require "./spec_helper"
struct EncodingNegotiatorTest < NegotiatorTestCase
@negotiator : ANG::EncodingNegotiator
def initialize
@negotiator = ANG::EncodingNegotiator.new
end
def test_best_unmatched_header : Nil
@negotiator.best("foo, bar, yo", {"baz"}).should be_nil
end
def test_best_respects_quality : Nil
accept = @negotiator.best "gzip;q=0.7,identity", {"identity;q=0.5", "gzip;q=0.9"}
accept = accept.should_not be_nil
accept.should be_a ANG::AcceptEncoding
accept.coding.should eq "gzip"
end
@[DataProvider("best_data_provider")]
def test_best(header : String, priorities : Indexable(String), expected : String?) : Nil
accept = @negotiator.best header, priorities
if accept.nil?
expected.should be_nil
else
accept.should be_a ANG::AcceptEncoding
accept.header.should eq expected
end
end
def best_data_provider : Tuple
{
{"gzip;q=1.0, identity; q=0.5, *;q=0", {"identity"}, "identity"},
{"gzip;q=0.5, identity; q=0.5, *;q=0.7", {"bzip", "foo"}, "bzip"},
{"gzip;q=0.7, identity; q=0.5, *;q=0.7", {"gzip", "foo"}, "gzip"},
# Quality of source factors
{"gzip;q=0.7,identity", {"identity;q=0.5", "gzip;q=0.9"}, "gzip;q=0.9"},
}
end
end

View file

@ -0,0 +1,43 @@
require "./spec_helper"
struct LanguageNegotiatorTest < NegotiatorTestCase
@negotiator : ANG::LanguageNegotiator
def initialize
@negotiator = ANG::LanguageNegotiator.new
end
def test_best_respects_quality : Nil
accept = @negotiator.best "en;q=0.5,de", {"de;q=0.3", "en;q=0.9"}
accept = accept.should_not be_nil
accept.should be_a ANG::AcceptLanguage
accept.language.should eq "en"
end
@[DataProvider("best_data_provider")]
def test_best(header : String, priorities : Indexable(String), expected : String?) : Nil
accept = @negotiator.best header, priorities
if accept.nil?
expected.should be_nil
else
accept.should be_a ANG::AcceptLanguage
accept.header.should eq expected
end
end
def best_data_provider : Tuple
{
{"en, de", {"fr"}, nil},
{"foo, bar, yo", {"baz", "biz"}, nil},
{"fr-FR, en;q=0.8", {"en-US", "de-DE"}, "en-US"},
{"en, *;q=0.9", {"fr"}, "fr"},
{"foo, bar, yo", {"yo"}, "yo"},
{"en; q=0.1, fr; q=0.4, bu; q=1.0", {"en", "fr"}, "fr"},
{"en; q=0.1, fr; q=0.4, fu; q=0.9, de; q=0.2", {"en", "fu"}, "fu"},
{"fr, zh-Hans-CN;q=0.3", {"fr"}, "fr"},
# Quality of source factors
{"en;q=0.5,de", {"de;q=0.3", "en;q=0.9"}, "en;q=0.9"},
}
end
end

129
spec/negotiator_spec.cr Normal file
View file

@ -0,0 +1,129 @@
require "./spec_helper"
struct NegotiatorTest < NegotiatorTestCase
@negotiator : ANG::Negotiator
def initialize
@negotiator = ANG::Negotiator.new
end
def test_best_respects_quality : Nil
accept = @negotiator.best "text/html,text/*;q=0.7", {"text/html;q=0.5", "text/plain;q=0.9"}
accept = accept.should_not be_nil
accept.should be_a ANG::Accept
accept.media_range.should eq "text/plain"
end
def test_best_invalid_unstrict
@negotiator.best("/qwer", {"foo/bar"}, false).should be_nil
end
def test_invalid_media_type : Nil
ex = expect_raises ANG::Exceptions::InvalidMediaType, "Invalid media type: '/qwer'." do
@negotiator.best "foo/bar", {"/qwer"}
end
ex.media_range.should eq "/qwer"
end
@[DataProvider("best_data_provider")]
def test_best(header : String, priorities : Indexable(String), expected : Tuple(String, Hash(String, String) | Nil) | Nil) : Nil
begin
accept_header = @negotiator.best header, priorities
rescue ex
ex.should eq expected
return
end
if accept_header.nil?
expected.should be_nil
return
end
accept_header.should be_a ANG::Accept
expected = expected.should_not be_nil
accept_header.media_range.should eq expected[0]
accept_header.parameters.should eq (expected[1] || Hash(String, String).new)
end
def best_data_provider : Tuple
rfc_header = "text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5"
php_pear_header = "text/html,application/xhtml+xml,application/xml;q=0.9,text/*;q=0.7,*/*,image/gif; q=0.8, image/jpeg; q=0.6, image/*"
{
{"/qwer", {"f/g"}, nil},
{"text/html", {"application/rss"}, nil},
{rfc_header, {"text/html;q=0.4", "text/plain"}, {"text/plain", nil}},
# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html.
{rfc_header, {"text/html;level=1"}, {"text/html", {"level" => "1"}}},
{rfc_header, {"text/html"}, {"text/html", nil}},
{rfc_header, {"image/jpeg"}, {"image/jpeg", nil}},
{rfc_header, {"text/html;level=2"}, {"text/html", {"level" => "2"}}},
{rfc_header, {"text/html;level=3"}, {"text/html", {"level" => "3"}}},
{"text/*;q=0.7, text/html;q=0.3, */*;q=0.5, image/png;q=0.4", {"text/html", "image/png"}, {"image/png", nil}},
{"image/png;q=0.1, text/plain, audio/ogg;q=0.9", {"image/png", "text/plain", "audio/ogg"}, {"text/plain", nil}},
{"image/png, text/plain, audio/ogg", {"baz/asdf"}, nil},
{"image/png, text/plain, audio/ogg", {"audio/ogg"}, {"audio/ogg", nil}},
{"image/png, text/plain, audio/ogg", {"YO/SuP"}, nil},
{"text/html; charset=UTF-8, application/pdf", {"text/html; charset=UTF-8"}, {"text/html", {"charset" => "UTF-8"}}},
{"text/html; charset=UTF-8, application/pdf", {"text/html"}, nil},
{"text/html, application/pdf", {"text/html; charset=UTF-8"}, {"text/html", {"charset" => "UTF-8"}}},
# PHP"s PEAR HTTP2 assertions I took from the other lib.
{php_pear_header, {"image/gif", "image/png", "application/xhtml+xml", "application/xml", "text/html", "image/jpeg", "text/plain"}, {"image/png", nil}},
{php_pear_header, {"image/gif", "application/xhtml+xml", "application/xml", "image/jpeg", "text/plain"}, {"application/xhtml+xml", nil}},
{php_pear_header, {"image/gif", "application/xml", "image/jpeg", "text/plain"}, {"application/xml", nil}},
{php_pear_header, {"image/gif", "image/jpeg", "text/plain"}, {"image/gif", nil}},
{php_pear_header, {"text/plain", "image/png", "image/jpeg"}, {"image/png", nil}},
{php_pear_header, {"image/jpeg", "image/gif"}, {"image/gif", nil}},
{php_pear_header, {"image/png"}, {"image/png", nil}},
{php_pear_header, {"audio/midi"}, {"audio/midi", nil}},
{"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", {"application/rss+xml"}, {"application/rss+xml", nil}},
# Case sensitiviy
{"text/* ; q=0.3, TEXT/html ;Q=0.7, text/html ; level=1, texT/Html ;leVel = 2 ;q=0.4, */* ; q=0.5", {"text/html; level=2"}, {"text/html", {"level" => "2"}}},
{"text/* ; q=0.3, text/html;Q=0.7, text/html ;level=1, text/html; level=2;q=0.4, */*;q=0.5", {"text/HTML; level=3"}, {"text/html", {"level" => "3"}}},
# IE8
{"image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*", {"text/html", "application/xhtml+xml"}, {"text/html", nil}},
# wildcards with `+`
{"application/vnd.api+json", {"application/json", "application/*+json"}, {"application/*+json", nil}},
{"application/json;q=0.7, application/*+json;q=0.7", {"application/hal+json", "application/problem+json"}, {"application/hal+json", nil}},
{"application/json;q=0.7, application/problem+*;q=0.7", {"application/hal+xml", "application/problem+xml"}, {"application/problem+xml", nil}},
{php_pear_header, {"application/*+xml"}, {"application/*+xml", nil}},
{"application/hal+json", {"application/ld+json", "application/hal+json", "application/xml", "text/xml", "application/json", "text/html"}, {"application/hal+json", nil}},
}
end
def test_ordered_elements_exception_handling : Nil
expect_raises ArgumentError, "The header string should not be empty." do
@negotiator.ordered_elements ""
end
end
@[DataProvider("test_ordered_elements_data_provider")]
def test_ordered_elements(header : String, expected : Indexable(String)) : Nil
elements = @negotiator.ordered_elements header
expected.each_with_index do |element, idx|
elements[idx].should be_a ANG::Accept
element.should eq elements[idx].header
end
end
def test_ordered_elements_data_provider : Tuple
{
{"/qwer", [] of String}, # Invalid
{"text/html, text/xml", {"text/html", "text/xml"}}, # Ordered as given if no quality modifier
{"text/html;q=0.3, text/html;q=0.7", {"text/html;q=0.7", "text/html;q=0.3"}}, # Ordered by quality modifier
{"text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5", {"text/html;level=1", "text/html;q=0.7", "*/*;q=0.5", "text/html;level=2;q=0.4", "text/*;q=0.3"}}, # Ordered by quality modifier; one without wins
}
end
end

View file

@ -0,0 +1,11 @@
abstract struct NegotiatorTestCase < ASPEC::TestCase
def test_best_exception_handling : Nil
expect_raises ArgumentError, "priorities should not be empty." do
@negotiator.best "foo/bar", [] of String
end
expect_raises ArgumentError, "The header string should not be empty." do
@negotiator.best "", {"text/html"}
end
end
end

View file

@ -1,2 +1,8 @@
require "spec"
require "../src/athena-COMPONENT_NAME"
require "athena-spec"
require "../src/athena-negotiation"
require "./negotiator_test_case"
include ASPEC::Methods
ASPEC.run_all