From ccfe7bafafbca43b94cf3d1c75144f51d57a5386 Mon Sep 17 00:00:00 2001 From: "Evgeny Grin (Karlson2k)" Date: Wed, 26 Apr 2017 15:19:20 +0300 Subject: reason_phrase.c: Fixed shift in HTTP reasons strings. Added test for HTTP reasons strings. --- src/microhttpd/Makefile.am | 5 ++ src/microhttpd/reason_phrase.c | 1 - src/microhttpd/test_http_reasons.c | 131 +++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/microhttpd/test_http_reasons.c (limited to 'src') diff --git a/src/microhttpd/Makefile.am b/src/microhttpd/Makefile.am index 7573fa7a..8cffd29d 100644 --- a/src/microhttpd/Makefile.am +++ b/src/microhttpd/Makefile.am @@ -142,6 +142,7 @@ endif check_PROGRAMS = \ test_str_compare \ test_str_to_value \ + test_http_reasons \ test_shutdown_select \ test_shutdown_poll \ test_daemon @@ -249,3 +250,7 @@ test_str_compare_SOURCES = \ test_str_to_value_SOURCES = \ test_str.c test_helpers.h mhd_str.c + +test_http_reasons_SOURCES = \ + test_http_reasons.c \ + reason_phrase.c microhttpd.h mhd_str.c mhd_str.h diff --git a/src/microhttpd/reason_phrase.c b/src/microhttpd/reason_phrase.c index d2cd152f..a6ffc75f 100644 --- a/src/microhttpd/reason_phrase.c +++ b/src/microhttpd/reason_phrase.c @@ -74,7 +74,6 @@ static const char *const two_hundred[] = { static const char *const three_hundred[] = { "Multiple Choices", "Moved Permanently", - "Moved Temporarily", "Found", "See Other", "Not Modified", diff --git a/src/microhttpd/test_http_reasons.c b/src/microhttpd/test_http_reasons.c new file mode 100644 index 00000000..1a78e2c6 --- /dev/null +++ b/src/microhttpd/test_http_reasons.c @@ -0,0 +1,131 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 2017 Karlson2k (Evgeny Grin) + + This test tool is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2, or + (at your option) any later version. + + This test tool is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/** + * @file microhttpd/test_http_reasons.c + * @brief Unit tests for MHD_get_reason_phrase_for() function + * @author Karlson2k (Evgeny Grin) + */ + +#include "mhd_options.h" +#include +#include "microhttpd.h" +#include "mhd_str.h" + +static int expect_result(int code, const char* expected) +{ + const char* const reason = MHD_get_reason_phrase_for(code); + if (MHD_str_equal_caseless_(reason, expected)) + return 0; + fprintf(stderr, "Incorrect reason returned for code %d:\n Returned: \"%s\" \tExpected: \"%s\"\n", + code, reason, expected); + return 1; +} + +static int expect_absent(int code) +{ + return expect_result(code, "unknown"); +} + +static int test_absent_codes(void) +{ + int errcount = 0; + errcount += expect_absent(0); + errcount += expect_absent(1); + errcount += expect_absent(50); + errcount += expect_absent(99); + errcount += expect_absent(600); + errcount += expect_absent(601); + errcount += expect_absent(900); + errcount += expect_absent(10000); + return errcount; +} + +static int test_1xx(void) +{ + int errcount = 0; + errcount += expect_result(MHD_HTTP_CONTINUE, "continue"); + errcount += expect_result(MHD_HTTP_PROCESSING, "processing"); + errcount += expect_absent(110); + errcount += expect_absent(190); + return errcount; +} + +static int test_2xx(void) +{ + int errcount = 0; + errcount += expect_result(MHD_HTTP_OK, "ok"); + errcount += expect_result(MHD_HTTP_ALREADY_REPORTED, "already reported"); + errcount += expect_absent(217); + errcount += expect_result(MHD_HTTP_IM_USED, "im used"); + errcount += expect_absent(230); + errcount += expect_absent(295); + return errcount; +} + +static int test_3xx(void) +{ + int errcount = 0; + errcount += expect_result(MHD_HTTP_MULTIPLE_CHOICES, "multiple choices"); + errcount += expect_result(MHD_HTTP_SEE_OTHER, "see other"); + errcount += expect_result(MHD_HTTP_PERMANENT_REDIRECT, "permanent redirect"); + errcount += expect_absent(311); + errcount += expect_absent(399); + return errcount; +} + +static int test_4xx(void) +{ + int errcount = 0; + errcount += expect_result(MHD_HTTP_BAD_REQUEST, "bad request"); + errcount += expect_result(MHD_HTTP_NOT_FOUND, "not found"); + errcount += expect_result(MHD_HTTP_URI_TOO_LONG, "uri too long"); + errcount += expect_result(MHD_HTTP_EXPECTATION_FAILED, "expectation failed"); + errcount += expect_result(MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, "request header fields too large"); + errcount += expect_absent(441); + errcount += expect_result(MHD_HTTP_NO_RESPONSE, "no response"); + errcount += expect_result(MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS, "unavailable for legal reasons"); + errcount += expect_absent(470); + errcount += expect_absent(493); + return errcount; +} + +static int test_5xx(void) +{ + int errcount = 0; + errcount += expect_result(MHD_HTTP_INTERNAL_SERVER_ERROR, "internal server error"); + errcount += expect_result(MHD_HTTP_BAD_GATEWAY, "bad gateway"); + errcount += expect_result(MHD_HTTP_HTTP_VERSION_NOT_SUPPORTED, "http version not supported"); + errcount += expect_result(MHD_HTTP_NETWORK_AUTHENTICATION_REQUIRED, "network authentication required"); + errcount += expect_absent(520); + errcount += expect_absent(597); + return errcount; +} + +int main(int argc, char * argv[]) +{ + int errcount = 0; + errcount += test_absent_codes(); + errcount += test_1xx(); + errcount += test_2xx(); + errcount += test_3xx(); + errcount += test_4xx(); + errcount += test_5xx(); + return errcount == 0 ? 0 : 1; +} -- cgit v1.2.3