unit tests

This commit is contained in:
Andreas Schamberger
2011-10-31 11:55:14 +01:00
parent 4becca0208
commit e0049c906d
13 changed files with 569 additions and 26 deletions

View File

@ -80,6 +80,7 @@ class MultiPart extends PartHeader
/**
* Get string array with MIME headers for usage in HTTP header (with CURL).
* Only 'Content-Type' and 'Content-Description' headers are returned.
*
* @return arrray(string)
*/
@ -92,19 +93,7 @@ class MultiPart extends PartHeader
$headers = array();
foreach ($this->headers as $fieldName => $value) {
if (in_array($fieldName, $allowed)) {
$fieldValue = '';
if (is_array($value)) {
if (isset($value['@'])) {
$fieldValue .= $value['@'];
}
foreach ($value as $subName => $subValue) {
if ($subName != '@') {
$fieldValue .= '; ' . $subName . '="' . $subValue . '"';
}
}
} else {
$fieldValue .= $value;
}
$fieldValue = $this->generateHeaderFieldValue($value);
// for http only ISO-8859-1
$headers[] = $fieldName . ': '. iconv('utf-8', 'ISO-8859-1//TRANSLIT', $fieldValue);
}

View File

@ -92,23 +92,51 @@ abstract class PartHeader
);
$headers = '';
foreach ($this->headers as $fieldName => $value) {
$fieldValue = '';
if (is_array($value)) {
if (isset($value['@'])) {
$fieldValue .= $value['@'];
}
foreach ($value as $subName => $subValue) {
if ($subName != '@') {
$fieldValue .= '; ' . $subName . '=' . $subValue;
}
}
} else {
$fieldValue .= $value;
}
$fieldValue = $this->generateHeaderFieldValue($value);
// do not use proper encoding as Apache Axis does not understand this
// $headers .= iconv_mime_encode($field_name, $field_value, $preferences) . "\r\n";
$headers .= $fieldName . ': ' . $fieldValue . "\r\n";
}
return $headers;
}
/**
* Generates a header field value from the given value paramater.
*
* @param array(string=>string)|string $value
* @return string
*/
protected function generateHeaderFieldValue($value)
{
$fieldValue = '';
if (is_array($value)) {
if (isset($value['@'])) {
$fieldValue .= $value['@'];
}
foreach ($value as $subName => $subValue) {
if ($subName != '@') {
$fieldValue .= '; ' . $subName . '=' . $this->quoteValueString($subValue);
}
}
} else {
$fieldValue .= $value;
}
return $fieldValue;
}
/**
* Quote string with '"' if it contains one of the special characters:
* "(" / ")" / "<" / ">" / "@" / "," / ";" / ":" / "\" / <"> / "/" / "[" / "]" / "?" / "="
*
* @param string $string
* @return string
*/
private function quoteValueString($string)
{
if (preg_match('~[()<>@,;:\\"/\[\]?=]~', $string)) {
return '"' . $string . '"';
} else {
return $string;
}
}
}