/** * Confirms that responses are passed along to the request object for * validation. */ public function testResponsePrototypeGuarantee() { $proto = array('response' => array('foo' => null, 'bar' => array('knock-knock' => null), 'baz' => null)); $jsonResponse = <<<EOF { "response": { "foo": ["", 1, "b"], "bar": { "knock-knock": "who's there" }, "baz": "927381.29" } } EOF; $stubProperties = array('responseFormat' => Base::RESPONSE_FORMAT_JSON); $instance = $this->_getStub($stubProperties, array('executeCurlHandle' => $this->returnValue($jsonResponse))); $request = new Request('http://127.0.0.1'); $request->setResponseValidator($proto); $request->setResponseValidationMethod(Request::VALIDATE_PROTOTYPE); $instance->callGetResponse($request); /* Adding an additional element to the response that is not found in the prototype doesn't throw an exception. */ $jsonResponse = <<<EOF { "response": { "foo": ["", 1, "b"], "bar": { "knock-knock": "who's there" }, "baz": "927381.29", "bort": { "eenie": "meenie", "minie": "moe" } } } EOF; $instance = $this->_getStub($stubProperties, array('executeCurlHandle' => $this->returnValue($jsonResponse))); $instance->callGetResponse($request); // But adding an extra requirement to the prototype does $proto['baz'] = null; $request->setResponseValidator($proto); $this->assertThrows('RuntimeException', array($instance, 'callGetResponse'), array($request)); }
/** * Confirms that the enforcing of validation on the response works as * expected. */ public function testResponseValidation() { $request = new Request('127.0.0.1'); $response = array('foo' => 'a', 'bar' => 'b', 'baz' => 'c'); /* Note that because a raw response with a non-zero length must be passed to GenericAPI\Request::validateResponse(), but it does not need to have anything to do with the parsed response, I am passing a placeholder argument in these calls. */ $request->setResponseValidator(array('foo', 'bar')); // The response can have more keys than the validator checks for $request->validateResponse('a', $response); $request->setResponseValidator(array('foo', 'bar', 'baz', 'asdf')); $this->assertThrows(__NAMESPACE__ . '\\ResponseValidationException', array($request, 'validateResponse'), array('a', $response)); $response['asdf'] = 'd'; $request->validateResponse('a', $response); $proto = array('response' => array('foo' => null, 'bar' => array('knock-knock' => null), 'baz' => null)); $request->setResponseValidator($proto); $request->setResponseValidationMethod(Request::VALIDATE_PROTOTYPE); $response = array('response' => array('foo' => array('', 1, 'b'), 'bar' => array('knock-knock' => "who's there"), 'baz' => '927381.29')); $request->validateResponse('a', $response); /* This doesn't just validate the presence of the array keys, so this should cause a failure. */ $response['response']['bar'] = 'asdf'; $this->assertThrows(__NAMESPACE__ . '\\ResponseValidationException', array($request, 'validateResponse'), array('a', $response)); $response['response']['bar'] = array('knock-knock' => "who's there"); // Adding a requirement to the prototype also causes a failure $proto['baz'] = null; $request->setResponseValidator($proto); $this->assertThrows(__NAMESPACE__ . '\\ResponseValidationException', array($request, 'validateResponse'), array('a', $response)); // But we can selectively disble validation $request->setResponseValidationMethod(null); $request->validateResponse('a', $response); }