<commit_before><?php

namespace Boomerang\Test;

use Boomerang\HttpRequest;

class HttpRequestTest extends \PHPUnit_Framework_TestCase {

	public function testAcceptOverride() {
		$req       = new HttpRequest('http://example.com/test.json');
		$headers   = $req->getHeaders();
		$headers_a = explode(",", $headers['Accept']);

		$this->assertEquals('application/json', $headers_a[0]);


		$req       = new HttpRequest('http://example.com/test.xml');
		$headers   = $req->getHeaders();
		$headers_a = explode(",", $headers['Accept']);

		$this->assertEquals('application/xml', $headers_a[0]);


		$req = new HttpRequest('http://example.com/test.xml');
		$req->setHeader('Accept', 'application/json');
		$headers   = $req->getHeaders();
		$headers_a = explode(",", $headers['Accept']);

		$this->assertEquals('application/json', $headers_a[0]);
	}

}<commit_msg>Adds a WebServer to the HttpRequestTest<commit_after><?php

namespace Boomerang\Test;

use Boomerang\Factories\HttpResponseFactory;
use Boomerang\HttpRequest;

class HttpRequestTest extends \PHPUnit_Framework_TestCase {

	/** @var \TestWebServer */
	public static $webServer;

	public static function setUpBeforeClass() {
		require_once(__DIR__ . '/../Server/TestWebServer.php');
		self::$webServer = new \TestWebServer();
	}

	public function testAcceptOverride() {
		$req       = new HttpRequest('http://example.com/test.json');
		$headers   = $req->getHeaders();
		$headers_a = explode(",", $headers['Accept']);

		$this->assertEquals('application/json', $headers_a[0]);


		$req       = new HttpRequest('http://example.com/test.xml');
		$headers   = $req->getHeaders();
		$headers_a = explode(",", $headers['Accept']);

		$this->assertEquals('application/xml', $headers_a[0]);


		$req = new HttpRequest('http://example.com/test.xml');
		$req->setHeader('Accept', 'application/json');
		$headers   = $req->getHeaders();
		$headers_a = explode(",", $headers['Accept']);

		$this->assertEquals('application/json', $headers_a[0]);
	}

	public function testMethods() {
		$methods = [
			HttpRequest::GET,
			HttpRequest::POST,
			HttpRequest::PUT,
			HttpRequest::PATCH,
			HttpRequest::DELETE,
			HttpRequest::TRACE,
			HttpRequest::OPTIONS,
		];

		$mockResponse = $this->getMock(\HttpResponse::class);

		foreach( $methods as $method ) {
			$mockFactory    = $this->getMock(HttpResponseFactory::class);
			$mockNewInstace = $mockFactory->method('newInstance');

			$mockNewInstace->will($this->returnCallback(function ( $body, $headers, HttpRequest $request ) use ( $mockResponse, $method ) {
				$data = json_decode($body, true);
				$this->assertSame($data['METHOD'], $method);
				$this->assertSame($data['INPUT'], 'This is the requests body');

				$this->assertSame($data['REQUEST_URI'], '/path?param1=1&param2=2');

				$this->assertSame([
					'param1' => '1',
					'param2' => '2',
				], $data['_GET']);

				return $mockResponse;
			}));

			$req = new HttpRequest(self::$webServer->getServerRoot() . '/path?param1=1&param2=2', $mockFactory);
			$req->setMethod($method);
			$req->setBody('This is the requests body');
			$resp = $req->makeRequest();
			$this->assertSame($mockResponse, $resp);
		}
	}

	public function testDeprecatedPostMethod() {
		$mockResponse   = $this->getMock(\HttpResponse::class);
		$mockFactory    = $this->getMock(HttpResponseFactory::class);
		$mockNewInstace = $mockFactory->method('newInstance');

		$mockNewInstace->will($this->returnCallback(function ( $body, $headers, HttpRequest $request ) use ( $mockResponse ) {
			$data = json_decode($body, true);
			$this->assertSame($data['METHOD'], HttpRequest::POST);
			$this->assertSame($data['INPUT'], '');
			$this->assertSame($data['_POST'], [
				'myPostKey' => 'myPostedValue',
			]);

			$this->assertSame($data['REQUEST_URI'], '/postTest');

			return $mockResponse;
		}));

		$req = new HttpRequest(self::$webServer->getServerRoot() . '/postTest', $mockFactory);
		$this->assertSame(HttpRequest::GET, $req->getMethod());
		$req->setPost('myPostKey', 'myPostedValue');
		$this->assertSame(HttpRequest::POST, $req->getMethod());

		$resp = $req->makeRequest();
		$this->assertSame($mockResponse, $resp);
	}

}
{'fraction_non_alphanumeric': 0.19534437245020397, 'fraction_numerical': 0.004319654427645789, 'mean_word_length': 4.979913916786226, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '3204362', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1329, 'n_words': 218}