diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 4527909..adb4a37 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -6,7 +6,7 @@
colors="true"
stopOnFailure="true">
-
+
diff --git a/src/Peru/Http/Async/ClientInterface.php b/src/Peru/Http/Async/ClientInterface.php
index 2c95e76..e94c3fc 100644
--- a/src/Peru/Http/Async/ClientInterface.php
+++ b/src/Peru/Http/Async/ClientInterface.php
@@ -15,4 +15,15 @@ interface ClientInterface
* @return PromiseInterface
*/
public function getAsync(string $url, array $headers = []): PromiseInterface;
+
+ /**
+ * Post Request.
+ *
+ * @param string $url
+ * @param mixed $data
+ * @param array $headers
+ *
+ * @return PromiseInterface
+ */
+ public function postAsync(string $url, $data, array $headers = []): PromiseInterface;
}
diff --git a/src/Peru/Http/Async/HttpClient.php b/src/Peru/Http/Async/HttpClient.php
index 7937e5b..5c1e026 100644
--- a/src/Peru/Http/Async/HttpClient.php
+++ b/src/Peru/Http/Async/HttpClient.php
@@ -28,11 +28,30 @@ class HttpClient extends Client implements ClientInterface
* @return PromiseInterface
*/
public function getAsync(string $url, array $headers = []): PromiseInterface
+ {
+ return $this->requestAsync('GET', $url, null, $headers);
+ }
+
+ /**
+ * Post Request.
+ *
+ * @param string $url
+ * @param mixed $data
+ * @param array $headers
+ *
+ * @return PromiseInterface
+ */
+ public function postAsync(string $url, $data, array $headers = []): PromiseInterface
+ {
+ return $this->requestAsync('POST', $url, $data, $headers);
+ }
+
+ private function requestAsync($method, $url, $data, $headers)
{
$deferred = new Deferred();
- $headers = $this->buildHeaders();
- $request = $this->request('GET', $url, $headers);
+ $headers = $this->buildHeaders($headers);
+ $request = $this->request($method, $url, $headers);
$request->on('response', function (Response $response) use ($deferred) {
$this->saveCookies($response->getHeaders());
@@ -48,7 +67,7 @@ public function getAsync(string $url, array $headers = []): PromiseInterface
$request->on('error', function ($e) use ($deferred) {
$deferred->reject($e);
});
- $request->end();
+ $request->end($data);
return $deferred->promise();
}
@@ -70,12 +89,14 @@ private function saveCookies(array $headers)
}, $responseCookies);
}
- private function buildHeaders()
+ private function buildHeaders(array $headers)
{
if (empty($this->cookies)) {
- return [];
+ return $headers;
}
- return ['Cookie' => implode('; ', $this->cookies)];
+ $headers['Cookie'] = implode('; ', $this->cookies);
+
+ return $headers;
}
}
diff --git a/src/Peru/Http/ContextClient.php b/src/Peru/Http/ContextClient.php
index b9e1e80..c87783d 100644
--- a/src/Peru/Http/ContextClient.php
+++ b/src/Peru/Http/ContextClient.php
@@ -17,7 +17,7 @@ class ContextClient implements ClientInterface
{
private const FORM_CONTENT_TYPE = 'application/x-www-form-urlencoded';
private const USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.0.1';
-
+
/**
* stream_context extra options.
*
diff --git a/src/Peru/Jne/Async/Dni.php b/src/Peru/Jne/Async/Dni.php
index b52f073..41ab4e3 100644
--- a/src/Peru/Jne/Async/Dni.php
+++ b/src/Peru/Jne/Async/Dni.php
@@ -8,7 +8,7 @@
class Dni
{
- private const URL_CONSULT_FORMAT = 'http://aplicaciones007.jne.gob.pe/srop_publico/Consulta/Afiliado/GetNombresCiudadano?DNI=%s';
+ private const URL_CONSULT = 'http://aplicaciones007.jne.gob.pe/srop_publico/Consulta/api/AfiliadoApi/GetNombresCiudadano';
/**
* @var ClientInterface
@@ -41,11 +41,20 @@ public function __construct(ClientInterface $client, DniParser $parser)
*/
public function get(string $dni): PromiseInterface
{
- $url = sprintf(self::URL_CONSULT_FORMAT, $dni);
+ $url = self::URL_CONSULT;
+ $payload = json_encode(['CODDNI' => $dni]);
return $this->client
- ->getAsync($url)
+ ->postAsync(
+ $url,
+ $payload,
+ [
+ 'Content-Type' => 'application/json;chartset=utf-8',
+ 'Content-Length' => strlen($payload),
+ 'Requestverificationtoken' => '30OB7qfO2MmL2Kcr1z4S0ttQcQpxH9pDUlZnkJPVgUhZOGBuSbGU4qM83JcSu7DZpZw-IIIfaDZgZ4vDbwE5-L9EPoBIHOOC1aSPi4FS_Sc1:clDOiaq7mKcLTK9YBVGt2R3spEU8LhtXEe_n5VG5VLPfG9UkAQfjL_WT9ZDmCCqtJypoTD26ikncynlMn8fPz_F_Y88WFufli38cUM-24PE1',
+ ])
->then(function ($raw) use ($dni) {
+ $raw = json_decode($raw)->data;
return $this->parser->parse($dni, $raw);
});
}
diff --git a/src/Peru/Jne/Dni.php b/src/Peru/Jne/Dni.php
index 7d1c6f2..de8b31d 100644
--- a/src/Peru/Jne/Dni.php
+++ b/src/Peru/Jne/Dni.php
@@ -11,13 +11,12 @@
use Peru\Http\ClientInterface;
use Peru\Reniec\Person;
use Peru\Services\DniInterface;
-
/**
* Class Dni.
*/
class Dni implements DniInterface
{
- private const URL_CONSULT_FORMAT = 'http://aplicaciones007.jne.gob.pe/srop_publico/Consulta/Afiliado/GetNombresCiudadano?DNI=%s';
+ private const URL_CONSULT = 'http://aplicaciones007.jne.gob.pe/srop_publico/Consulta/api/AfiliadoApi/GetNombresCiudadano';
/**
* @var ClientInterface
*/
@@ -48,8 +47,16 @@ public function __construct(ClientInterface $client, DniParser $parser)
*/
public function get(string $dni): ?Person
{
- $url = sprintf(self::URL_CONSULT_FORMAT, $dni);
- $raw = $this->client->get($url);
+ $url = self::URL_CONSULT;
+
+ $json = $this->client->post(
+ $url,
+ json_encode(['CODDNI' => $dni]),
+ [
+ 'Content-Type' => 'application/json;chartset=utf-8',
+ 'Requestverificationtoken' => '30OB7qfO2MmL2Kcr1z4S0ttQcQpxH9pDUlZnkJPVgUhZOGBuSbGU4qM83JcSu7DZpZw-IIIfaDZgZ4vDbwE5-L9EPoBIHOOC1aSPi4FS_Sc1:clDOiaq7mKcLTK9YBVGt2R3spEU8LhtXEe_n5VG5VLPfG9UkAQfjL_WT9ZDmCCqtJypoTD26ikncynlMn8fPz_F_Y88WFufli38cUM-24PE1',
+ ]);
+ $raw = json_decode($json)->data;
return $this->parser->parse($dni, $raw);
}
diff --git a/tests/Peru/Http/Async/HttpClientTest.php b/tests/Peru/Http/Async/HttpClientTest.php
index bc9ce82..c6c02aa 100644
--- a/tests/Peru/Http/Async/HttpClientTest.php
+++ b/tests/Peru/Http/Async/HttpClientTest.php
@@ -49,6 +49,31 @@ public function testGetWithError()
await($this->client->getAsync('http://http323bin.org'), $this->loop);
}
+ /**
+ * @throws \Exception
+ */
+ public function testPost()
+ {
+ $data = json_encode(['result' => 42]);
+ $result = await($this->client->postAsync('https://httpbin.org/post', $data, [
+ 'Content-Type' => 'application/json',
+ 'Content-Length' => strlen($data)
+ ]), $this->loop);
+
+ $obj = json_decode($result);
+
+ $this->assertEquals(42, $obj->json->result);
+ }
+
+ /**
+ * @expectedException \RuntimeException
+ * @throws \Exception
+ */
+ public function testPostWithError()
+ {
+ await($this->client->postAsync('https://httpbin323.org/post', ''), $this->loop);
+ }
+
protected function tearDown()
{
$this->loop->run();
diff --git a/tests/Peru/Jne/Async/DniTest.php b/tests/Peru/Jne/Async/DniTest.php
index 5b1c312..6c8ace6 100644
--- a/tests/Peru/Jne/Async/DniTest.php
+++ b/tests/Peru/Jne/Async/DniTest.php
@@ -12,6 +12,7 @@
use PHPUnit\Framework\TestCase;
use React\EventLoop\Factory;
use React\EventLoop\LoopInterface;
+use Tests\Peru\Sunat\Async\HttpClientStub;
class DniTest extends TestCase
{
@@ -27,7 +28,7 @@ class DniTest extends TestCase
protected function setUp()
{
$this->loop = Factory::create();
- $this->consult = new Dni(new HttpClient($this->loop), new DniParser());
+ $this->consult = new Dni(new HttpClientStub(new HttpClient($this->loop)), new DniParser());
}
/**
@@ -35,12 +36,12 @@ protected function setUp()
*/
public function testGetDni()
{
- $promise = $this->consult->get('48004836');
- /**@var $person Person */
- $person = await($promise, $this->loop);
+ $promise = $this->consult->get('48004836');
+ /**@var $person Person */
+ $person = await($promise, $this->loop);
- $this->assertNotNull($person);
- $this->assertEquals('48004836', $person->dni);
+ $this->assertNotNull($person);
+ $this->assertEquals('48004836', $person->dni);
}
protected function tearDown()
diff --git a/tests/Peru/Jne/DniTest.php b/tests/Peru/Jne/DniTest.php
index d8a8093..3e9105e 100644
--- a/tests/Peru/Jne/DniTest.php
+++ b/tests/Peru/Jne/DniTest.php
@@ -12,6 +12,7 @@
use Peru\{Http\ContextClient, Http\EmptyResponseDecorator, Jne\Dni, Jne\DniParser};
use PHPUnit\Framework\TestCase;
+use Tests\Peru\Sunat\ClientStubDecorator;
/**
* Class DniTest
@@ -33,7 +34,7 @@ public function setUp()
]
];
- $this->cs = new Dni(new EmptyResponseDecorator($client), new DniParser());
+ $this->cs = new Dni(new ClientStubDecorator(new EmptyResponseDecorator($client)), new DniParser());
}
/**
diff --git a/tests/Peru/Sunat/Async/HttpClientStub.php b/tests/Peru/Sunat/Async/HttpClientStub.php
index dd033b6..d1b7e60 100644
--- a/tests/Peru/Sunat/Async/HttpClientStub.php
+++ b/tests/Peru/Sunat/Async/HttpClientStub.php
@@ -36,4 +36,18 @@ public function getAsync(string $url, array $headers = []): PromiseInterface
{
return $this->client->getAsync(ClientStubDecorator::getNewUrl($url), $headers);
}
+
+ /**
+ * Post Request.
+ *
+ * @param string $url
+ * @param mixed $data
+ * @param array $headers
+ *
+ * @return PromiseInterface
+ */
+ public function postAsync(string $url, $data, array $headers = []): PromiseInterface
+ {
+ return $this->client->postAsync(ClientStubDecorator::getNewUrl($url), $data, $headers);
+ }
}
\ No newline at end of file
diff --git a/tests/Peru/Sunat/ClientStubDecorator.php b/tests/Peru/Sunat/ClientStubDecorator.php
index 6df2042..a94c4c2 100644
--- a/tests/Peru/Sunat/ClientStubDecorator.php
+++ b/tests/Peru/Sunat/ClientStubDecorator.php
@@ -57,7 +57,11 @@ public function post(string $url, $data, array $headers = [])
public static function getNewUrl($url)
{
- $urlBase = getenv('MOCK_URL');
+ $urlBase = $_ENV['MOCK_URL'];
+ if (empty($urlBase)) {
+ return $url;
+ }
+
$u = parse_url($url);
return $urlBase.$u['path'].(isset($u['query']) ? "?$u[query]" : '');