Skip to content

Commit

Permalink
httpd - Add a cgi handler for handling post data and a system test to…
Browse files Browse the repository at this point in the history
… curl post to the endpoint to check that it is working

Add the first system test to tests/ along with a CMakeLists.txt
  • Loading branch information
chmorgan committed Oct 16, 2017
1 parent 6c78c2b commit c139897
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.9)

enable_testing()

include(ExternalProject)

set(DEFAULT_INSTALL_DIR ${CMAKE_BINARY_DIR}/install)
Expand All @@ -21,3 +23,15 @@ ExternalProject_Add(httpd
UPDATE_COMMAND ""
CMAKE_ARGS ${ARGS}
)

ExternalProject_Add(tests
SOURCE_DIR "${CMAKE_SOURCE_DIR}/tests"
DOWNLOAD_COMMAND ""
UPDATE_COMMAND ""
CMAKE_ARGS ${ARGS}
)

add_test(NAME test_post
COMMAND ${DEFAULT_INSTALL_DIR}/bin/test_post.sh
WORKING_DIRECTORY ${DEFAULT_INSTALL_DIR}/bin
)
23 changes: 23 additions & 0 deletions httpd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ void myEchoWebsocketConnect(Websock *ws) {
ws->recvCb = myEchoWebsocketRecv;
}

CgiStatus ICACHE_FLASH_ATTR cgiUploadTest(HttpdConnData *connData) {

printf("connData->post->len %d\n", connData->post->len);
printf("connData->post->received %d\n", connData->post->received);

if(connData->post->len == connData->post->received)
{
httpd_printf("Upload done. Sending response.\n");
httpdStartResponse(connData, 200);
httpdHeader(connData, "Content-Type", "text/plain");
httpdEndHeaders(connData);
httpdSend(connData, "Data received", -1);
httpdSend(connData, "\n", -1);

return HTTPD_CGI_DONE;
} else
{
return HTTPD_CGI_MORE;
}
}

int main()
{
HttpdBuiltInUrl builtInUrls[]={
Expand All @@ -56,6 +77,8 @@ int main()
{"/websocket/ws.cgi", cgiWebsocket, myWebsocketConnect},
{"/websocket/echo.cgi", cgiWebsocket, myEchoWebsocketConnect},

{"/upload", cgiUploadTest, NULL},

{"*", cgiEspFsHook, NULL},
{NULL, NULL, NULL}
};
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.9)

install(PROGRAMS test_post.sh DESTINATION bin)
20 changes: 20 additions & 0 deletions tests/test_post.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# test that we can perform a post of data to a cgi handler

./httpd&
httpd_pid=$!

# generate some random data to upload
# NOTE: This test was previously failing at a size of 300000 but would work at 100000,
# so we are using 300000 here
dd bs=1 count=300000 if=/dev/urandom of=testfile.bin

timeout 5s curl -k -X POST https://localhost:9000/upload --data-binary @testfile.bin
if [ $? -ne 0 ]
then
exit_code=1
fi

# shut down httpd
kill -KILL ${httpd_pid}

exit ${exit_code}

0 comments on commit c139897

Please sign in to comment.