/** * The inverse operation of calling toResponse. * * This function will attempt to parse a Response object into an * ApiResponse object, which can be useful for introspection and testing. * * @param Response $response * * @throws CoreException * @return static */ public static function fromResponse(Response $response) { $body = Std::jsonDecode($response->getContent()); $result = Spec::define(['messages' => Boa::arrOf(Boa::string()), 'status' => Boa::in(static::getValidStatuses()), 'code' => Boa::integer()])->check($body); if ($result->failed()) { throw new CoreException('Unable to parse an ApiResponse out of the content of a' . ' Response object. Make sure that the Response object was' . ' actually generated by an ApiResponse or a compatible' . ' implementation.'); } return new static(Arr::except($body, static::getReservedKeys()), $body['status'], $body['messages']); }
public function testHandleWithMissingAuthorization() { $generator = new KeyPairGenerator(); $hasher = new HmacHasher(); $pair = $generator->generateHmac(); $content = json_encode(['doge' => 'much secret']); $brokenRequest = Request::create('/auth/something/important/dont/hax/pls', 'POST', [], [], [], [], $content); $brokenRequest->headers->set('Content-Hash', $hasher->hash($content, $pair->getSecretKey())); $finder = m::mock(KeyPairFinderInterface::class); $finder->shouldReceive('byPublicId')->with($pair->getPublicId(), KeyPairTypes::TYPE_HMAC)->once()->andReturn($pair); $instance = new HmacMiddleware($finder); $response = $instance->handle($brokenRequest, function () { // }); $body = Std::jsonDecode($response->getContent()); $this->assertEqualsMatrix([['One or more fields are invalid. Please check your input.', $body['messages'][0]], [['authorization'], $body['missing']]]); }