test1626: Curl_copy_header_value unit test

Closes #20903
This commit is contained in:
Daniel Stenberg 2026-03-12 10:29:30 +01:00
parent 7a4fa90048
commit 9148862c26
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
5 changed files with 159 additions and 2 deletions

View file

@ -214,6 +214,8 @@ static CURLcode copy_custom_value(const char *header, char **valp)
*
* This function MUST be used after the header has already been confirmed to
* lead with "word:".
*
* @unittest: 1626
*/
char *Curl_copy_header_value(const char *header)
{

View file

@ -214,7 +214,7 @@ test1590 test1591 test1592 test1593 test1594 test1595 test1596 test1597 \
test1598 test1599 test1600 test1601 test1602 test1603 test1604 test1605 \
test1606 test1607 test1608 test1609 test1610 test1611 test1612 test1613 \
test1614 test1615 test1616 test1617 \
test1620 test1621 test1622 test1623 test1624 test1625 \
test1620 test1621 test1622 test1623 test1624 test1625 test1626 \
\
test1630 test1631 test1632 test1633 test1634 test1635 test1636 test1637 \
\

25
tests/data/test1626 Normal file
View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="US-ASCII"?>
<testcase>
<info>
<keywords>
unittest
Curl_copy_header_value
</keywords>
</info>
<client>
<features>
unittest
http
</features>
<name>
Curl_copy_header_value unit test
</name>
</client>
<verify>
<stdout mode="text">
28 invokes
</stdout>
</verify>
</testcase>

View file

@ -37,7 +37,7 @@ TESTS_C = \
unit1600.c unit1601.c unit1602.c unit1603.c unit1605.c unit1606.c \
unit1607.c unit1608.c unit1609.c unit1610.c unit1611.c unit1612.c unit1614.c \
unit1615.c unit1616.c unit1620.c \
unit1625.c \
unit1625.c unit1626.c \
unit1636.c \
unit1650.c unit1651.c unit1652.c unit1653.c unit1654.c unit1655.c unit1656.c \
unit1657.c unit1658.c unit1660.c unit1661.c unit1663.c unit1664.c \

130
tests/unit/unit1626.c Normal file
View file

@ -0,0 +1,130 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "unitcheck.h"
#ifndef CURL_DISABLE_HTTP
#include "urldata.h"
#include "url.h"
struct check1626 {
const char *in; /* send this in */
const char *out; /* expect this out */
};
static CURLcode test_unit1626(const char *arg)
{
size_t i;
static const struct check1626 list[] = {
/* basic */
{ "Header: value", "value" },
/* no space */
{ "Header:value", "value" },
/* multiple spaces */
{ "Header: value", "value" },
/* tabs */
{ "Header: \tvalue", "value" },
/* trailing space */
{ "Header: value ", "value" },
/* leading and trailing spaces */
{ "Header: value ", "value" },
/* nothing after colon */
{ "Header:", "" },
/* spaces-only after colon */
{ "Header: ", "" },
/* spaces and tabs after colon */
{ "Header: \t ", "" },
/* spaces in the value */
{ "Header: one two", "one two" },
/* multiple spaces in the value */
{ "Header: one two a b c ", "one two a b c" },
/* realistic */
{ "Header: text/html", "text/html" },
/* ending with CR */
{ "Header: value\r", "value" },
/* ending with LF */
{ "Header: value\n", "value" },
/* quoted value */
{ "Header: \"value\"\n", "\"value\"" },
/* quoted value with trailing space */
{ "Header: \"value\" ", "\"value\"" },
/* leading whitespace before header name */
{ " Header: value", "value" },
/* tab before colon */
{ "Header\t: value", "value" },
/* mixed whitespace after colon */
{ "Header:\t value", "value" },
/* value containing colon */
{ "Header: foo:bar", "foo:bar" },
/* multiple colons */
{ "Header: foo:bar:baz", "foo:bar:baz" },
/* tab-only value */
{ "Header:\t\t", "" },
/* CRLF ending */
{ "Header: value\r\n", "value" },
/* value with internal tabs */
{ "Header: foo\tbar", "foo\tbar" },
/* value with leading tab trimmed */
{ "Header:\tfoo", "foo" },
/* colon with spaces before it */
{ "Header : value", "value" },
/* multiple spaces before colon */
{ "Header : value", "value" },
/* spaces around colon */
{ "Header : value", "value" },
};
(void)arg;
for(i = 0; i < CURL_ARRAYSIZE(list); i++) {
bool ok;
char *get = Curl_copy_header_value(list[i].in);
ok = get && !strcmp(list[i].out, get);
if(!ok) {
curl_mprintf("Input: %s\n"
"Got: %s\n"
"Expected: %s\n",
list[i].in, get, list[i].out);
}
curlx_free(get);
if(!ok)
break;
}
curl_mprintf("%zu invokes\n", i);
if(i != CURL_ARRAYSIZE(list))
return CURLE_FAILED_INIT;
return CURLE_OK;
}
#else
/* for HTTP-disabled builds */
static CURLcode test_unit1626(const char *arg)
{
(void)arg;
return CURLE_OK;
}
#endif