Added the add_cookie feature

Added test cases to check cookies in WSF_RESPONSE-
Added mock classes use for test cases.
This commit is contained in:
jvelilla
2015-03-17 14:27:53 -03:00
parent 4a35ff7b77
commit c4d362ff31
4 changed files with 334 additions and 1 deletions

View File

@@ -316,6 +316,37 @@ feature -- Header output operation: helpers
end
end
feature -- Header add cookie
add_cookie (a_cookie: WSF_COOKIE)
-- Add a Set-Cookie header field to the response, iff there is not exist
-- a Set-Cookie header field with the same cookie-name.
--| Servers SHOULD NOT include more than one Set-Cookie header field in
--| the same response with the same cookie-name.
local
l_same_cookie_name: BOOLEAN
l_cookie_header: STRING
l_cn: STRING
l_nv: STRING
do
across internal_header.headers as ic until l_same_cookie_name loop
if ic.item.starts_with ("Set-Cookie") then
l_cookie_header := ic.item.twin
l_cookie_header.to_lower
l_cn := a_cookie.name
l_cn.to_lower
l_nv := l_cookie_header.split (';').at (1).split (':').at (2)
l_nv.adjust
if l_nv.starts_with (l_cn) then
l_same_cookie_name := True
end
end
end
if not l_same_cookie_name then
internal_header.add_header (a_cookie.header_line)
end
end
feature -- Output report
transfered_content_length: NATURAL_64
@@ -520,7 +551,7 @@ feature -- Error reporting
end
note
copyright: "2011-2014, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Colin Adams, Eiffel Software and others"
copyright: "2011-2015, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Colin Adams, Eiffel Software and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
source: "[
Eiffel Software

View File

@@ -0,0 +1,147 @@
note
description: "Summary description for {TEST_WSF_RESPONSE_TEST_SUITE}."
date: "$Date$"
revision: "$Revision$"
class
TEST_WSF_RESPONSE_TEST_SUITE
inherit
WSF_TO_WGI_SERVICE
rename
default_create as df_wgi,
execute as execute_wgi
end
EQA_TEST_SET
redefine
on_prepare
select
default_create
end
feature {NONE} -- Events
on_prepare
do
make_from_service (create {WSF_SERVICE_NULL})
end
feature -- Test Cases
test_add_single_cookie
local
w_res: WSF_RESPONSE
l_cookie: WSF_COOKIE
l_header: WSF_HEADER
l_res: WGI_RESPONSE_NULL
do
create {WGI_RESPONSE_NULL} l_res.make
create w_res.make_from_wgi (l_res)
create l_header.make
l_header.put_content_type_text_html
create l_cookie.make ("user_id", "u12345")
l_cookie.set_domain ("www.example.com")
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
l_cookie.set_path ("/")
l_cookie.set_secure (True)
l_cookie.set_http_only (True)
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly"))
w_res.put_header_text (l_header.string)
w_res.add_cookie (l_cookie)
w_res.set_status_code ({HTTP_STATUS_CODE}.ok)
w_res.put_string ("Test")
assert ("Expected", l_res.output.same_string("200 %R%NContent-Type: text/html%R%NSet-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly%R%N%R%NTest") )
end
test_add_multiple_cookiewith_similar_cookie_name
local
w_res: WSF_RESPONSE
l_cookie: WSF_COOKIE
l_header: WSF_HEADER
l_res: WGI_RESPONSE_NULL
do
create {WGI_RESPONSE_NULL} l_res.make
create w_res.make_from_wgi (l_res)
create l_header.make
l_header.put_content_type_text_html
create l_cookie.make ("user_id", "u12345")
l_cookie.set_domain ("www.example.com")
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
l_cookie.set_path ("/")
l_cookie.set_secure (True)
l_cookie.set_http_only (True)
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly"))
w_res.put_header_text (l_header.string)
w_res.add_cookie (l_cookie)
create l_cookie.make ("user_id", "newUser")
l_cookie.set_domain ("www.example.com")
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
l_cookie.set_path ("/")
l_cookie.set_secure (True)
l_cookie.set_http_only (True)
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=newUser; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly"))
w_res.add_cookie (l_cookie) -- Ignored
w_res.set_status_code ({HTTP_STATUS_CODE}.ok)
w_res.put_string ("Test")
assert ("Expected", l_res.output.same_string("200 %R%NContent-Type: text/html%R%NSet-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly%R%N%R%NTest") )
end
test_add_multiple_cookie_with_similar_cookie_name_2
local
w_res: WSF_RESPONSE
l_cookie: WSF_COOKIE
l_header: WSF_HEADER
l_res: WGI_RESPONSE_NULL
do
create {WGI_RESPONSE_NULL} l_res.make
create w_res.make_from_wgi (l_res)
create l_header.make
l_header.put_content_type_text_html
w_res.put_header_text (l_header.string)
create l_cookie.make ("user_id", "u12345")
l_cookie.set_domain ("www.example.com")
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
l_cookie.set_path ("/")
l_cookie.set_secure (True)
l_cookie.set_http_only (True)
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly"))
w_res.add_cookie (l_cookie)
create l_cookie.make ("user_id", "newUser")
l_cookie.set_domain ("www.example.com")
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
l_cookie.set_path ("/")
l_cookie.set_secure (True)
l_cookie.set_http_only (True)
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: user_id=newUser; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly"))
w_res.add_cookie (l_cookie) -- Ignored
create l_cookie.make ("ewf_sessionid", "test")
l_cookie.set_domain ("www.example.com")
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
l_cookie.set_path ("/")
l_cookie.set_secure (True)
l_cookie.set_http_only (True)
assert("Expected", l_cookie.header_line.same_string ("Set-Cookie: ewf_sessionid=test; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly"))
w_res.add_cookie (l_cookie)
w_res.set_status_code ({HTTP_STATUS_CODE}.ok)
w_res.put_string ("Test")
assert ("Expected", l_res.output.same_string("200 %R%NContent-Type: text/html%R%NSet-Cookie: user_id=u12345; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly%R%NSet-Cookie: ewf_sessionid=test; Domain=www.example.com; Path=/; Expires=Sat, 18 Apr 2015 21:22:05 GMT; Max-Age=-1; Secure; HttpOnly%R%N%R%NTest") )
end
end

View File

@@ -0,0 +1,132 @@
note
description: "Summary description for {WGI_RESPONSE_NULL}."
date: "$Date$"
revision: "$Revision$"
class
WGI_RESPONSE_NULL
inherit
WGI_RESPONSE
create
make
feature {NONE} -- Initialization
make
do
create output.make_empty
create error.make_empty
end
feature {WGI_CONNECTOR, WGI_SERVICE} -- Commit
commit
-- Commit the current response
do
end
feature -- Status report
status_committed: BOOLEAN
-- Status code set and committed?
header_committed: BOOLEAN
-- Header committed?
message_committed: BOOLEAN
-- Message committed?
message_writable: BOOLEAN
-- Can message be written?
do
Result := status_is_set and header_committed
end
feature -- Status setting
status_is_set: BOOLEAN
-- Is status set?
do
Result := status_code > 0
end
set_status_code (a_code: INTEGER; a_reason_phrase: detachable READABLE_STRING_8)
-- Set response status code
-- Should be done before sending any data back to the client
do
status_code := a_code
status_reason_phrase := a_reason_phrase
if attached a_reason_phrase as l_rp then
output.prepend (l_rp)
end
output.prepend (" ")
output.prepend (a_code.out)
output.append ("%R%N")
status_committed := True
end
status_code: INTEGER
-- Response status
status_reason_phrase: detachable READABLE_STRING_8
-- Custom status reason phrase for the Response (optional)
feature -- Header output operation
put_header_text (a_text: READABLE_STRING_8)
do
output.append (a_text)
output.append ("%R%N")
header_committed := True
end
feature -- Output operation
put_character (c: CHARACTER_8)
-- Send the character `c'
do
output.append_character (c)
end
put_string (s: READABLE_STRING_8)
-- Send the string `s'
do
output.append (s)
end
put_substring (s: READABLE_STRING_8; start_index, end_index: INTEGER)
-- Send the substring `start_index:end_index]'
--| Could be optimized according to the target output
do
output.append_substring (s, start_index, end_index)
end
flush
do
output.wipe_out
end
feature -- Error reporting
put_error (a_message: READABLE_STRING_8)
-- Report error described by `a_message'
-- This might be used by the underlying connector
do
if attached error as err then
err.append (a_message)
end
end
feature {EQA_TEST_SET} -- Implementation: Access
output: STRING
-- Server output channel
error: detachable STRING
-- Server output channel
end

View File

@@ -0,0 +1,23 @@
note
description: "Summary description for {WSF_SERVICE_NULL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_SERVICE_NULL
inherit
WSF_SERVICE
feature -- Execute
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
-- Execute the request
-- See `req.input' for input stream
-- `req.meta_variables' for the CGI meta variable
-- and `res' for output buffer
do
end
end