|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Farzai\Bitkub\ClientBuilder; |
| 6 | +use Farzai\Bitkub\Requests\GenerateSignatureV3; |
| 7 | +use Farzai\Bitkub\Tests\MockHttpClient; |
| 8 | + |
| 9 | +it('applies signature headers to request', function () { |
| 10 | + $psrClient = MockHttpClient::make() |
| 11 | + ->addSequence(fn ($client) => MockHttpClient::responseServerTimestamp()); |
| 12 | + |
| 13 | + $client = ClientBuilder::create() |
| 14 | + ->setCredentials('my-api-key', 'my-secret') |
| 15 | + ->setHttpClient($psrClient) |
| 16 | + ->build(); |
| 17 | + |
| 18 | + $config = $client->getConfig(); |
| 19 | + $interceptor = new GenerateSignatureV3($config, $client); |
| 20 | + |
| 21 | + // Build a simple request |
| 22 | + $request = (new \Farzai\Transport\RequestBuilder) |
| 23 | + ->method('POST') |
| 24 | + ->uri('/api/v3/market/balances') |
| 25 | + ->build(); |
| 26 | + |
| 27 | + $signedRequest = $interceptor->apply($request); |
| 28 | + |
| 29 | + expect($signedRequest->getHeaderLine('X-BTK-APIKEY'))->toBe('my-api-key'); |
| 30 | + expect($signedRequest->getHeaderLine('X-BTK-SIGN'))->not->toBeEmpty(); |
| 31 | + expect($signedRequest->getHeaderLine('X-BTK-TIMESTAMP'))->not->toBeEmpty(); |
| 32 | +}); |
| 33 | + |
| 34 | +it('applies signature with query string present', function () { |
| 35 | + $psrClient = MockHttpClient::make() |
| 36 | + ->addSequence(fn ($client) => MockHttpClient::responseServerTimestamp()); |
| 37 | + |
| 38 | + $client = ClientBuilder::create() |
| 39 | + ->setCredentials('my-api-key', 'my-secret') |
| 40 | + ->setHttpClient($psrClient) |
| 41 | + ->build(); |
| 42 | + |
| 43 | + $config = $client->getConfig(); |
| 44 | + $interceptor = new GenerateSignatureV3($config, $client); |
| 45 | + |
| 46 | + // Build a request with query params |
| 47 | + $request = (new \Farzai\Transport\RequestBuilder) |
| 48 | + ->method('GET') |
| 49 | + ->uri('/api/v3/market/my-open-orders') |
| 50 | + ->withQuery(['sym' => 'THB_BTC']) |
| 51 | + ->build(); |
| 52 | + |
| 53 | + $signedRequest = $interceptor->apply($request); |
| 54 | + |
| 55 | + expect($signedRequest->getHeaderLine('X-BTK-APIKEY'))->toBe('my-api-key'); |
| 56 | + expect($signedRequest->getHeaderLine('X-BTK-SIGN'))->not->toBeEmpty(); |
| 57 | + // Signature should differ from a request without query |
| 58 | + $signedRequest2 = $interceptor->apply( |
| 59 | + (new \Farzai\Transport\RequestBuilder) |
| 60 | + ->method('GET') |
| 61 | + ->uri('/api/v3/market/my-open-orders') |
| 62 | + ->build() |
| 63 | + ); |
| 64 | + |
| 65 | + expect($signedRequest->getHeaderLine('X-BTK-SIGN')) |
| 66 | + ->not->toBe($signedRequest2->getHeaderLine('X-BTK-SIGN')); |
| 67 | +}); |
| 68 | + |
| 69 | +it('applies signature with request body', function () { |
| 70 | + $psrClient = MockHttpClient::make() |
| 71 | + ->addSequence(fn ($client) => MockHttpClient::responseServerTimestamp()); |
| 72 | + |
| 73 | + $client = ClientBuilder::create() |
| 74 | + ->setCredentials('my-api-key', 'my-secret') |
| 75 | + ->setHttpClient($psrClient) |
| 76 | + ->build(); |
| 77 | + |
| 78 | + $config = $client->getConfig(); |
| 79 | + $interceptor = new GenerateSignatureV3($config, $client); |
| 80 | + |
| 81 | + // Build a request with a body |
| 82 | + $request = (new \Farzai\Transport\RequestBuilder) |
| 83 | + ->method('POST') |
| 84 | + ->uri('/api/v3/market/place-bid') |
| 85 | + ->withJson(['sym' => 'THB_BTC', 'amt' => 1000, 'rat' => 15000, 'typ' => 'limit']) |
| 86 | + ->build(); |
| 87 | + |
| 88 | + $signedRequest = $interceptor->apply($request); |
| 89 | + |
| 90 | + expect($signedRequest->getHeaderLine('X-BTK-APIKEY'))->toBe('my-api-key'); |
| 91 | + expect($signedRequest->getHeaderLine('X-BTK-SIGN'))->not->toBeEmpty(); |
| 92 | + expect($signedRequest->getHeaderLine('X-BTK-TIMESTAMP'))->not->toBeEmpty(); |
| 93 | +}); |
| 94 | + |
| 95 | +it('reuses timestamp within sync interval', function () { |
| 96 | + $psrClient = MockHttpClient::make() |
| 97 | + ->addSequence(fn ($client) => MockHttpClient::responseServerTimestamp()); |
| 98 | + |
| 99 | + $client = ClientBuilder::create() |
| 100 | + ->setCredentials('my-api-key', 'my-secret') |
| 101 | + ->setHttpClient($psrClient) |
| 102 | + ->build(); |
| 103 | + |
| 104 | + $config = $client->getConfig(); |
| 105 | + $interceptor = new GenerateSignatureV3($config, $client); |
| 106 | + |
| 107 | + $request = (new \Farzai\Transport\RequestBuilder) |
| 108 | + ->method('POST') |
| 109 | + ->uri('/api/v3/market/balances') |
| 110 | + ->build(); |
| 111 | + |
| 112 | + // First call syncs with server |
| 113 | + $signed1 = $interceptor->apply($request); |
| 114 | + |
| 115 | + // Second call should reuse the cached drift (no second HTTP call needed) |
| 116 | + $signed2 = $interceptor->apply($request); |
| 117 | + |
| 118 | + expect($signed1->getHeaderLine('X-BTK-APIKEY'))->toBe('my-api-key'); |
| 119 | + expect($signed2->getHeaderLine('X-BTK-APIKEY'))->toBe('my-api-key'); |
| 120 | +}); |
0 commit comments