Skip to content

Commit d7d07c1

Browse files
committed
add unit test for X-Forwarded-For field in request
1 parent a72f2e9 commit d7d07c1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tempesta_fw/t/unit/test_http_parser.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,48 @@ TEST(http_parser, content_type_line_parser)
24212421
#undef TAIL
24222422
}
24232423

2424+
static
2425+
TfwStr get_next_str_val(TfwStr *str)
2426+
{
2427+
TfwStr v, *c, *end;
2428+
unsigned int nchunks = 0;
2429+
2430+
v = *str = tfw_str_next_str_val(str);
2431+
TFW_STR_FOR_EACH_CHUNK(c, &v, end) {
2432+
if (!(c->flags & TFW_STR_VALUE))
2433+
break;
2434+
nchunks++;
2435+
}
2436+
v.nchunks = nchunks;
2437+
2438+
return v;
2439+
}
2440+
2441+
TEST(http_parser, xff)
2442+
{
2443+
TfwStr xff, v;
2444+
2445+
const char *s_client = "203.0.113.195";
2446+
const char *s_proxy1 = "70.41.3.18";
2447+
const char *s_proxy2 = "150.172.238.178";
2448+
2449+
FOR_REQ("GET /foo HTTP/1.1\r\n"
2450+
"X-Forwarded-For: 203.0.113.195,70.41.3.18,150.172.238.178\r\n"
2451+
"\r\n");
2452+
{
2453+
xff = req->h_tbl->tbl[TFW_HTTP_HDR_X_FORWARDED_FOR];
2454+
2455+
v = get_next_str_val(&xff);
2456+
EXPECT_TRUE(tfw_str_eq_cstr(&v, s_client, strlen(s_client), 0));
2457+
2458+
v = get_next_str_val(&xff);
2459+
EXPECT_TRUE(tfw_str_eq_cstr(&v, s_proxy1, strlen(s_proxy1), 0));
2460+
2461+
v = get_next_str_val(&xff);
2462+
EXPECT_TRUE(tfw_str_eq_cstr(&v, s_proxy2, strlen(s_proxy2), 0));
2463+
}
2464+
}
2465+
24242466
TEST_SUITE(http_parser)
24252467
{
24262468
int r;
@@ -2458,6 +2500,7 @@ TEST_SUITE(http_parser)
24582500
TEST_RUN(http_parser, resp_hop_by_hop);
24592501
TEST_RUN(http_parser, fuzzer);
24602502
TEST_RUN(http_parser, content_type_line_parser);
2503+
TEST_RUN(http_parser, xff);
24612504

24622505
/*
24632506
* Testing for correctness of redirection mark parsing (in

0 commit comments

Comments
 (0)