future-public: add set leverage request
This commit is contained in:
1 parent
bea493935b
commit
0996c5fc27
9 files changed
+377
-25
No files matched your search
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
use Msr\LaravelBitunixApi\Requests\ChangeLeverageRequestContract;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Handler\MockHandler;
|
||||
use GuzzleHttp\HandlerStack;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
|
||||
it('can change leverage successfully', function () {
|
||||
$api = app(ChangeLeverageRequestContract::class);
|
||||
// This test will make a real API call if credentials are valid
|
||||
// For testing purposes, we'll just verify the method exists and can be called
|
||||
expect(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 12))
|
||||
->not->toThrow(Exception::class);
|
||||
});
|
||||
|
||||
it('validates required parameters for change leverage', function () {
|
||||
$api = app(ChangeLeverageRequestContract::class);
|
||||
|
||||
expect(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 10))
|
||||
->not->toThrow(Exception::class)
|
||||
->and(fn() => $api->changeLeverage('BTCUSDT', 'USDT', 0))
|
||||
->not->toThrow(Exception::class);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
use Msr\LaravelBitunixApi\Requests\Header;
|
||||
|
||||
beforeEach(function () {
|
||||
config([
|
||||
'bitunix-api.api_key' => 'test-api-key',
|
||||
'bitunix-api.api_secret' => 'test-secret-key',
|
||||
'bitunix-api.language' => 'en-US',
|
||||
]);
|
||||
});
|
||||
|
||||
it('can sort query parameters in ascending ASCII order', function () {
|
||||
$parameters = [
|
||||
'uid' => '200',
|
||||
'id' => '1',
|
||||
'name' => 'test'
|
||||
];
|
||||
|
||||
$sorted = Header::sortQueryParameters($parameters);
|
||||
|
||||
expect(array_keys($sorted))->toBe(['id', 'name', 'uid']);
|
||||
expect($sorted)->toBe([
|
||||
'id' => '1',
|
||||
'name' => 'test',
|
||||
'uid' => '200'
|
||||
]);
|
||||
});
|
||||
|
||||
it('can digest query parameters to string format', function () {
|
||||
$parameters = [
|
||||
'id' => '1',
|
||||
'uid' => '200'
|
||||
];
|
||||
|
||||
$result = Header::digestQueryParameters($parameters);
|
||||
|
||||
expect($result)->toBe('id1uid200');
|
||||
});
|
||||
|
||||
it('can generate a valid nonce', function () {
|
||||
$nonce = Header::generateNonce();
|
||||
|
||||
expect($nonce)
|
||||
->toBeString()
|
||||
->toHaveLength(32)
|
||||
->toMatch('/^[a-f0-9]+$/');
|
||||
});
|
||||
|
||||
it('can generate timestamp in milliseconds', function () {
|
||||
$timestamp = Header::generateTimestamp();
|
||||
|
||||
expect($timestamp)
|
||||
->toBeString()
|
||||
->toMatch('/^\d{13}$/'); // 13 digits for milliseconds
|
||||
});
|
||||
|
||||
it('can generate signature according to Bitunix documentation', function () {
|
||||
$nonce = '123456';
|
||||
$timestamp = '20241120123045';
|
||||
$queryParams = ['id' => '1', 'uid' => '200'];
|
||||
$body = '{"uid":"2899","arr":[{"id":1,"name":"maple"},{"id":2,"name":"lily"}]}';
|
||||
|
||||
$sign = Header::generateSignValue($queryParams, $body, $nonce, $timestamp);
|
||||
|
||||
expect($sign)
|
||||
->toBeString()
|
||||
->toHaveLength(64) // SHA256 hex length
|
||||
->toMatch('/^[a-f0-9]+$/');
|
||||
});
|
||||
|
||||
it('can generate complete headers for authenticated requests', function () {
|
||||
$queryParams = ['symbol' => 'BTCUSDT'];
|
||||
$body = '{"symbol":"BTCUSDT","marginCoin":"USDT","leverage":12}';
|
||||
|
||||
$headers = Header::generateHeaders($queryParams, $body);
|
||||
|
||||
expect($headers)
|
||||
->toHaveKeys(['api-key', 'sign', 'nonce', 'timestamp', 'language', 'Content-Type'])
|
||||
->and($headers['api-key'])->toBe('test-api-key')
|
||||
->and($headers['sign'])->toBeString()
|
||||
->and($headers['nonce'])->toBeString()
|
||||
->and($headers['timestamp'])->toBeString()
|
||||
->and($headers['language'])->toBe('en-US')
|
||||
->and($headers['Content-Type'])->toBe('application/json');
|
||||
});
|
||||
|
||||
it('throws exception when API credentials are missing', function () {
|
||||
config([
|
||||
'bitunix-api.api_key' => '',
|
||||
'bitunix-api.api_secret' => '',
|
||||
]);
|
||||
|
||||
expect(fn() => Header::generateSignValue([], '', 'nonce', 'timestamp'))
|
||||
->toThrow(InvalidArgumentException::class, 'API key and secret must be configured');
|
||||
});
|
||||
|
||||
it('handles empty query parameters correctly', function () {
|
||||
$result = Header::digestQueryParameters([]);
|
||||
|
||||
expect($result)->toBe('');
|
||||
});
|
||||
|
||||
it('handles empty body correctly', function () {
|
||||
$nonce = '123456';
|
||||
$timestamp = '20241120123045';
|
||||
|
||||
$sign = Header::generateSignValue([], '', $nonce, $timestamp);
|
||||
|
||||
expect($sign)
|
||||
->toBeString()
|
||||
->toHaveLength(64);
|
||||
});
|
||||
|
||||
it('generates consistent signature for same inputs', function () {
|
||||
$nonce = '123456';
|
||||
$timestamp = '20241120123045';
|
||||
$queryParams = ['id' => '1'];
|
||||
$body = '{"test":"value"}';
|
||||
|
||||
$sign1 = Header::generateSignValue($queryParams, $body, $nonce, $timestamp);
|
||||
$sign2 = Header::generateSignValue($queryParams, $body, $nonce, $timestamp);
|
||||
|
||||
expect($sign1)->toBe($sign2);
|
||||
});
|
||||
|
||||
it('generates different signatures for different inputs', function () {
|
||||
$nonce = '123456';
|
||||
$timestamp = '20241120123045';
|
||||
|
||||
$sign1 = Header::generateSignValue(['id' => '1'], '{"test":"value1"}', $nonce, $timestamp);
|
||||
$sign2 = Header::generateSignValue(['id' => '2'], '{"test":"value2"}', $nonce, $timestamp);
|
||||
|
||||
expect($sign1)->not->toBe($sign2);
|
||||
});
|
||||
Reference in new issue
Block a user