PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mochiweb_request_tests.erl

http://github.com/basho/mochiweb
Erlang | 152 lines | 119 code | 33 blank | 0 comment | 0 complexity | 3ac9447edc1eef131b81dd29a7062fca MD5 | raw file
Possible License(s): MIT
  1. -module(mochiweb_request_tests).
  2. -ifdef(TEST).
  3. -include_lib("eunit/include/eunit.hrl").
  4. accepts_content_type_test() ->
  5. Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  6. mochiweb_headers:make([{"Accept", "multipart/related"}])),
  7. ?assertEqual(true, Req1:accepts_content_type("multipart/related")),
  8. ?assertEqual(true, Req1:accepts_content_type(<<"multipart/related">>)),
  9. Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  10. mochiweb_headers:make([{"Accept", "text/html"}])),
  11. ?assertEqual(false, Req2:accepts_content_type("multipart/related")),
  12. Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  13. mochiweb_headers:make([{"Accept", "text/html, multipart/*"}])),
  14. ?assertEqual(true, Req3:accepts_content_type("multipart/related")),
  15. Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  16. mochiweb_headers:make([{"Accept", "text/html, multipart/*; q=0.0"}])),
  17. ?assertEqual(false, Req4:accepts_content_type("multipart/related")),
  18. Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  19. mochiweb_headers:make([{"Accept", "text/html, multipart/*; q=0"}])),
  20. ?assertEqual(false, Req5:accepts_content_type("multipart/related")),
  21. Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  22. mochiweb_headers:make([{"Accept", "text/html, */*; q=0.0"}])),
  23. ?assertEqual(false, Req6:accepts_content_type("multipart/related")),
  24. Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  25. mochiweb_headers:make([{"Accept", "multipart/*; q=0.0, */*"}])),
  26. ?assertEqual(false, Req7:accepts_content_type("multipart/related")),
  27. Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  28. mochiweb_headers:make([{"Accept", "*/*; q=0.0, multipart/*"}])),
  29. ?assertEqual(true, Req8:accepts_content_type("multipart/related")),
  30. Req9 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  31. mochiweb_headers:make([{"Accept", "*/*; q=0.0, multipart/related"}])),
  32. ?assertEqual(true, Req9:accepts_content_type("multipart/related")),
  33. Req10 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  34. mochiweb_headers:make([{"Accept", "text/html; level=1"}])),
  35. ?assertEqual(true, Req10:accepts_content_type("text/html;level=1")),
  36. Req11 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  37. mochiweb_headers:make([{"Accept", "text/html; level=1, text/html"}])),
  38. ?assertEqual(true, Req11:accepts_content_type("text/html")),
  39. Req12 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  40. mochiweb_headers:make([{"Accept", "text/html; level=1; q=0.0, text/html"}])),
  41. ?assertEqual(false, Req12:accepts_content_type("text/html;level=1")),
  42. Req13 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  43. mochiweb_headers:make([{"Accept", "text/html; level=1; q=0.0, text/html"}])),
  44. ?assertEqual(false, Req13:accepts_content_type("text/html; level=1")),
  45. Req14 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  46. mochiweb_headers:make([{"Accept", "text/html;level=1;q=0.1, text/html"}])),
  47. ?assertEqual(true, Req14:accepts_content_type("text/html; level=1")).
  48. accepted_encodings_test() ->
  49. Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  50. mochiweb_headers:make([])),
  51. ?assertEqual(["identity"],
  52. Req1:accepted_encodings(["gzip", "identity"])),
  53. Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  54. mochiweb_headers:make([{"Accept-Encoding", "gzip, deflate"}])),
  55. ?assertEqual(["gzip", "identity"],
  56. Req2:accepted_encodings(["gzip", "identity"])),
  57. Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  58. mochiweb_headers:make([{"Accept-Encoding", "gzip;q=0.5, deflate"}])),
  59. ?assertEqual(["deflate", "gzip", "identity"],
  60. Req3:accepted_encodings(["gzip", "deflate", "identity"])),
  61. Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  62. mochiweb_headers:make([{"Accept-Encoding", "identity, *;q=0"}])),
  63. ?assertEqual(["identity"],
  64. Req4:accepted_encodings(["gzip", "deflate", "identity"])),
  65. Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  66. mochiweb_headers:make([{"Accept-Encoding", "gzip; q=0.1, *;q=0"}])),
  67. ?assertEqual(["gzip"],
  68. Req5:accepted_encodings(["gzip", "deflate", "identity"])),
  69. Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  70. mochiweb_headers:make([{"Accept-Encoding", "gzip; q=, *;q=0"}])),
  71. ?assertEqual(bad_accept_encoding_value,
  72. Req6:accepted_encodings(["gzip", "deflate", "identity"])),
  73. Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  74. mochiweb_headers:make([{"Accept-Encoding", "gzip;q=2.0, *;q=0"}])),
  75. ?assertEqual(bad_accept_encoding_value,
  76. Req7:accepted_encodings(["gzip", "identity"])),
  77. Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  78. mochiweb_headers:make([{"Accept-Encoding", "deflate, *;q=0.0"}])),
  79. ?assertEqual([],
  80. Req8:accepted_encodings(["gzip", "identity"])).
  81. accepted_content_types_test() ->
  82. Req1 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  83. mochiweb_headers:make([{"Accept", "text/html"}])),
  84. ?assertEqual(["text/html"],
  85. Req1:accepted_content_types(["text/html", "application/json"])),
  86. Req2 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  87. mochiweb_headers:make([{"Accept", "text/html, */*;q=0"}])),
  88. ?assertEqual(["text/html"],
  89. Req2:accepted_content_types(["text/html", "application/json"])),
  90. Req3 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  91. mochiweb_headers:make([{"Accept", "text/*, */*;q=0"}])),
  92. ?assertEqual(["text/html"],
  93. Req3:accepted_content_types(["text/html", "application/json"])),
  94. Req4 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  95. mochiweb_headers:make([{"Accept", "text/*;q=0.8, */*;q=0.5"}])),
  96. ?assertEqual(["text/html", "application/json"],
  97. Req4:accepted_content_types(["application/json", "text/html"])),
  98. Req5 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  99. mochiweb_headers:make([{"Accept", "text/*;q=0.8, */*;q=0.5"}])),
  100. ?assertEqual(["text/html", "application/json"],
  101. Req5:accepted_content_types(["text/html", "application/json"])),
  102. Req6 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  103. mochiweb_headers:make([{"Accept", "text/*;q=0.5, */*;q=0.5"}])),
  104. ?assertEqual(["application/json", "text/html"],
  105. Req6:accepted_content_types(["application/json", "text/html"])),
  106. Req7 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  107. mochiweb_headers:make(
  108. [{"Accept", "text/html;q=0.5, application/json;q=0.5"}])),
  109. ?assertEqual(["application/json", "text/html"],
  110. Req7:accepted_content_types(["application/json", "text/html"])),
  111. Req8 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  112. mochiweb_headers:make([{"Accept", "text/html"}])),
  113. ?assertEqual([],
  114. Req8:accepted_content_types(["application/json"])),
  115. Req9 = mochiweb_request:new(nil, 'GET', "/foo", {1, 1},
  116. mochiweb_headers:make([{"Accept", "text/*;q=0.9, text/html;q=0.5, */*;q=0.7"}])),
  117. ?assertEqual(["application/json", "text/html"],
  118. Req9:accepted_content_types(["text/html", "application/json"])).
  119. -endif.