/** * Request a resource (URL is found using ACME server directory). * * @param string $method * @param string $resource * @param array $payload * @param bool $returnJson * * @throws AcmeCoreServerException When the ACME server returns an error HTTP status code. * @throws AcmeCoreClientException When an error occured during response parsing. * * @return array|string */ protected function requestResource($method, $resource, array $payload, $returnJson = true) { if (!$this->directory) { $this->initializeDirectory(); } return $this->httpClient->signedRequest($method, $this->directory->getResourceUrl($resource), $payload, $returnJson); }
public function testSignedRequestPayload() { $container = []; $stack = HandlerStack::create(new MockHandler([new Response(200, [], json_encode(['test' => 'ok']))])); $stack->push(Middleware::history($container)); $keyPairGenerator = new KeyPairGenerator(); $dataSigner = $this->getMockBuilder(DataSigner::class)->getMock(); $dataSigner->expects($this->once())->method('signData')->willReturn('foobar'); $client = new SecureHttpClient($keyPairGenerator->generateKeyPair(), new Client(['handler' => $stack]), new Base64SafeEncoder(), new KeyParser(), $dataSigner, $this->getMockBuilder(ServerErrorHandler::class)->getMock()); $client->signedRequest('POST', '/acme/new-reg', ['contact' => '*****@*****.**'], true); // Check request object $this->assertCount(1, $container); /** @var RequestInterface $request */ $request = $container[0]['request']; $this->assertInstanceOf(RequestInterface::class, $request); $this->assertEquals('POST', $request->getMethod()); $this->assertEquals('/acme/new-reg', $request->getUri() instanceof Uri ? $request->getUri()->getPath() : $request->getUri()); $body = \GuzzleHttp\Psr7\copy_to_string($request->getBody()); $payload = @json_decode($body, true); $this->assertInternalType('array', $payload); $this->assertArrayHasKey('header', $payload); $this->assertArrayHasKey('alg', $payload['header']); $this->assertArrayHasKey('jwk', $payload['header']); $this->assertArrayHasKey('protected', $payload); $this->assertArrayHasKey('payload', $payload); $this->assertArrayHasKey('signature', $payload); $this->assertEquals('Zm9vYmFy', $payload['signature']); }