Skip to content

Commit 6a85d46

Browse files
committed
Added User Agent Header:
- Also adds VERSION const to client. - Includes 'PHP' in the PHP verson because. - TODO: idenify HHVM and provide correct user agent - Fixes #6
1 parent 3f495a7 commit 6a85d46

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/Client.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
*/
2929
class Client
3030
{
31+
const VERSION = '1.0.0-beta1';
32+
3133
const BASE_API = 'https://api.nexmo.com';
3234
const BASE_REST = 'https://rest.nexmo.com';
3335

34-
3536
/**
3637
* API Credentials
3738
* @var CredentialsInterface
@@ -221,6 +222,13 @@ public function send(\Psr\Http\Message\RequestInterface $request)
221222
}
222223
}
223224

225+
//set the user-agent
226+
$request = $request->withHeader('user-agent', implode('/', [
227+
'nexmo-php',
228+
self::VERSION,
229+
'PHP-' . implode('.', [PHP_MAJOR_VERSION, PHP_MINOR_VERSION])
230+
]));
231+
224232
$response = $this->client->sendRequest($request);
225233
return $response;
226234
}

test/ClientTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,36 @@ public function testNamespaceFactory()
239239
$this->assertSame($api, $client->sms());
240240
}
241241

242+
public function testUserAgentString()
243+
{
244+
$version = Client::VERSION;
245+
$php = 'PHP-' . implode('.', [
246+
PHP_MAJOR_VERSION,
247+
PHP_MINOR_VERSION
248+
]);
249+
250+
//get a mock response to test
251+
$response = new Response();
252+
$response->getBody()->write('test response');
253+
$this->http->addResponse($response);
254+
255+
$client = new Client(new Basic('key', 'secret'), [], $this->http);
256+
$request = $this->getRequest();
257+
258+
//api client should simply pass back the http response
259+
$client->send($request);
260+
261+
//useragent should match the expected format
262+
$agent = $this->http->getRequests()[0]->getHeaderLine('user-agent');
263+
$expected = implode('/', [
264+
'nexmo-php',
265+
$version,
266+
$php
267+
]);
268+
269+
$this->assertEquals($expected, $agent);
270+
}
271+
242272
/**
243273
* Allow tests to check that the API client is correctly forming the HTTP request before sending it to the HTTP
244274
* client.

0 commit comments

Comments
 (0)