Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/aphront/headerparser/__tests__/AphrontHTTPHeaderParserTestCase.php
12241 views
1
<?php
2
3
final class AphrontHTTPHeaderParserTestCase extends PhutilTestCase {
4
5
public function testHeaderParser() {
6
$cases = array(
7
array(
8
'Key: x; y; z',
9
'Key',
10
'x; y; z',
11
array(
12
array('x', null),
13
array('y', null),
14
array('z', null),
15
),
16
),
17
array(
18
'Content-Disposition: form-data; name="label"',
19
'Content-Disposition',
20
'form-data; name="label"',
21
array(
22
array('form-data', null),
23
array('name', 'label'),
24
),
25
),
26
array(
27
'Content-Type: multipart/form-data; charset=utf-8',
28
'Content-Type',
29
'multipart/form-data; charset=utf-8',
30
array(
31
array('multipart/form-data', null),
32
array('charset', 'utf-8'),
33
),
34
),
35
array(
36
'Content-Type: application/octet-stream; charset="ut',
37
'Content-Type',
38
'application/octet-stream; charset="ut',
39
false,
40
),
41
array(
42
'Content-Type: multipart/form-data; boundary=ABCDEFG',
43
'Content-Type',
44
'multipart/form-data; boundary=ABCDEFG',
45
array(
46
array('multipart/form-data', null),
47
array('boundary', 'ABCDEFG'),
48
),
49
),
50
array(
51
'Content-Type: multipart/form-data; boundary="ABCDEFG"',
52
'Content-Type',
53
'multipart/form-data; boundary="ABCDEFG"',
54
array(
55
array('multipart/form-data', null),
56
array('boundary', 'ABCDEFG'),
57
),
58
),
59
);
60
61
foreach ($cases as $case) {
62
$input = $case[0];
63
$expect_name = $case[1];
64
$expect_content = $case[2];
65
66
$parser = id(new AphrontHTTPHeaderParser())
67
->parseRawHeader($input);
68
69
$actual_name = $parser->getHeaderName();
70
$actual_content = $parser->getHeaderContent();
71
72
$this->assertEqual(
73
$expect_name,
74
$actual_name,
75
pht('Header name for: %s', $input));
76
77
$this->assertEqual(
78
$expect_content,
79
$actual_content,
80
pht('Header content for: %s', $input));
81
82
if (isset($case[3])) {
83
$expect_pairs = $case[3];
84
85
$caught = null;
86
try {
87
$actual_pairs = $parser->getHeaderContentAsPairs();
88
} catch (Exception $ex) {
89
$caught = $ex;
90
}
91
92
if ($expect_pairs === false) {
93
$this->assertEqual(
94
true,
95
($caught instanceof Exception),
96
pht('Expect exception for header pairs of: %s', $input));
97
} else {
98
$this->assertEqual(
99
$expect_pairs,
100
$actual_pairs,
101
pht('Header pairs for: %s', $input));
102
}
103
}
104
}
105
}
106
107
108
}
109
110