Skip to content

Commit 8746c27

Browse files
committed
Use a lambda instead of std::bind1st
Add a test for parsing http headers inside urlstest.cpp Adapt the expected outcome of urlstest from 12 to 14 errors
1 parent 1fe2509 commit 8746c27

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/pcm-sensor-server.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2218,9 +2218,9 @@ class HTTPHeader {
22182218
while ( ss.good() ) {
22192219
std::getline( ss, s, listSeparatorChar );
22202220
// Remove leading whitespace
2221-
s.erase( s.begin(), std::find_if( s.begin(), s.end(), std::bind1st( std::not_equal_to<char>(), ' ' ) ) );
2221+
s.erase( s.begin(), std::find_if( s.begin(), s.end(), []( char c ){ return c != ' '; } ) );
22222222
// Remove trailing whitespace
2223-
s.erase( std::find_if( s.rbegin(), s.rend(), std::bind1st( std::not_equal_to<char>(), ' ') ).base(), s.end() );
2223+
s.erase( std::find_if( s.rbegin(), s.rend(), []( char c ){ return c != ' '; } ).base(), s.end() );
22242224
elementList.push_back( s );
22252225
}
22262226
return elementList;

tests/test.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ fi
210210

211211
echo Testing urltest
212212
./tests/urltest
213-
# We have 12 expected errors, anything else is a bug
214-
if [ "$?" != 12 ]; then
215-
echo "Error in urltest, 12 expected errors but found $?!"
213+
# We have 14 expected errors, anything else is a bug
214+
if [ "$?" != 14 ]; then
215+
echo "Error in urltest, 14 expected errors but found $?!"
216216
exit 1
217217
fi
218218

tests/urltest.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,19 @@ std::vector<std::string> urls{
2828
"http://ww\x00\x00\x00rstmark\x0a"
2929
};
3030

31+
std::vector<std::string> headers{
32+
"Content-Encoding text/html", // Invalid header no colon found
33+
" Content-Encoding : text/html ", // Valid header, should clean up the whitespace before and after
34+
" H o s t : my.host.com", // Valid, spaces in header name should be accepted and silenty removed
35+
"MyUnknownHeaderType : value", // Valid, MyUnknownHeaderType is considered a custom header type
36+
" Host : \"my.host.com" // Invalid, header value not properly quoted
37+
};
38+
3139
int main( int, char** ) {
32-
int errors = 0;
40+
// httpheader::debugPrint uses dbg(3), picking 5 for future changes
41+
debug::dyn_debug_level( 5 );
42+
43+
int errors = 0, errors2 = 0;
3344
for ( auto & s : urls ) {
3445
try {
3546
std::cout << s << "\n";
@@ -40,5 +51,12 @@ int main( int, char** ) {
4051
++errors;
4152
}
4253
}
43-
return errors;
54+
for ( auto & h : headers ) {
55+
std::cout << h << "\n";
56+
HTTPHeader hh = HTTPHeader::parse( h );
57+
hh.debugPrint();
58+
if ( hh.type() == HeaderType::Invalid )
59+
++errors2;
60+
}
61+
return errors+errors2;
4462
}

0 commit comments

Comments
 (0)