OAuthUtilTest.php
4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
require_once dirname(__FILE__) . '/common.php';
/**
* Tests of OAuthUtil
*/
class OAuthUtilTest extends PHPUnit_Framework_TestCase {
public function testUrlencode() {
// Tests taken from
// http://wiki.oauth.net/TestCases ("Parameter Encoding")
$this->assertEquals('abcABC123', OAuthUtil::urlencode_rfc3986('abcABC123'));
$this->assertEquals('-._~', OAuthUtil::urlencode_rfc3986('-._~'));
$this->assertEquals('%25', OAuthUtil::urlencode_rfc3986('%'));
$this->assertEquals('%2B', OAuthUtil::urlencode_rfc3986('+'));
$this->assertEquals('%0A', OAuthUtil::urlencode_rfc3986("\n"));
$this->assertEquals('%20', OAuthUtil::urlencode_rfc3986(' '));
$this->assertEquals('%7F', OAuthUtil::urlencode_rfc3986("\x7F"));
//$this->assertEquals('%C2%80', OAuthUtil::urlencode_rfc3986("\x00\x80"));
//$this->assertEquals('%E3%80%81', OAuthUtil::urlencode_rfc3986("\x30\x01"));
// Last two checks disabled because of lack of UTF-8 support, or lack
// of knowledge from me (morten.fangel) on how to use it properly..
// A few tests to ensure code-coverage
$this->assertEquals( '', OAuthUtil::urlencode_rfc3986(NULL));
$this->assertEquals( '', OAuthUtil::urlencode_rfc3986(new stdClass()));
}
public function testUrldecode() {
// Tests taken from
// http://wiki.oauth.net/TestCases ("Parameter Encoding")
$this->assertEquals('abcABC123', OAuthUtil::urldecode_rfc3986('abcABC123'));
$this->assertEquals('-._~', OAuthUtil::urldecode_rfc3986('-._~'));
$this->assertEquals('%', OAuthUtil::urldecode_rfc3986('%25'));
$this->assertEquals('+', OAuthUtil::urldecode_rfc3986('%2B'));
$this->assertEquals("\n", OAuthUtil::urldecode_rfc3986('%0A'));
$this->assertEquals(' ', OAuthUtil::urldecode_rfc3986('%20'));
$this->assertEquals("\x7F", OAuthUtil::urldecode_rfc3986('%7F'));
//$this->assertEquals("\x00\x80", OAuthUtil::urldecode_rfc3986('%C2%80'));
//$this->assertEquals("\x30\x01", OAuthUtil::urldecode_rfc3986('%E3%80%81'));
// Last two checks disabled because of lack of UTF-8 support, or lack
// of knowledge from me (morten.fangel) on how to use it properly..
}
public function testParseParameter() {
// Tests taken from
// http://wiki.oauth.net/TestCases ("Normalize Request Parameters")
$this->assertEquals(
array('name'=>''),
OAuthUtil::parse_parameters('name')
);
$this->assertEquals(
array('a'=>'b'),
OAuthUtil::parse_parameters('a=b')
);
$this->assertEquals(
array('a'=>'b','c'=>'d'),
OAuthUtil::parse_parameters('a=b&c=d')
);
$this->assertEquals(
array('a'=>array('x!y','x y')),
OAuthUtil::parse_parameters('a=x!y&a=x+y')
);
$this->assertEquals(
array('x!y'=>'a', 'x' =>'a'),
OAuthUtil::parse_parameters('x!y=a&x=a')
);
}
public function testBuildHttpQuery() {
// Tests taken from
// http://wiki.oauth.net/TestCases ("Normalize Request Parameters")
$this->assertEquals(
'name=',
OAuthUtil::build_http_query(array('name'=>''))
);
$this->assertEquals(
'a=b',
OAuthUtil::build_http_query(array('a'=>'b'))
);
$this->assertEquals(
'a=b&c=d',
OAuthUtil::build_http_query(array('a'=>'b','c'=>'d'))
);
$this->assertEquals(
'a=x%20y&a=x%21y',
OAuthUtil::build_http_query(array('a'=>array('x!y','x y')))
);
$this->assertEquals(
'x=a&x%21y=a',
OAuthUtil::build_http_query(array('x!y'=>'a', 'x' =>'a'))
);
// Test taken from the Spec 9.1.1
$this->assertEquals(
'a=1&c=hi%20there&f=25&f=50&f=a&z=p&z=t',
OAuthUtil::build_http_query(array('a'=>'1', 'c' =>'hi there', 'f'=>array(25, 50, 'a'), 'z'=>array('p','t')))
);
}
public function testSplitHeader() {
$this->assertEquals(
array('oauth_foo'=>'bar','oauth_baz'=>'bla,rgh'),
OAuthUtil::split_header('OAuth realm="",oauth_foo=bar,oauth_baz="bla,rgh"')
);
$this->assertEquals(
array(),
OAuthUtil::split_header('OAuth realm="",foo=bar,baz="bla,rgh"')
);
$this->assertEquals(
array('foo'=>'bar', 'baz'=>'bla,rgh'),
OAuthUtil::split_header('OAuth realm="",foo=bar,baz="bla,rgh"', false)
);
$this->assertEquals(
array('oauth_foo' => 'hi there'),
OAuthUtil::split_header('OAuth realm="",oauth_foo=hi+there,foo=bar,baz="bla,rgh"')
);
}
public function testGetHeaders() {
if (function_exists('apache_request_headers')) {
$this->markTestSkipped('We assume the apache module is well tested. Since this module is present, no need testing our suplement');
}
$_SERVER['HTTP_HOST'] = 'foo';
$_SERVER['HTTP_X_WHATEVER'] = 'bar';
$this->assertEquals( array('Host'=>'foo', 'X-Whatever'=>'bar'), OAuthUtil::get_headers() );
}
}