<commit_before><?php

use OAuth\OAuth\Request;
use OAuth\Consumer;
use OAuth\Token;
use OAuth\SignatureMethod\SignatureMethod_PLAINTEXT;
use OAuth\SignatureMethod\SignatureMethod_HMAC_SHA1;

/*
 * Tests of OAuth\Request
 *
 * The tests works by using OAuthTestUtils::build_request
 * to populare $_SERVER, $_GET & $_POST.
 *
 * Most of the base string and signature tests
 * are either very simple or based upon
 * http://wiki.oauth.net/TestCases
 */

require_once dirname(__FILE__) . '/common.php';

class OAuthRequestTest extends PHPUnit_Framework_TestCase {	
	public function testCanGetSingleParameter() {
		// Yes, a awesomely boring test.. But if this doesn't work, the other tests is unreliable
		$request = new OAuth\Request('', '', array('test'=>'foo'));
		$this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to read back parameter');

		$request = new OAuth\Request('', '', array('test'=>array('foo', 'bar')));
		$this->assertEquals( array('foo', 'bar'), $request->get_parameter('test'), 'Failed to read back parameter');

	
		$request = new OAuth\Request('', '', array('test'=>'foo', 'bar'=>'baz'));
		$this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to read back parameter');
		$this->assertEquals( 'baz', $request->get_parameter('bar'), 'Failed to read back parameter');
	}
	
	public function testGetAllParameters() {
		// Yes, a awesomely boring test.. But if this doesn't work, the other tests is unreliable
		$request = new OAuth\Request('', '', array('test'=>'foo'));
		$this->assertEquals( array('test'=>'foo'), $request->get_parameters(), 'Failed to read back parameters');

		$request = new OAuth\Request('', '', array('test'=>'foo', 'bar'=>'baz'));
		$this->assertEquals( array('test'=>'foo', 'bar'=>'baz'), $request->get_parameters(), 'Failed to read back parameters');

		$request = new OAuth\Request('', '', array('test'=>array('foo', 'bar')));
		$this->assertEquals( array('test'=>array('foo', 'bar')), $request->get_parameters(), 'Failed to read back parameters');
	}
	
	public function testSetParameters() {
		$request = new OAuth\Request('', '');
		$this->assertEquals( NULL, $request->get_parameter('test'), 'Failed to assert that non-existing parameter is NULL');

		$request->set_parameter('test', 'foo');
		$this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to set single-entry parameter');

		$request->set_parameter('test', 'bar');
		$this->assertEquals( array('foo', 'bar'), $request->get_parameter('test'), 'Failed to set single-entry parameter');

		$request->set_parameter('test', 'bar', false);
		$this->assertEquals( 'bar', $request->get_parameter('test'), 'Failed to set single-entry parameter');
	}
	
	public function testUnsetParameter() {
		$request = new OAuth\Request('', '');
		$this->assertEquals( NULL, $request->get_parameter('test'));

		$request->set_parameter('test', 'foo');
		$this->assertEquals( 'foo', $request->get_parameter('test'));

		$request->unset_parameter('test');
		$this->assertEquals( NULL, $request->get_parameter('test'), 'Failed to unset parameter');
	}
	
	public function testCreateRequestFromConsumerAndToken() {
		$cons = new Consumer('key', 'kd94hf93k423kf44');
		$token = new Token('token', '<PASSWORD>');
		
		$request = OAuth\Request::from_consumer_and_token($cons, $token, 'POST', 'http://example.com');
		$this->assertEquals('POST', $request->get_normalized_http_method());
		$this->assertEquals('http://example.com', $request->get_normalized_http_url());
		$this->assertEquals('1.0', $request->get_parameter('oauth_version'));
		$this->assertEquals($cons->key, $request->get_parameter('oauth_consumer_key'));
		$this->assertEquals($token->key, $request->get_parameter('oauth_token'));
		$this->assertEquals(time(), $request->get_parameter('oauth_timestamp'));
		$this->assertRegExp('/[0-9a-f]{32}/', $request->get_parameter('oauth_nonce'));
		// We don't know what the nonce will be, except it'll be md5 and hence 32 hexa digits
		
		$request = OAuth\Request::from_consumer_and_token($cons, $token, 'POST', 'http://example.com', array('oauth_nonce'=>'foo'));
		$this->assertEquals('foo', $request->get_parameter('oauth_nonce'));
		
		$request = OAuth\Request::from_consumer_and_token($cons, NULL, 'POST', 'http://example.com', array('oauth_nonce'=>'foo'));
		$this->assertNull($request->get_parameter('oauth_token'));
		
		// Test that parameters given in the $http_url instead of in the $parameters-parameter
		// will still be picked up
		$request = OAuth\Request::from_consumer_and_token($cons, $token, 'POST', 'http://example.com/?foo=bar');
		$this->assertEquals('http://example.com/', $request->get_normalized_http_url());
		$this->assertEquals('bar', $request->get_parameter('foo'));
	}
	
	public function testBuildRequestFromPost() {
		OAuthTestUtils::build_request('POST', 'http://testbed/test', 'foo=bar&baz=blargh');
		$this->assertEquals(array('foo'=>'bar','baz'=>'blargh'), OAuth\Request::from_request()->get_parameters(), 'Failed to parse POST parameters');
	}
	
	public function testBuildRequestFromGet() {
		OAuthTestUtils::build_request('GET', 'http://testbed/test?foo=bar&baz=blargh');		
		$this->assertEquals(array('foo'=>'bar','baz'=>'blargh'), OAuth\Request::from_request()->get_parameters(), 'Failed to parse GET parameters');
	}

	public function testBuildRequestFromHeader() {
		$test_header = 'OAuth realm="",oauth_foo=bar,oauth_baz="bla,rgh"';
		OAuthTestUtils::build_request('POST', 'http://testbed/test', '', $test_header);
		$this->assertEquals(array('oauth_foo'=>'bar','oauth_baz'=>'bla,rgh'), OAuth\Request::from_request()->get_parameters(), 'Failed to split auth-header correctly');<commit_msg>Remove timestamp assertion. It's minutely off in tests that report code coverage.<commit_after><?php

use OAuth\OAuth\Request;
use OAuth\Consumer;
use OAuth\Token;
use OAuth\SignatureMethod\SignatureMethod_PLAINTEXT;
use OAuth\SignatureMethod\SignatureMethod_HMAC_SHA1;

/*
 * Tests of OAuth\Request
 *
 * The tests works by using OAuthTestUtils::build_request
 * to populare $_SERVER, $_GET & $_POST.
 *
 * Most of the base string and signature tests
 * are either very simple or based upon
 * http://wiki.oauth.net/TestCases
 */

require_once dirname(__FILE__) . '/common.php';

class OAuthRequestTest extends PHPUnit_Framework_TestCase {	
	public function testCanGetSingleParameter() {
		// Yes, a awesomely boring test.. But if this doesn't work, the other tests is unreliable
		$request = new OAuth\Request('', '', array('test'=>'foo'));
		$this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to read back parameter');

		$request = new OAuth\Request('', '', array('test'=>array('foo', 'bar')));
		$this->assertEquals( array('foo', 'bar'), $request->get_parameter('test'), 'Failed to read back parameter');

	
		$request = new OAuth\Request('', '', array('test'=>'foo', 'bar'=>'baz'));
		$this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to read back parameter');
		$this->assertEquals( 'baz', $request->get_parameter('bar'), 'Failed to read back parameter');
	}
	
	public function testGetAllParameters() {
		// Yes, a awesomely boring test.. But if this doesn't work, the other tests is unreliable
		$request = new OAuth\Request('', '', array('test'=>'foo'));
		$this->assertEquals( array('test'=>'foo'), $request->get_parameters(), 'Failed to read back parameters');

		$request = new OAuth\Request('', '', array('test'=>'foo', 'bar'=>'baz'));
		$this->assertEquals( array('test'=>'foo', 'bar'=>'baz'), $request->get_parameters(), 'Failed to read back parameters');

		$request = new OAuth\Request('', '', array('test'=>array('foo', 'bar')));
		$this->assertEquals( array('test'=>array('foo', 'bar')), $request->get_parameters(), 'Failed to read back parameters');
	}
	
	public function testSetParameters() {
		$request = new OAuth\Request('', '');
		$this->assertEquals( NULL, $request->get_parameter('test'), 'Failed to assert that non-existing parameter is NULL');

		$request->set_parameter('test', 'foo');
		$this->assertEquals( 'foo', $request->get_parameter('test'), 'Failed to set single-entry parameter');

		$request->set_parameter('test', 'bar');
		$this->assertEquals( array('foo', 'bar'), $request->get_parameter('test'), 'Failed to set single-entry parameter');

		$request->set_parameter('test', 'bar', false);
		$this->assertEquals( 'bar', $request->get_parameter('test'), 'Failed to set single-entry parameter');
	}
	
	public function testUnsetParameter() {
		$request = new OAuth\Request('', '');
		$this->assertEquals( NULL, $request->get_parameter('test'));

		$request->set_parameter('test', 'foo');
		$this->assertEquals( 'foo', $request->get_parameter('test'));

		$request->unset_parameter('test');
		$this->assertEquals( NULL, $request->get_parameter('test'), 'Failed to unset parameter');
	}
	
	public function testCreateRequestFromConsumerAndToken() {
		$cons = new Consumer('key', '<KEY>');
		$token = new Token('token', '<PASSWORD>');
		
		$request = OAuth\Request::from_consumer_and_token($cons, $token, 'POST', 'http://example.com');
		$this->assertEquals('POST', $request->get_normalized_http_method());
		$this->assertEquals('http://example.com', $request->get_normalized_http_url());
		$this->assertEquals('1.0', $request->get_parameter('oauth_version'));
		$this->assertEquals($cons->key, $request->get_parameter('oauth_consumer_key'));
		$this->assertEquals($token->key, $request->get_parameter('oauth_token'));
		$this->assertRegExp('/[0-9a-f]{32}/', $request->get_parameter('oauth_nonce'));
		// We don't know what the nonce will be, except it'll be md5 and hence 32 hexa digits
		
		$request = OAuth\Request::from_consumer_and_token($cons, $token, 'POST', 'http://example.com', array('oauth_nonce'=>'foo'));
		$this->assertEquals('foo', $request->get_parameter('oauth_nonce'));
		
		$request = OAuth\Request::from_consumer_and_token($cons, NULL, 'POST', 'http://example.com', array('oauth_nonce'=>'foo'));
		$this->assertNull($request->get_parameter('oauth_token'));
		
		// Test that parameters given in the $http_url instead of in the $parameters-parameter
		// will still be picked up
		$request = OAuth\Request::from_consumer_and_token($cons, $token, 'POST', 'http://example.com/?foo=bar');
		$this->assertEquals('http://example.com/', $request->get_normalized_http_url());
		$this->assertEquals('bar', $request->get_parameter('foo'));
	}
	
	public function testBuildRequestFromPost() {
		OAuthTestUtils::build_request('POST', 'http://testbed/test', 'foo=bar&baz=blargh');
		$this->assertEquals(array('foo'=>'bar','baz'=>'blargh'), OAuth\Request::from_request()->get_parameters(), 'Failed to parse POST parameters');
	}
	
	public function testBuildRequestFromGet() {
		OAuthTestUtils::build_request('GET', 'http://testbed/test?foo=bar&baz=blargh');		
		$this->assertEquals(array('foo'=>'bar','baz'=>'blargh'), OAuth\Request::from_request()->get_parameters(), 'Failed to parse GET parameters');
	}

	public function testBuildRequestFromHeader() {
		$test_header = 'OAuth realm="",oauth_foo=bar,oauth_baz="bla,rgh"';
		OAuthTestUtils::build_request('POST', 'http://testbed/test', '', $test_header);
		$this->assertEquals(array('oauth_foo'=>'bar','oauth_baz'=>'bla,rgh'), OAuth\Request::from_request()->get_parameters(), 'Failed to split auth-header correctly');
{'fraction_non_alphanumeric': 0.19555987970988856, 'fraction_numerical': 0.0025650097293472493, 'mean_word_length': 6.419291338582677, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 170, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5186937', 'n_tokens_mistral': 3640, 'n_tokens_neox': 3405, 'n_words': 750}