/examples/rest_server/src/app/app_test.e

http://github.com/jocelyn/EiffelWebReloaded · Specman e · 149 lines · 134 code · 15 blank · 0 comment · 9 complexity · a9b7fec9b8009dd75738c86fd6c0eaf9 MD5 · raw file

  1. note
  2. description: "Summary description for {APP_TEST}."
  3. date: "$Date$"
  4. revision: "$Revision$"
  5. class
  6. APP_TEST
  7. inherit
  8. APP_REQUEST_HANDLER
  9. redefine
  10. initialize
  11. end
  12. create
  13. make
  14. feature {NONE} -- Initialization
  15. make (a_path: STRING)
  16. do
  17. path := a_path
  18. description := "Return a simple test output "
  19. initialize
  20. end
  21. initialize
  22. do
  23. Precursor
  24. enable_request_method_get
  25. enable_format_text
  26. end
  27. feature -- Access
  28. authentication_required: BOOLEAN = False
  29. feature -- Execution
  30. execute_application (ctx: REST_REQUEST_CONTEXT; a_format: detachable STRING; a_args: detachable STRING)
  31. local
  32. rep: detachable REST_RESPONSE
  33. s: STRING
  34. v: STRING_8
  35. ht: HASH_TABLE_ITERATION_CURSOR [STRING_GENERAL, STRING_GENERAL]
  36. do
  37. if a_args /= Void and then not a_args.is_empty then
  38. if a_args.same_string ("crash") then
  39. rep := Void
  40. (create {DEVELOPER_EXCEPTION}).raise
  41. elseif a_args.starts_with ("env") then
  42. create rep.make (path)
  43. create s.make_empty
  44. ht := ctx.variables.new_cursor
  45. if a_format = Void or else a_format.same_string ("text") then
  46. rep.headers.put_content_type_text_plain
  47. from
  48. ht.start
  49. until
  50. ht.after
  51. loop
  52. s.append_string (ht.key.as_string_8 + " = " + ht.item.as_string_8 + "%N")
  53. ht.forth
  54. end
  55. elseif a_format.same_string ("html") then
  56. rep.headers.put_content_type_text_html
  57. from
  58. ht.start
  59. until
  60. ht.after
  61. loop
  62. s.append_string ("<li><strong>" + ht.key.as_string_8 + "</strong> = " + ht.item.as_string_8 + "</li>%N")
  63. ht.forth
  64. end
  65. elseif a_format.same_string ("json") then
  66. rep.headers.put_content_type_application_json
  67. s.append ("{ %"application%": %""+ path +"%" ")
  68. from
  69. ht.start
  70. until
  71. ht.after
  72. loop
  73. v := ht.item.as_string_8.string
  74. v.replace_substring_all ("\", "&#92;")
  75. s.append_string (",%"" + ht.key.as_string_8 + "%": %"" + v + "%"%N")
  76. ht.forth
  77. end
  78. s.append ("}%N")
  79. elseif a_format.same_string ("xml") then
  80. rep.headers.put_content_type_text_xml
  81. s.append ("<application name=%""+ path +"%">")
  82. from
  83. ht.start
  84. until
  85. ht.after
  86. loop
  87. s.append_string ("<variable name=%"" + ht.key.as_string_8 + "%">" + ht.item.as_string_8 + "</variable>%N")
  88. ht.forth
  89. end
  90. s.append ("</application>%N")
  91. else
  92. rep.headers.put_content_type_text_plain
  93. s.append ("Format not supported")
  94. end
  95. rep.set_message (s)
  96. else
  97. end
  98. end
  99. if rep = Void then
  100. create rep.make (path)
  101. rep.headers.put_content_type_text_html
  102. create s.make_empty
  103. s.append_string ("test")
  104. if attached ctx.environment_variable ("REQUEST_COUNT") as l_request_count then
  105. s.append_string ("(request_count="+ l_request_count +")<br/>%N")
  106. end
  107. s.append ("%N Try <a href=%"http://" + ctx.script_absolute_url (ctx.path_info + "/env") + "%">/test/env</a> to display all variables <br/>%N")
  108. s.append ("%N Try <a href=%"http://" + ctx.script_absolute_url (ctx.path_info + ".json/env") + "%">/test.json/env</a> to display all variables in JSON <br/>%N")
  109. s.append ("%N Try <a href=%"http://" + ctx.script_absolute_url (ctx.path_info + ".xml/env") + "%">/test.xml/env</a> to display all variables in XML <br/>%N")
  110. s.append ("%N Try <a href=%"http://" + ctx.script_absolute_url (ctx.path_info + ".html/env") + "%">/test.html/env</a> to display all variables in HTML<br/>%N")
  111. s.append ("%N Try <a href=%"http://" + ctx.script_absolute_url (ctx.path_info + "/crash") + "%">/crash</a> to demonstrate exception trace <br/>%N")
  112. if attached ctx.http_authorization_login_password as t then
  113. s.append_string ("Check login=" + t.login + "<br/>%N")
  114. end
  115. if ctx.authenticated and then attached ctx.authenticated_identifier as l_login then
  116. s.append_string ("Authenticated: login=" + l_login.as_string_8 + "<br/>%N")
  117. end
  118. s.append_string ("<br/>script_url(%"" + ctx.path_info + "%")=" + ctx.script_url (ctx.path_info) + "%N")
  119. rep.set_message (s)
  120. end
  121. ctx.output.put_string (rep.string)
  122. rep.recycle
  123. end
  124. note
  125. copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
  126. license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
  127. source: "[
  128. Eiffel Software
  129. 5949 Hollister Ave., Goleta, CA 93117 USA
  130. Telephone 805-685-1006, Fax 805-685-6869
  131. Website http://www.eiffel.com
  132. Customer support http://support.eiffel.com
  133. ]"
  134. end