aboutsummaryrefslogtreecommitdiffstats
path: root/src/cow_http_hd.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2014-12-15 23:04:45 +0200
committerLoïc Hoguin <[email protected]>2014-12-15 23:04:45 +0200
commit6581c2e808e0b57ba4e8389c2126779bc43ada0a (patch)
treef0a8fac64de04adf5132d547c6e43e3ddab433eb /src/cow_http_hd.erl
parentc1af15e981801b6baf616f12dc3ddd944a30e4c5 (diff)
downloadcowlib-6581c2e808e0b57ba4e8389c2126779bc43ada0a.tar.gz
cowlib-6581c2e808e0b57ba4e8389c2126779bc43ada0a.tar.bz2
cowlib-6581c2e808e0b57ba4e8389c2126779bc43ada0a.zip
Add a property for cow_http_hd:parse_accept_language/1
Diffstat (limited to 'src/cow_http_hd.erl')
-rw-r--r--src/cow_http_hd.erl55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/cow_http_hd.erl b/src/cow_http_hd.erl
index 441cd1d..1672d2a 100644
--- a/src/cow_http_hd.erl
+++ b/src/cow_http_hd.erl
@@ -43,6 +43,12 @@ alpha_chars() -> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
digit_chars() -> "0123456789".
alphanum_chars() -> "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".
+alpha() ->
+ oneof(alpha_chars()).
+
+alphanum() ->
+ oneof(alphanum_chars()).
+
tchar() ->
frequency([
{1, oneof([$!, $#, $$, $%, $&, $', $*, $+, $-, $., $^, $_, $`, $|, $~])},
@@ -600,6 +606,55 @@ language_range_list_sep(<< $\t, R/bits >>, Acc) -> language_range_list_sep(R, Ac
language_range_list_sep(<< $,, R/bits >>, Acc) -> language_range_list(R, Acc).
-ifdef(TEST).
+language_tag() ->
+ oneof([
+ [alpha()],
+ [alpha(), alpha()],
+ [alpha(), alpha(), alpha()],
+ [alpha(), alpha(), alpha(), alpha()],
+ [alpha(), alpha(), alpha(), alpha(), alpha()],
+ [alpha(), alpha(), alpha(), alpha(), alpha(), alpha()],
+ [alpha(), alpha(), alpha(), alpha(), alpha(), alpha(), alpha()],
+ [alpha(), alpha(), alpha(), alpha(), alpha(), alpha(), alpha(), alpha()]
+ ]).
+
+language_subtag() ->
+ [$-, oneof([
+ [alphanum()],
+ [alphanum(), alphanum()],
+ [alphanum(), alphanum(), alphanum()],
+ [alphanum(), alphanum(), alphanum(), alphanum()],
+ [alphanum(), alphanum(), alphanum(), alphanum(), alphanum()],
+ [alphanum(), alphanum(), alphanum(), alphanum(), alphanum(), alphanum()],
+ [alphanum(), alphanum(), alphanum(), alphanum(), alphanum(), alphanum(), alphanum()],
+ [alphanum(), alphanum(), alphanum(), alphanum(), alphanum(), alphanum(), alphanum(), alphanum()]
+ ])].
+
+language_range() ->
+ [language_tag(), list(language_subtag())].
+
+accept_language() ->
+ ?LET({R, W},
+ {language_range(), weight()},
+ {iolist_to_binary(R), W, iolist_to_binary([R, case W of
+ undefined -> [];
+ _ -> [<<";q=">>, qvalue_to_iodata(W)]
+ end])}
+ ).
+
+prop_parse_accept_language() ->
+ ?FORALL(L,
+ non_empty(list(accept_language())),
+ begin
+ << _, AcceptLanguage/binary >> = iolist_to_binary([[$,, A] || {_, _, A} <- L]),
+ ResL = parse_accept_language(AcceptLanguage),
+ CheckedL = [begin
+ ResR =:= ?INLINE_LOWERCASE_BC(R)
+ andalso (ResW =:= W orelse (W =:= undefined andalso ResW =:= 1000))
+ end || {{R, W, _}, {ResR, ResW}} <- lists:zip(L, ResL)],
+ [true] =:= lists:usort(CheckedL)
+ end).
+
parse_accept_language_test_() ->
Tests = [
{<<"da, en-gb;q=0.8, en;q=0.7">>, [