Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
bwinf
medal
Commits
a78d7ea0
Commit
a78d7ea0
authored
Apr 03, 2019
by
Daniel Brüning
Browse files
Added also a test to see if the logout function works correct
parent
74727437
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main.rs
View file @
a78d7ea0
...
...
@@ -284,6 +284,13 @@ mod tests {
stop_tx
.send
(())
.unwrap
();
}
fn
login_and_fn
(
port
:
u16
,
client
:
&
reqwest
::
Client
,
username
:
&
str
,
password
:
&
str
)
->
reqwest
::
Response
{
let
params
=
[(
"username"
,
username
),
(
"password"
,
password
)];
let
mut
resp
=
client
.post
(
&
format!
(
"http://localhost:{}/login"
,
port
))
.form
(
&
params
)
.send
()
.unwrap
();
return
resp
;
}
#[test]
fn
start_server_and_check_request
()
{
start_server_and_fn
(
8080
,
||{
...
...
@@ -307,10 +314,8 @@ mod tests {
#[test]
fn
check_login_wrong_credentials
()
{
start_server_and_fn
(
8081
,
||{
let
params
=
[(
"username"
,
"nonexistingusername"
),
(
"password"
,
"wrongpassword"
)];
let
client
=
reqwest
::
Client
::
new
()
.unwrap
();
let
mut
resp
=
client
.post
(
"http://localhost:8081/login"
)
.form
(
&
params
)
.send
()
.unwrap
();
let
mut
resp
=
login_and_fn
(
8081
,
&
client
,
"nonexistingusername"
,
"wrongpassword"
);
let
mut
content
=
String
::
new
();
resp
.read_to_string
(
&
mut
content
);
assert
!
(
content
.contains
(
"<h1>Login</h1>"
));
...
...
@@ -322,11 +327,9 @@ mod tests {
#[test]
fn
start_server_and_check_login
()
{
start_server_and_create_user_and_fn
(
8082
,
||{
let
params
=
[(
"username"
,
"testusr"
),
(
"password"
,
"testpw"
)];
let
mut
client
=
reqwest
::
Client
::
new
()
.unwrap
();
client
.redirect
(
reqwest
::
RedirectPolicy
::
custom
(|
attempt
|
{
attempt
.stop
()}));
let
mut
resp
=
client
.post
(
"http://localhost:8082/login"
)
.form
(
&
params
)
.send
()
.unwrap
();
let
mut
resp
=
login_and_fn
(
8082
,
&
client
,
"testusr"
,
"testpw"
);
let
mut
content
=
String
::
new
();
resp
.read_to_string
(
&
mut
content
);
assert
!
(
!
content
.contains
(
"Error"
));
...
...
@@ -337,7 +340,6 @@ mod tests {
None
=>
panic!
(
"No setCookie."
),
Some
(
cookie
)
=>
if
cookie
.len
()
==
1
{
let
newCookie
=
reqwest
::
header
::
Cookie
(
cookie
.to_vec
());
println!
(
"newCookie: {:?}"
,
newCookie
);
let
mut
newResp
=
client
.get
(
"http://localhost:8082"
)
.header
(
newCookie
)
.send
()
.unwrap
();
...
...
@@ -353,4 +355,41 @@ mod tests {
})
}
#[test]
fn
start_server_and_check_logout
()
{
start_server_and_create_user_and_fn
(
8083
,
||{
let
mut
client
=
reqwest
::
Client
::
new
()
.unwrap
();
client
.redirect
(
reqwest
::
RedirectPolicy
::
custom
(|
attempt
|
{
attempt
.stop
()}));
let
mut
resp
=
login_and_fn
(
8083
,
&
client
,
"testusr"
,
"testpw"
);
let
mut
content
=
String
::
new
();
resp
.read_to_string
(
&
mut
content
);
assert
!
(
!
content
.contains
(
"Error"
));
let
header
=
resp
.headers
();
let
setCookie
=
header
.get
::
<
reqwest
::
header
::
SetCookie
>
();
match
setCookie
{
None
=>
panic!
(
"No setCookie."
),
Some
(
cookie
)
=>
if
cookie
.len
()
==
1
{
let
newCookie
=
reqwest
::
header
::
Cookie
(
cookie
.to_vec
());
let
newCookie2
=
newCookie
.clone
();
let
mut
newResp
=
client
.get
(
"http://localhost:8082/logout"
)
.header
(
newCookie
)
.send
()
.unwrap
();
newResp
=
client
.get
(
"http://localhost:8082"
)
.header
(
newCookie2
)
.send
()
.unwrap
();
let
mut
newContent
=
String
::
new
();
newResp
.read_to_string
(
&
mut
newContent
);
assert
!
(
!
content
.contains
(
"Error"
));
assert
!
(
newContent
.contains
(
"Benutzername:"
));
assert
!
(
newContent
.contains
(
"Passwort:"
));
assert
!
(
newContent
.contains
(
"Gruppencode / Teilnahmecode:"
));
assert
!
(
newContent
.contains
(
"<h1>Jugendwettbewerb Informatik</h1>"
));
}
else
{
panic!
(
"More than one setCookie."
);
},
};
})
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment