Skip to content

Commit 51a4340

Browse files
committed
Support for application/json on InboundMessage
Resolves #33
1 parent ca8109a commit 51a4340

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/Message/InboundMessage.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,18 @@ public function getRequestData($sent = true)
7171
throw new \RuntimeException('inbound message request should only ever be `' . ServerRequestInterface::class . '`');
7272
}
7373

74+
// Check our incoming content type
75+
$isApplicationJson = false;
76+
$contentTypes = $request->getHeader('Content-Type');
77+
// We only respect application/json if it's the first entry without any preference weighting
78+
// as that's what Nexmo send
79+
if (count($contentTypes) && $contentTypes[0] === 'application/json') {
80+
$isApplicationJson = true;
81+
}
82+
7483
switch($request->getMethod()){
7584
case 'POST':
76-
$params = $request->getParsedBody();
85+
$params = $isApplicationJson ? json_decode((string)$request->getBody(), true) : $request->getParsedBody();
7786
break;
7887
case 'GET':
7988
$params = $request->getQueryParams();

test/Message/InboundMessageTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ public function getResponses()
133133
public function getRequests()
134134
{
135135
return [
136-
[$this->getServerRequest('https://ohyt2ctr9l0z.runscope.net/sms_post', 'POST', 'inbound')],
137-
[$this->getServerRequest('https://ohyt2ctr9l0z.runscope.net/sms_post', 'GET', 'inbound')],
136+
'post, application/json' => [$this->getServerRequest('https://ohyt2ctr9l0z.runscope.net/sms_post', 'POST', 'json', ['Content-Type' => 'application/json'])],
137+
'post, form-encoded' => [$this->getServerRequest('https://ohyt2ctr9l0z.runscope.net/sms_post', 'POST', 'inbound')],
138+
'get, form-encoded' => [$this->getServerRequest('https://ohyt2ctr9l0z.runscope.net/sms_post', 'GET', 'inbound')],
138139
];
139140
}
140141

@@ -144,7 +145,7 @@ public function getRequests()
144145
* @param null $file
145146
* @return ServerRequest
146147
*/
147-
protected function getServerRequest($url = 'https://ohyt2ctr9l0z.runscope.net/sms_post', $method = 'GET', $type = 'inbound')
148+
protected function getServerRequest($url = 'https://ohyt2ctr9l0z.runscope.net/sms_post', $method = 'GET', $type = 'inbound', $headers = [])
148149
{
149150
$data = file_get_contents(__DIR__ . '/requests/' . $type . '.txt');
150151
$params = [];
@@ -162,10 +163,14 @@ protected function getServerRequest($url = 'https://ohyt2ctr9l0z.runscope.net/sm
162163
$body = fopen(__DIR__ . '/requests/' . $type . '.txt', 'r');
163164
$query = [];
164165
$parsed = $params;
166+
if (isset($headers['Content-Type']) && $headers['Content-Type'] === 'application/json')
167+
{
168+
$parsed = null;
169+
}
165170
break;
166171
}
167172

168-
return new ServerRequest([], [], $url, $method, $body, [], [], $query, $parsed);
173+
return new ServerRequest([], [], $url, $method, $body, $headers, [], $query, $parsed);
169174
}
170175

171176
/**

test/Message/requests/json.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"msisdn":"14845552121","to":"16105553939","messageId":"02000000DA7C52E7","text":"Test this.","type":"text","keyword":"TEST","message-timestamp":"2016-05-24 11:12:01","timestamp":"1464088321","nonce":"51a87f47-476f-447c-bffe-c18600fd12ad","sig":"8cfbb03967d8d6339c82736d7781a7fd"}

0 commit comments

Comments
 (0)