PageRenderTime 226ms CodeModel.GetById 222ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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