Skip to content

Commit 8d7d787

Browse files
Merge pull request #44 from dapr/release/1.0.0-rc.4
Release rc.4
2 parents 2a0d0e3 + 66edad0 commit 8d7d787

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ jobs:
9696
- name: Install logger
9797
run: composer require monolog/monolog
9898
- name: Install Dapr
99-
run: wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash -s 1.0.0-rc.4
99+
run: wget -q https://raw.githubusercontent.com/dapr/cli/master/install/install.sh -O - | /bin/bash -s 1.0.0-rc.6
100100
- name: Initialize Integration Environment
101101
run: composer run-script dapr-init
102102
- name: Integration Environment Logs

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"PHP_CLI_SERVER_WORKERS=100 dapr run --components-path components -a dev -p 9502 -- php -S 0.0.0.0:9502 -t src"
4242
],
4343
"dapr-init": [
44-
"dapr init --runtime-version 1.0.0-rc.3"
44+
"dapr init --runtime-version 1.0.0-rc.4"
4545
],
4646
"clean": [
4747
"dapr uninstall",

src/index.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Dapr\PubSub\CloudEvent;
2121
use Dapr\PubSub\Publish;
2222
use Dapr\PubSub\Subscription;
23+
use Dapr\PubSub\Topic;
2324
use Dapr\State\Attributes\StateStore;
2425
use Dapr\State\StateManager;
2526
use Dapr\State\TransactionalState;
@@ -263,8 +264,11 @@ function () use ($one) {
263264
'/test/pubsub',
264265
function (FactoryInterface $container) {
265266
$publisher = $container->make(Publish::class, ['pubsub' => 'pubsub']);
266-
$topic = $publisher->topic(topic: 'test');
267-
$body = [];
267+
/**
268+
* @var Topic $topic
269+
*/
270+
$topic = $publisher->topic(topic: 'test');
271+
$body = [];
268272

269273
$topic->publish(['test_event']);
270274
sleep(5);
@@ -351,6 +355,17 @@ function (FactoryInterface $container) {
351355
unlink('/tmp/sub-received');
352356
$return['Publishing raw event'] = $body;
353357

358+
$topic->publish('raw data', content_type: 'application/octet-stream');
359+
sleep(2);
360+
$return['Binary response'] = ['raw' => json_decode($raw_event = file_get_contents('/tmp/sub-received'), true)];
361+
unlink('/tmp/sub-received');
362+
$return['Binary response'] = assert_equals(
363+
$return['Binary response'],
364+
'raw data',
365+
$return['Binary response']['raw']['data'],
366+
'Data properly decoded'
367+
);
368+
354369
return $return;
355370
}
356371
);

src/lib/PubSub/CloudEvent.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ private static function from_array(array $raw): CloudEvent
163163
if ( ! empty($time)) {
164164
$event->time = new DateTime($time);
165165
}
166-
$event->data = $raw['data'] ?? null;
166+
if (isset($raw['data_base64'])) {
167+
$event->data = base64_decode($raw['data_base64']);
168+
} else {
169+
$event->data = $raw['data'] ?? null;
170+
}
167171

168172
return $event;
169173
}

src/lib/PubSub/Topic.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ public function __construct(
2525
*
2626
* @param CloudEvent|mixed $event The event to publish
2727
* @param array|null $metadata Additional metadata to pass to the component
28+
* @param string $content_type The header to include in the publish request. Ignored when $event is a CloudEvent
2829
*
2930
* @return bool Whether the event was successfully dispatched
3031
*/
31-
public function publish(mixed $event, ?array $metadata = null): bool
32+
public function publish(mixed $event, ?array $metadata = null, $content_type = 'application/json'): bool
3233
{
3334
$this->logger->debug('Sending {event} to {topic}', ['event' => $event, 'topic' => $this->topic]);
3435
if ($event instanceof CloudEvent) {
@@ -42,7 +43,7 @@ public function publish(mixed $event, ?array $metadata = null): bool
4243
try {
4344
$this->client->post("/publish/{$this->pubsub}/{$this->topic}", $event, $metadata);
4445

45-
$this->client->extra_headers = [];
46+
$this->client->extra_headers = ['Content-Type: '.$content_type];
4647

4748
return true;
4849
} catch (DaprException) { // @codeCoverageIgnoreStart

tests/PublishTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Dapr\DaprClient;
34
use Dapr\PubSub\CloudEvent;
45
use Dapr\PubSub\Publish;
56
use DI\DependencyException;
@@ -27,6 +28,15 @@ public function testSimplePublish()
2728
$publisher->topic('topic')->publish(['my' => 'event']);
2829
}
2930

31+
public function testBinaryPublish()
32+
{
33+
$publisher = $this->container->make(Publish::class, ['pubsub' => 'pubsub']);
34+
$this->get_client()->register_post('/publish/pubsub/topic', 200, null, 'data');
35+
$publisher->topic('topic')->publish('data', content_type: 'application/octet-stream');
36+
$client = $this->container->get(DaprClient::class);
37+
$this->assertSame(['Content-Type: application/octet-stream'], $client->extra_headers);
38+
}
39+
3040
/**
3141
* @throws DependencyException
3242
* @throws NotFoundException
@@ -83,4 +93,24 @@ public function testParsingCloudEvent()
8393
$this->assertTrue($event->validate());
8494
$this->assertSame('https://example.com/message', $event->source);
8595
}
96+
97+
public function testParsingBinaryEvent()
98+
{
99+
$eventjson = <<<JSON
100+
{
101+
"specversion" : "1.0",
102+
"type" : "xml.message",
103+
"source" : "https://example.com/message",
104+
"subject" : "Test binary Message",
105+
"id" : "id-1234-5678-9101",
106+
"time" : "2020-09-23T06:23:21Z",
107+
"datacontenttype" : "application/octet-stream",
108+
"data" : "ZGF0YQ==",
109+
"data_base64": "ZGF0YQ=="
110+
}
111+
JSON;
112+
$event = CloudEvent::parse($eventjson);
113+
$this->assertTrue($event->validate());
114+
$this->assertSame('data', $event->data);
115+
}
86116
}

0 commit comments

Comments
 (0)