From 82a90a81477a3b2a8244b8ebee2d3be8bfa0aff2 Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Fri, 4 Mar 2022 19:17:21 +0100 Subject: [PATCH 1/4] adding first test for the http server - #50 --- http/tests/main.ark | 37 ++++++++++++++++++++++++++++++++++--- http/tests/run | 27 +++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/http/tests/main.ark b/http/tests/main.ark index 579916c..e930e21 100644 --- a/http/tests/main.ark +++ b/http/tests/main.ark @@ -1,4 +1,35 @@ -# TODO add real tests -(let do_nothing nil) +(import "http.arkm") -(print "HTTP tests done") +(http:server:create) + +(http:server:get "/hi" (fun (matches body params) { + [ + 200 + (if (nil? data) + "hello world" + (+ "hello, " body)) + "text/plain" + ] +})) + +(http:server:get "/numbers/(\\d+)" (fun (matches body params) { + (let number (toNumber (@ matches 0))) + + [200 (toString (+ 12 number)) "text/plain"] +})) + +(http:server:get "/stop" (fun (matches body params) { + (http:server:stop) + [200 "stopped" "text/plain"] +})) + +(let handler (fun (matches body params) { + (let str_params (if (nil? str_params) "nil" (toString (http:params:toList params)))) + [200 (+ (toString matches) "-" body "-" str_params) "text/plain"] +})) + +(http:server:post "/post" handler) +(http:server:put "/put" handler) +(http:server:delete "/delete" handler) + +(http:server:listen "localhost" 8080) diff --git a/http/tests/run b/http/tests/run index 22f346e..814becd 100644 --- a/http/tests/run +++ b/http/tests/run @@ -3,5 +3,28 @@ ARKSCRIPT=$1 ARKLIB=$2 -# todo, see #50 -$ARKSCRIPT main.ark --lib $ARKLIB +($ARKSCRIPT main.ark --lib $ARKLIB &) + +if [[ $(curl -X GET http://localhost:8080/hi) != "hello world" ]]; then + exit 1 +fi + +if [[ $(curl -X GET --data 'john' http://localhost:8080/hi) != "hello, john" ]]; then + exit 1 +fi + +if [[ $(curl -X POST --data 'john' http://localhost:8080/post) != "nil-john-nil" ]]; then + exit 1 +fi + +if [[ $(curl -X PUT --data 'john' http://localhost:8080/put) != "nil-john-nil" ]]; then + exit 1 +fi + +if [[ $(curl -X DELETE --data 'john' http://localhost:8080/delete) != "nil-john-nil" ]]; then + exit 1 +fi + +if [[ $(curl -X GET http://localhost:8080/stop) != "stopped" ]]; then + exit 1 +fi \ No newline at end of file From f6080e6c9e77ab7dcee99b35f5e94f69bc35819b Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Fri, 4 Mar 2022 19:24:50 +0100 Subject: [PATCH 2/4] fixing the console test --- console/tests/main.ark | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/console/tests/main.ark b/console/tests/main.ark index f26fffa..6e310e3 100644 --- a/console/tests/main.ark +++ b/console/tests/main.ark @@ -2,21 +2,21 @@ # no tests to do as this is only visual and cosmetics -(console:color "grey") (print "text color: grey") (console:color "reset") -(console:color "red") (print "text color: red") (console:color "reset") -(console:color "green") (print "text color: green") (console:color "reset") -(console:color "yellow") (print "text color: yellow") (console:color "reset") -(console:color "blue") (print "text color: blue") (console:color "reset") -(console:color "magenta") (print "text color: magenta") (console:color "reset") -(console:color "cyan") (print "text color: cyan") (console:color "reset") -(console:color "white") (print "text color: white") (console:color "reset") -(console:color "bright_grey") (print "text color: bright_grey") (console:color "reset") -(console:color "bright_red") (print "text color: bright_red") (console:color "reset") -(console:color "bright_green") (print "text color: bright_green") (console:color "reset") -(console:color "bright_yellow") (print "text color: bright_yellow") (console:color "reset") -(console:color "bright_blue") (print "text color: bright_blue") (console:color "reset") -(console:color "bright_magenta") (print "text color: bright_magenta") (console:color "reset") -(console:color "bright_cyan") (print "text color: bright_cyan") (console:color "reset") -(console:color "bright_white") (print "text color: bright_white") (console:color "reset") +(console:color "grey") (print "text color: grey") +(console:color "red") (print "text color: red") +(console:color "green") (print "text color: green") +(console:color "yellow") (print "text color: yellow") +(console:color "blue") (print "text color: blue") +(console:color "magenta") (print "text color: magenta") +(console:color "cyan") (print "text color: cyan") +(console:color "white") (print "text color: white") +(console:color "on_grey") (print "bg color: grey") +(console:color "on_red") (print "bg color: red") +(console:color "on_green") (print "bg color: green") +(console:color "on_yellow") (print "bg color: yellow") +(console:color "on_blue") (print "bg color: blue") +(console:color "on_magenta") (print "bg color: magenta") +(console:color "on_cyan") (print "bg color: cyan") +(console:color "on_white") (print "bg color: white") (console:color "reset") \ No newline at end of file From 1b744ec2652d2c51000646acb579c008ece062c7 Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Fri, 4 Mar 2022 19:28:42 +0100 Subject: [PATCH 3/4] adding more http tests --- http/tests/main.ark | 2 +- http/tests/run | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/http/tests/main.ark b/http/tests/main.ark index e930e21..e3aeb4a 100644 --- a/http/tests/main.ark +++ b/http/tests/main.ark @@ -5,7 +5,7 @@ (http:server:get "/hi" (fun (matches body params) { [ 200 - (if (nil? data) + (if (nil? body) "hello world" (+ "hello, " body)) "text/plain" diff --git a/http/tests/run b/http/tests/run index 814becd..1c0c2ff 100644 --- a/http/tests/run +++ b/http/tests/run @@ -13,6 +13,10 @@ if [[ $(curl -X GET --data 'john' http://localhost:8080/hi) != "hello, john" ]]; exit 1 fi +if [[ $(curl -X GET --data 'john' http://localhost:8080/numbers/4) != "16" ]]; then + exit 1 +fi + if [[ $(curl -X POST --data 'john' http://localhost:8080/post) != "nil-john-nil" ]]; then exit 1 fi From 3a8ed81e1fd16e0c47139d79cba53d46dd24bacc Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Fri, 4 Mar 2022 19:34:41 +0100 Subject: [PATCH 4/4] working on the http script runner --- http/tests/main.ark | 2 +- http/tests/run | 35 ++++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/http/tests/main.ark b/http/tests/main.ark index e3aeb4a..1921e4e 100644 --- a/http/tests/main.ark +++ b/http/tests/main.ark @@ -5,7 +5,7 @@ (http:server:get "/hi" (fun (matches body params) { [ 200 - (if (nil? body) + (if (empty? body) "hello world" (+ "hello, " body)) "text/plain" diff --git a/http/tests/run b/http/tests/run index 1c0c2ff..2139bbe 100644 --- a/http/tests/run +++ b/http/tests/run @@ -5,30 +5,51 @@ ARKLIB=$2 ($ARKSCRIPT main.ark --lib $ARKLIB &) -if [[ $(curl -X GET http://localhost:8080/hi) != "hello world" ]]; then +echo "GET /hi" +res=$(curl -s -X GET http://localhost:8080/hi) +if [[ $res != "hello world" ]]; then + echo "Got $res" exit 1 fi -if [[ $(curl -X GET --data 'john' http://localhost:8080/hi) != "hello, john" ]]; then +echo "GET /hi john" +res=$(curl -s -X GET --data 'john' http://localhost:8080/hi) +if [[ $res != "hello, john" ]]; then + echo "Got $res" exit 1 fi -if [[ $(curl -X GET --data 'john' http://localhost:8080/numbers/4) != "16" ]]; then +echo "GET /numbers/4" +res=$(curl -s -X GET http://localhost:8080/numbers/4) +if [[ $res != "16" ]]; then + echo "Got $res" exit 1 fi -if [[ $(curl -X POST --data 'john' http://localhost:8080/post) != "nil-john-nil" ]]; then +echo "POST /post john" +res=$(curl -s -X POST --data 'john' http://localhost:8080/post) +if [[ $res != "nil-john-nil" ]]; then + echo "Got $res" exit 1 fi -if [[ $(curl -X PUT --data 'john' http://localhost:8080/put) != "nil-john-nil" ]]; then +echo "PUT /put john" +res=$(curl -s -X PUT --data 'john' http://localhost:8080/put) +if [[ $res != "nil-john-nil" ]]; then + echo "Got $res" exit 1 fi -if [[ $(curl -X DELETE --data 'john' http://localhost:8080/delete) != "nil-john-nil" ]]; then +echo "DELETE /delete john" +res=$(curl -s -X DELETE --data 'john' http://localhost:8080/delete) +if [[ $res != "nil-john-nil" ]]; then + echo "Got $res" exit 1 fi -if [[ $(curl -X GET http://localhost:8080/stop) != "stopped" ]]; then +echo "GET /stop" +res=$(curl -s -X GET http://localhost:8080/stop) +if [[ $res != "stopped" ]]; then + echo "Got $res" exit 1 fi \ No newline at end of file