dispatcher_prop.erl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. %% Copyright (c) 2011, Magnus Klaar <magnus.klaar@gmail.com>
  2. %% Copyright (c) 2011, Loïc Hoguin <essen@ninenines.eu>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(dispatcher_prop).
  16. -include_lib("proper/include/proper.hrl").
  17. %% Generators.
  18. hostname_head_char() ->
  19. oneof([choose($a, $z), choose($A, $Z), choose($0, $9)]).
  20. hostname_char() ->
  21. oneof([choose($a, $z), choose($A, $Z), choose($0, $9), $-]).
  22. hostname_label() ->
  23. ?SUCHTHAT(Label, [hostname_head_char()|list(hostname_char())],
  24. length(Label) < 64).
  25. hostname() ->
  26. ?SUCHTHAT(Hostname,
  27. ?LET(Labels, list(hostname_label()), string:join(Labels, ".")),
  28. length(Hostname) > 0 andalso length(Hostname) =< 255).
  29. port_number() ->
  30. choose(1, 16#ffff).
  31. port_str() ->
  32. oneof(["", ?LET(Port, port_number(), ":" ++ integer_to_list(Port))]).
  33. server() ->
  34. ?LET({Hostname, PortStr}, {hostname(), port_str()},
  35. list_to_binary(Hostname ++ PortStr)).
  36. %% Properties.
  37. prop_split_host_symmetric() ->
  38. ?FORALL(Server, server(),
  39. begin case cowboy_dispatcher:split_host(Server) of
  40. {Tokens, RawHost, undefined} ->
  41. (Server == RawHost) and (Server == binary_join(Tokens, "."));
  42. {Tokens, RawHost, Port} ->
  43. PortBin = (list_to_binary(":" ++ integer_to_list(Port))),
  44. (Server == << RawHost/binary, PortBin/binary >>)
  45. and (Server == << (binary_join(Tokens, "."))/binary,
  46. PortBin/binary >>)
  47. end end).
  48. %% Internal.
  49. %% Contributed by MononcQc on #erlounge.
  50. binary_join(Flowers, Leaf) ->
  51. case Flowers of
  52. [] -> <<>>;
  53. [Petal|Pot] -> iolist_to_binary(
  54. [Petal | [[Leaf | Pollen] || Pollen <- Pot]])
  55. end.