Skip to content

Commit

Permalink
Merge branch 'release/0.8.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Nov 10, 2023
2 parents 7e1a773 + 3a1f8e4 commit cf18ab1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
11 changes: 5 additions & 6 deletions src/Lunr/Vortex/JPush/JPushDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public function __construct(Session $http, LoggerInterface $logger)
$this->http = $http;
$this->logger = $logger;
$this->auth_token = NULL;

$this->http->options = [
'timeout' => 15, // timeout in seconds
'connect_timeout' => 15 // timeout in seconds
];
}

/**
Expand Down Expand Up @@ -153,10 +148,14 @@ protected function push_batch(JPushPayload $payload, array &$endpoints): JPushBa
$tmp_payload['audience']['registration_id'] = $endpoints;

$json_payload = json_encode($tmp_payload, JSON_UNESCAPED_UNICODE);
$options = [
'timeout' => 15, // timeout in seconds
'connect_timeout' => 15 // timeout in seconds
];

try
{
$http_response = $this->http->post(self::JPUSH_SEND_URL, [], $json_payload, []);
$http_response = $this->http->post(self::JPUSH_SEND_URL, [], $json_payload, $options);
}
catch (RequestsException $e)
{
Expand Down
3 changes: 0 additions & 3 deletions src/Lunr/Vortex/JPush/Tests/JPushDispatcherBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class JPushDispatcherBaseTest extends JPushDispatcherTest
public function testRequestsSessionIsSetCorrectly(): void
{
$this->assertPropertySame('http', $this->http);

$this->assertSame(15, $this->http->options['timeout']);
$this->assertSame(15, $this->http->options['connect_timeout']);
}

/**
Expand Down
23 changes: 15 additions & 8 deletions src/Lunr/Vortex/JPush/Tests/JPushDispatcherPushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,15 @@ public function testPushWithFailedRequest(): void
$url = 'https://api.jpush.cn/v3/push';
$post = '{"alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
->willReturn($payload);

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->throwException(new RequestsException('cURL error 10: Request error', 'curlerror', NULL)));

$message = 'Dispatching JPush notification(s) failed: {message}';
Expand Down Expand Up @@ -187,14 +188,15 @@ public function testPushWithTimeoutRequest()
$url = 'https://api.jpush.cn/v3/push';
$post = '{"alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
->willReturn($payload);

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->throwException(new RequestsException('cURL error 28: Request timed out', 'curlerror', NULL)));

$message = 'Dispatching JPush notification(s) failed: {message}';
Expand Down Expand Up @@ -224,6 +226,7 @@ public function testPushWithFailureResponse()
$url = 'https://api.jpush.cn/v3/push';
$post = '{"alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
Expand All @@ -235,7 +238,7 @@ public function testPushWithFailureResponse()

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->returnValue($response));

$message = 'Dispatching JPush notification failed: {error}';
Expand Down Expand Up @@ -263,6 +266,7 @@ public function testPushRequestWithDefaultValues(): void
$url = 'https://api.jpush.cn/v3/push';
$post = '{"alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
Expand All @@ -272,7 +276,7 @@ public function testPushRequestWithDefaultValues(): void

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->returnValue($response));

$this->class->push($this->payload, $endpoints);
Expand All @@ -289,6 +293,7 @@ public function testPushRequestWithSingleEndpoint(): void
$url = 'https://api.jpush.cn/v3/push';
$post = '{"collapse_key":"abcde-12345","alert":"hello","audience":{"registration_id":["endpoint"]}}';
$payload = [ 'collapse_key' => 'abcde-12345', 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
Expand All @@ -300,7 +305,7 @@ public function testPushRequestWithSingleEndpoint(): void

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->returnValue($response));

$this->class->push($this->payload, $endpoints);
Expand All @@ -317,6 +322,7 @@ public function testPushRequestWithMultibyteCharacters(): void
$url = 'https://api.jpush.cn/v3/push';
$post = '{"collapse_key":"abcde-12345","alert":"hello","message":{"msg_content":"凄い"},"audience":{"registration_id":["endpoint"]}}';
$payload = [ 'collapse_key' => 'abcde-12345', 'alert' => 'hello', 'message' => [ 'msg_content' => '凄い' ] ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(1))
->method('get_payload')
Expand All @@ -328,7 +334,7 @@ public function testPushRequestWithMultibyteCharacters(): void

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->willReturn($response);

$this->class->push($this->payload, $endpoints);
Expand Down Expand Up @@ -366,7 +372,7 @@ public function testPushRequestWithMultipleEndpointsOneBatch(): void

$this->http->expects($this->once())
->method('post')
->with($url, [], $post, [])
->with($url, [], $post, $options)
->will($this->returnValue($response));

$this->class->push($this->payload, $endpoints);
Expand Down Expand Up @@ -394,14 +400,15 @@ public function testPushRequestWithMultipleEndpointsMultipleBatches(): void
$post2 = '{"collapse_key":"abcde-12345","alert":"hello","audience":{"registration_id":["endpoint3","endpoint4"]}}';
$post3 = '{"collapse_key":"abcde-12345","alert":"hello","audience":{"registration_id":["endpoint5"]}}';
$payload = [ 'collapse_key' => 'abcde-12345', 'alert' => 'hello' ];
$options = [ 'timeout' => 15, 'connect_timeout' => 15 ];

$this->payload->expects($this->exactly(3))
->method('get_payload')
->willReturn($payload);

$this->http->expects($this->exactly(3))
->method('post')
->withConsecutive([ $url, [], $post1, []], [ $url, [], $post2, []], [ $url, [], $post3, []])
->withConsecutive([ $url, [], $post1, $options ], [ $url, [], $post2, $options ], [ $url, [], $post3, $options ])
->will($this->returnValue($response));

$this->class->push($this->payload, $endpoints);
Expand Down

0 comments on commit cf18ab1

Please sign in to comment.