aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_http_reasons.c
blob: a4dab63a570f7312d9346edadc9fd10049e1a057 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
  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 <stdio.h>
#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;
  (void)argc; (void)argv; /* Unused. Silent compiler warning. */

  errcount += test_absent_codes();
  errcount += test_1xx();
  errcount += test_2xx();
  errcount += test_3xx();
  errcount += test_4xx();
  errcount += test_5xx();
  return errcount == 0 ? 0 : 1;
}