Skip to content

Commit c4d362f

Browse files
committed
Added the add_cookie feature
Added test cases to check cookies in WSF_RESPONSE- Added mock classes use for test cases.
1 parent 4a35ff7 commit c4d362f

File tree

4 files changed

+334
-1
lines changed

4 files changed

+334
-1
lines changed

library/server/wsf/src/wsf_response.e

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,37 @@ feature -- Header output operation: helpers
316316
end
317317
end
318318

319+
feature -- Header add cookie
320+
321+
add_cookie (a_cookie: WSF_COOKIE)
322+
-- Add a Set-Cookie header field to the response, iff there is not exist
323+
-- a Set-Cookie header field with the same cookie-name.
324+
--| Servers SHOULD NOT include more than one Set-Cookie header field in
325+
--| the same response with the same cookie-name.
326+
local
327+
l_same_cookie_name: BOOLEAN
328+
l_cookie_header: STRING
329+
l_cn: STRING
330+
l_nv: STRING
331+
do
332+
across internal_header.headers as ic until l_same_cookie_name loop
333+
if ic.item.starts_with ("Set-Cookie") then
334+
l_cookie_header := ic.item.twin
335+
l_cookie_header.to_lower
336+
l_cn := a_cookie.name
337+
l_cn.to_lower
338+
l_nv := l_cookie_header.split (';').at (1).split (':').at (2)
339+
l_nv.adjust
340+
if l_nv.starts_with (l_cn) then
341+
l_same_cookie_name := True
342+
end
343+
end
344+
end
345+
if not l_same_cookie_name then
346+
internal_header.add_header (a_cookie.header_line)
347+
end
348+
end
349+
319350
feature -- Output report
320351

321352
transfered_content_length: NATURAL_64
@@ -520,7 +551,7 @@ feature -- Error reporting
520551
end
521552
522553
note
523-
copyright: "2011-2014, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Colin Adams, Eiffel Software and others"
554+
copyright: "2011-2015, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Colin Adams, Eiffel Software and others"
524555
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
525556
source: "[
526557
Eiffel Software
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
note
2+
description: "Summary description for {TEST_WSF_RESPONSE_TEST_SUITE}."
3+
date: "$Date$"
4+
revision: "$Revision$"
5+
6+
class
7+
TEST_WSF_RESPONSE_TEST_SUITE
8+
9+
inherit
10+
WSF_TO_WGI_SERVICE
11+
rename
12+
default_create as df_wgi,
13+
execute as execute_wgi
14+
end
15+
EQA_TEST_SET
16+
redefine
17+
on_prepare
18+
select
19+
default_create
20+
end
21+
22+
feature {NONE} -- Events
23+
24+
on_prepare
25+
do
26+
make_from_service (create {WSF_SERVICE_NULL})
27+
end
28+
29+
feature -- Test Cases
30+
31+
test_add_single_cookie
32+
local
33+
w_res: WSF_RESPONSE
34+
l_cookie: WSF_COOKIE
35+
l_header: WSF_HEADER
36+
l_res: WGI_RESPONSE_NULL
37+
do
38+
create {WGI_RESPONSE_NULL} l_res.make
39+
create w_res.make_from_wgi (l_res)
40+
41+
create l_header.make
42+
l_header.put_content_type_text_html
43+
44+
create l_cookie.make ("user_id", "u12345")
45+
l_cookie.set_domain ("www.example.com")
46+
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
47+
l_cookie.set_path ("/")
48+
l_cookie.set_secure (True)
49+
l_cookie.set_http_only (True)
50+
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"))
51+
52+
w_res.put_header_text (l_header.string)
53+
w_res.add_cookie (l_cookie)
54+
w_res.set_status_code ({HTTP_STATUS_CODE}.ok)
55+
w_res.put_string ("Test")
56+
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") )
57+
end
58+
59+
60+
test_add_multiple_cookiewith_similar_cookie_name
61+
local
62+
w_res: WSF_RESPONSE
63+
l_cookie: WSF_COOKIE
64+
l_header: WSF_HEADER
65+
l_res: WGI_RESPONSE_NULL
66+
do
67+
create {WGI_RESPONSE_NULL} l_res.make
68+
create w_res.make_from_wgi (l_res)
69+
70+
create l_header.make
71+
l_header.put_content_type_text_html
72+
73+
create l_cookie.make ("user_id", "u12345")
74+
l_cookie.set_domain ("www.example.com")
75+
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
76+
l_cookie.set_path ("/")
77+
l_cookie.set_secure (True)
78+
l_cookie.set_http_only (True)
79+
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"))
80+
w_res.put_header_text (l_header.string)
81+
w_res.add_cookie (l_cookie)
82+
83+
84+
create l_cookie.make ("user_id", "newUser")
85+
l_cookie.set_domain ("www.example.com")
86+
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
87+
l_cookie.set_path ("/")
88+
l_cookie.set_secure (True)
89+
l_cookie.set_http_only (True)
90+
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"))
91+
92+
w_res.add_cookie (l_cookie) -- Ignored
93+
w_res.set_status_code ({HTTP_STATUS_CODE}.ok)
94+
w_res.put_string ("Test")
95+
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") )
96+
end
97+
98+
99+
test_add_multiple_cookie_with_similar_cookie_name_2
100+
local
101+
w_res: WSF_RESPONSE
102+
l_cookie: WSF_COOKIE
103+
l_header: WSF_HEADER
104+
l_res: WGI_RESPONSE_NULL
105+
do
106+
create {WGI_RESPONSE_NULL} l_res.make
107+
create w_res.make_from_wgi (l_res)
108+
109+
create l_header.make
110+
l_header.put_content_type_text_html
111+
w_res.put_header_text (l_header.string)
112+
113+
create l_cookie.make ("user_id", "u12345")
114+
l_cookie.set_domain ("www.example.com")
115+
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
116+
l_cookie.set_path ("/")
117+
l_cookie.set_secure (True)
118+
l_cookie.set_http_only (True)
119+
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"))
120+
121+
122+
w_res.add_cookie (l_cookie)
123+
create l_cookie.make ("user_id", "newUser")
124+
l_cookie.set_domain ("www.example.com")
125+
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
126+
l_cookie.set_path ("/")
127+
l_cookie.set_secure (True)
128+
l_cookie.set_http_only (True)
129+
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"))
130+
131+
w_res.add_cookie (l_cookie) -- Ignored
132+
133+
134+
create l_cookie.make ("ewf_sessionid", "test")
135+
l_cookie.set_domain ("www.example.com")
136+
l_cookie.set_expiration ("Sat, 18 Apr 2015 21:22:05 GMT")
137+
l_cookie.set_path ("/")
138+
l_cookie.set_secure (True)
139+
l_cookie.set_http_only (True)
140+
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"))
141+
142+
w_res.add_cookie (l_cookie)
143+
w_res.set_status_code ({HTTP_STATUS_CODE}.ok)
144+
w_res.put_string ("Test")
145+
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") )
146+
end
147+
end
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
note
2+
description: "Summary description for {WGI_RESPONSE_NULL}."
3+
date: "$Date$"
4+
revision: "$Revision$"
5+
6+
class
7+
WGI_RESPONSE_NULL
8+
9+
inherit
10+
11+
WGI_RESPONSE
12+
13+
create
14+
make
15+
16+
feature {NONE} -- Initialization
17+
18+
make
19+
do
20+
create output.make_empty
21+
create error.make_empty
22+
end
23+
24+
feature {WGI_CONNECTOR, WGI_SERVICE} -- Commit
25+
26+
commit
27+
-- Commit the current response
28+
do
29+
end
30+
31+
feature -- Status report
32+
33+
status_committed: BOOLEAN
34+
-- Status code set and committed?
35+
36+
header_committed: BOOLEAN
37+
-- Header committed?
38+
39+
message_committed: BOOLEAN
40+
-- Message committed?
41+
42+
message_writable: BOOLEAN
43+
-- Can message be written?
44+
do
45+
Result := status_is_set and header_committed
46+
end
47+
48+
feature -- Status setting
49+
50+
status_is_set: BOOLEAN
51+
-- Is status set?
52+
do
53+
Result := status_code > 0
54+
end
55+
56+
set_status_code (a_code: INTEGER; a_reason_phrase: detachable READABLE_STRING_8)
57+
-- Set response status code
58+
-- Should be done before sending any data back to the client
59+
do
60+
status_code := a_code
61+
status_reason_phrase := a_reason_phrase
62+
if attached a_reason_phrase as l_rp then
63+
output.prepend (l_rp)
64+
end
65+
output.prepend (" ")
66+
output.prepend (a_code.out)
67+
output.append ("%R%N")
68+
status_committed := True
69+
end
70+
71+
status_code: INTEGER
72+
-- Response status
73+
74+
status_reason_phrase: detachable READABLE_STRING_8
75+
-- Custom status reason phrase for the Response (optional)
76+
77+
feature -- Header output operation
78+
79+
put_header_text (a_text: READABLE_STRING_8)
80+
do
81+
output.append (a_text)
82+
output.append ("%R%N")
83+
header_committed := True
84+
end
85+
86+
feature -- Output operation
87+
88+
put_character (c: CHARACTER_8)
89+
-- Send the character `c'
90+
do
91+
output.append_character (c)
92+
end
93+
94+
put_string (s: READABLE_STRING_8)
95+
-- Send the string `s'
96+
do
97+
output.append (s)
98+
end
99+
100+
put_substring (s: READABLE_STRING_8; start_index, end_index: INTEGER)
101+
-- Send the substring `start_index:end_index]'
102+
--| Could be optimized according to the target output
103+
do
104+
output.append_substring (s, start_index, end_index)
105+
end
106+
107+
flush
108+
do
109+
output.wipe_out
110+
end
111+
112+
feature -- Error reporting
113+
114+
put_error (a_message: READABLE_STRING_8)
115+
-- Report error described by `a_message'
116+
-- This might be used by the underlying connector
117+
do
118+
if attached error as err then
119+
err.append (a_message)
120+
end
121+
end
122+
123+
feature {EQA_TEST_SET} -- Implementation: Access
124+
125+
output: STRING
126+
-- Server output channel
127+
128+
error: detachable STRING
129+
-- Server output channel
130+
131+
132+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
note
2+
description: "Summary description for {WSF_SERVICE_NULL}."
3+
author: ""
4+
date: "$Date$"
5+
revision: "$Revision$"
6+
7+
class
8+
WSF_SERVICE_NULL
9+
10+
inherit
11+
12+
WSF_SERVICE
13+
14+
15+
feature -- Execute
16+
execute (req: WSF_REQUEST; res: WSF_RESPONSE)
17+
-- Execute the request
18+
-- See `req.input' for input stream
19+
-- `req.meta_variables' for the CGI meta variable
20+
-- and `res' for output buffer
21+
do
22+
end
23+
end

0 commit comments

Comments
 (0)