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
f0a4e036
Commit
f0a4e036
authored
Sep 07, 2018
by
Robert Czechowski
Browse files
Add test to check for server start and site response
parent
3c8fb5be
Changes
3
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
f0a4e036
all
:
RUSTFLAGS
=
-Awarnings
RUST_BACKTRACE
=
1 cargo run
--features
watch
test
:
RUSTFLAGS
=
-Awarnings
RUST_BACKTRACE
=
1 cargo
test
src/main.rs
View file @
f0a4e036
...
...
@@ -46,7 +46,7 @@ use std::fs;
use
std
::
path
::{
Path
,
PathBuf
};
use
structopt
::
StructOpt
;
#[derive(Serialize,
Deserialize,
Clone)]
#[derive(Serialize,
Deserialize,
Clone
,
Default
)]
pub
struct
Config
{
host
:
Option
<
String
>
,
port
:
Option
<
u16
>
,
...
...
@@ -64,23 +64,13 @@ fn read_config_from_file(file: &Path) -> Config {
println!
(
"Reading Config file '{}'"
,
file
.to_str
()
.unwrap_or
(
"<Encoding error>"
));
let
mut
config
=
if
let
Ok
(
mut
file
)
=
fs
::
File
::
open
(
file
)
{
let
mut
config
:
Config
=
if
let
Ok
(
mut
file
)
=
fs
::
File
::
open
(
file
)
{
let
mut
contents
=
String
::
new
();
file
.read_to_string
(
&
mut
contents
)
.unwrap
();
serde_json
::
from_str
(
&
contents
)
.unwrap
()
}
else
{
println!
(
"Configuration file '{}' not found."
,
file
.to_str
()
.unwrap_or
(
"<Encoding error>"
));
Config
{
host
:
None
,
port
:
None
,
self_url
:
None
,
oauth_url
:
None
,
oauth_client_id
:
None
,
oauth_client_secret
:
None
,
oauth_access_token_url
:
None
,
oauth_user_data_url
:
None
,
database_file
:
None
,
}
Default
::
default
()
};
if
config
.host
.is_none
()
{
config
.host
=
Some
(
"[::]"
.to_string
())}
...
...
@@ -196,3 +186,61 @@ fn main() {
println!
(
"Could not run server. Is the port already in use?"
);
}
#[cfg(test)]
mod
tests
{
use
super
::
*
;
#[test]
fn
start_server_and_check_request
()
{
use
std
::{
thread
,
time
};
let
mut
conn
=
Connection
::
open_in_memory
()
.unwrap
();
db_apply_migrations
::
test
(
&
mut
conn
);
// add contests / tasks here
use
std
::
sync
::{
Arc
,
Mutex
,
Condvar
};
let
pair
=
Arc
::
new
((
Mutex
::
new
(
false
),
Condvar
::
new
()));
let
pair_
=
pair
.clone
();
let
mut
config
=
read_config_from_file
(
Path
::
new
(
"thisfileshoudnotexist"
));
let
srvr
=
start_server
(
conn
,
config
);
thread
::
spawn
(
move
||
{
// wait for server to start:
thread
::
sleep
(
time
::
Duration
::
from_millis
(
100
));
use
std
::
io
::
Read
;
let
mut
resp
=
reqwest
::
get
(
"http://localhost:8080"
)
.unwrap
();
assert
!
(
resp
.status
()
.is_success
());
let
mut
content
=
String
::
new
();
resp
.read_to_string
(
&
mut
content
);
assert
!
(
content
.contains
(
"<h1>Jugendwettbewerb Informatik</h1>"
));
assert
!
(
!
content
.contains
(
"Error"
));
let
&
(
ref
lock
,
ref
cvar
)
=
&*
pair_
;
let
mut
should_exit
=
lock
.lock
()
.unwrap
();
*
should_exit
=
true
;
cvar
.notify_one
();
//fs::copy("foo.txt", "bar.txt").unwrap();
});
// Copied from docs
let
&
(
ref
lock
,
ref
cvar
)
=
&*
pair
;
let
mut
should_exit
=
lock
.lock
()
.unwrap
();
while
!*
should_exit
{
should_exit
=
cvar
.wait
(
should_exit
)
.unwrap
();
}
srvr
.unwrap
()
.close
()
.unwrap
();
assert
!
(
true
);
}
}
src/webfw_iron.rs
View file @
f0a4e036
...
...
@@ -773,7 +773,7 @@ fn cookie_warning(req: &mut Request) -> IronResult<Response> {
}
pub
fn
start_server
(
conn
:
Connection
,
config
:
::
Config
)
{
pub
fn
start_server
(
conn
:
Connection
,
config
:
::
Config
)
->
iron
::
error
::
HttpResult
<
iron
::
Listening
>
{
let
router
=
router!
(
greet
:
get
"/"
=>
greet_personal
,
contests
:
get
"/contest/"
=>
contests
,
...
...
@@ -824,8 +824,9 @@ pub fn start_server(conn: Connection, config: ::Config) {
let
socket_addr
=
format!
(
"{}:{}"
,
config
.host
.unwrap
(),
config
.port
.unwrap
());
let
_res
=
Iron
::
new
(
ch
)
.http
(
&
socket_addr
);
let
srvr
=
Iron
::
new
(
ch
)
.http
(
&
socket_addr
);
println!
(
"Listening on {}."
,
&
socket_addr
);
srvr
}
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