t_cgi.ml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. open Alcotest
  2. open Seppo_lib
  3. let set_up () =
  4. Mirage_crypto_rng_lwt.initialize (module Mirage_crypto_rng.Fortuna);
  5. Unix.chdir "../../../test/"
  6. let tc_script_url_dir () =
  7. "/seppo.cgi" |> Cgi.Request.script_url_dir |> check string __LOC__ "/";
  8. "/apchk.cgi" |> Cgi.Request.script_url_dir |> check string __LOC__ "/";
  9. "/cgi-bin/subdi/seppo.cgi" |> Cgi.Request.script_url_dir |> check string __LOC__ "/subdi/";
  10. "/suhu/.cgi" |> Cgi.Request.script_url_dir |> check string __LOC__ "/suhu/";
  11. "/cgi-bin/suha/.cgi" |> Cgi.Request.script_url_dir |> check string __LOC__ "/suha/";
  12. "/cgi-bun/suha/.cgi" |> Cgi.Request.script_url_dir |> check string __LOC__ "/cgi-bun/suha/";
  13. ()
  14. let tc_from_env () =
  15. let env l s = l |> List.assoc_opt s in
  16. let r = Cgi.Request.(from_env ~getenv_opt:(env [
  17. (* hCONTENT_LENGTH, "CONTENT_LENGTH" ; *)
  18. (* hCONTENT_TYPE, "CONTENT_TYPE" ; *)
  19. (* hHTTP_COOKIE, "HTTP_COOKIE" ; *)
  20. (* hHTTP_HOST, "example.com" ; *)
  21. (* hHTTP_USER_AGENT, "agent" ; *)
  22. (* hHTTP_X_FORWARDED_FOR, "217.88.75.197" ; *)
  23. (* hHTTP_X_FORWARDED_PROTO, "https" ; *)
  24. (* hHTTPS, "HTTPS" ; *)
  25. (* hPATH_INFO, "path_info" ; *)
  26. (* hQUERY_STRING, "" ; *)
  27. hREMOTE_ADDR, "192.0.2.7" ; (* https://www.rfc-editor.org/rfc/rfc5735#section-3 *)
  28. hREQUEST_METHOD, "GET" ;
  29. (* hREQUEST_URI, "/i.cgi" ; *)
  30. hSCRIPT_NAME, "/i.cgi" ;
  31. hSERVER_NAME, "example.com" ;
  32. hSERVER_PORT, "80" ;
  33. ]) () |> consolidate |> proxy) |> Result.get_ok in
  34. r.host |> check string __LOC__ "example.com";
  35. r.path_info |> check string __LOC__ "";
  36. r.remote_addr |> check string __LOC__ "192.0.2.7";
  37. r.scheme |> check string __LOC__ "http";
  38. r.script_name |> check string __LOC__ "/i.cgi";
  39. r.server_port |> check string __LOC__ "80";
  40. ()
  41. let tc_from_env_proxy () =
  42. let env l s = l |> List.assoc_opt s in
  43. let r = Cgi.Request.(from_env ~getenv_opt:(env [
  44. (* hCONTENT_LENGTH, "CONTENT_LENGTH" ; *)
  45. (* hCONTENT_TYPE, "CONTENT_TYPE" ; *)
  46. (* hHTTP_COOKIE, "HTTP_COOKIE" ; *)
  47. (* hHTTP_HOST, "example.com" ; *)
  48. (* hHTTP_USER_AGENT, "agent" ; *)
  49. hHTTP_X_FORWARDED_FOR, "192.0.2.7" ; (* https://www.rfc-editor.org/rfc/rfc5735#section-3 *)
  50. hHTTP_X_FORWARDED_PROTO, "https" ;
  51. (* hHTTPS, "HTTPS" ; *)
  52. (* hPATH_INFO, "path_info" ; *)
  53. (* hQUERY_STRING, "" ; *)
  54. hREMOTE_ADDR, "127.0.0.1" ;
  55. hREQUEST_METHOD, "GET" ;
  56. (* hREQUEST_URI, "/i.cgi" ; *)
  57. hSCRIPT_NAME, "/i.cgi" ;
  58. hSERVER_NAME, "example.com" ;
  59. hSERVER_PORT, "80" ;
  60. ]) () |> consolidate |> proxy) |> Result.get_ok in
  61. r.host |> check string __LOC__ "example.com";
  62. r.path_info |> check string __LOC__ "";
  63. r.remote_addr |> check string __LOC__ "192.0.2.7";
  64. r.scheme |> check string __LOC__ "https";
  65. r.script_name |> check string __LOC__ "/i.cgi";
  66. r.server_port |> check string __LOC__ "443";
  67. ()
  68. let tc_abs () =
  69. let env l s = l |> List.assoc_opt s in
  70. let r = Cgi.Request.(from_env ~getenv_opt:(env [
  71. hHTTP_X_FORWARDED_FOR, "192.0.2.7" ; (* https://www.rfc-editor.org/rfc/rfc5735#section-3 *)
  72. hHTTP_X_FORWARDED_PROTO, "https" ;
  73. hREMOTE_ADDR, "127.0.0.1" ;
  74. hREQUEST_METHOD, "GET" ;
  75. hSCRIPT_NAME, "/i.cgi" ;
  76. hSERVER_NAME, "example.com" ;
  77. hSERVER_PORT, "80" ;
  78. ]) () |> proxy) |> Result.get_ok in
  79. r |> Cgi.Request.srvr
  80. |> Uri.to_string |> check string __LOC__ "https://example.com";
  81. r |> Cgi.Request.abs
  82. |> Uri.to_string |> check string __LOC__ "https://example.com/i.cgi"
  83. let () =
  84. run
  85. "seppo_suite" [
  86. __FILE__ , [
  87. "set_up" , `Quick, set_up;
  88. "tc_script_url_dir ", `Quick, tc_script_url_dir ;
  89. "tc_from_env " , `Quick, tc_from_env ;
  90. "tc_from_env_proxy ", `Quick, tc_from_env_proxy ;
  91. "tc_abs " , `Quick, tc_abs ;
  92. ]
  93. ]