Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
User Agent for all HTTP(S) requests. (#873)
Browse files Browse the repository at this point in the history
* Continuous integration through Travis CI

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | no
| Fixed tickets |
| License       | MIT
| Doc PR        |

* Unit Test (no Azure access).

* Unit Test (no Azure access) for Common/Models

* Unit Tests (no Azure access) for Common/

* Unit Tests (no Azure access) for MediaServices.

* Unit tests for ServiceBus/Internal/

* Unit Tests (no Azure access) for ServiceBus, Service Management, and Service Runtime.

* Unit Tests (no Azure access).

* test only local unit tests.

* Fix travis-ci

* case-sensetive

* Fix unit test.

* Fix casing.

* Casing.

* Casing.

* Casing.

* Casing.

* typo fix.

* HHVM-3.9, no PHP 5.4, no PHP 7

* HHVM removed for now.

* Authentication local test

* Include skipped tests.

* Upper-case bug is fixed.

* Upper-case bug is fixed.

* Upper-case bug is fixed.

* Set User-Agent for HttpClient requests.

* Replace www.microsoft.com with example.com.

Because www.microsoft.com behaviour depends on if the 'User-Agent'
header is present.

* Remove a user agent from ServiceBuilder.

* Additional test to validate User-Agent header.

* Changelog.
  • Loading branch information
sergey-shandar authored and yaqiyang committed Sep 22, 2016
1 parent d303c34 commit 36e79cb
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 29 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- All HTTP(S) requests have the same 'User-Agent' header.
- Updated packages: firebase/php-jwt 3.0 => 4.0, fabpot/php-cs-fixer => friendofphp/php-cs-fixer.
- Support for HTTP_PROXY environment variable for logging with Fiddler
- Support for LiveStreaming in Media Services - AMS SDK Live Features & Samples
Expand Down
1 change: 0 additions & 1 deletion phpunit.local.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by PHP Project Wizard (PPW) 1.1.0-dev on Tue Feb 22 10:57:31 CET 2011 -->

<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
Expand Down
7 changes: 3 additions & 4 deletions src/Common/Internal/Http/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,9 @@ public function __construct(
null, null, $config
);

$this->setHeaders(array(
'user-agent' => null,
'expect' => '',
));
// Replace User-Agent.
$this->setHeader(Resources::USER_AGENT, Resources::SDK_USER_AGENT, true);
$this->setHeader('expect', '');

$this->_requestUrl = null;
$this->_response = null;
Expand Down
10 changes: 2 additions & 8 deletions src/Common/ServicesBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,7 @@ public function createServiceBusService($connectionString)
);

// Adding headers filter
$headers = array(
Resources::USER_AGENT => Resources::SDK_USER_AGENT,
);
$headers = array();

$headersFilter = new HeadersFilter($headers);
$serviceBusWrapper = $serviceBusWrapper->withFilter($headersFilter);
Expand Down Expand Up @@ -265,9 +263,7 @@ public function createServiceManagementService($connectionString)
);

// Adding headers filter
$headers = array(
Resources::USER_AGENT => Resources::SDK_USER_AGENT,
);
$headers = array();

$headers[Resources::X_MS_VERSION] = Resources::SM_API_LATEST_VERSION;

Expand Down Expand Up @@ -315,15 +311,13 @@ public function createMediaServicesService($settings)
$dataMaxVersion = Resources::MEDIA_SERVICES_MAX_DATA_SERVICE_VERSION_VALUE;
$accept = Resources::ACCEPT_HEADER_VALUE;
$contentType = Resources::ATOM_ENTRY_CONTENT_TYPE;
$userAgent = Resources::SDK_USER_AGENT;

$headers = array(
Resources::X_MS_VERSION => $xMSVersion,
Resources::DATA_SERVICE_VERSION => $dataVersion,
Resources::MAX_DATA_SERVICE_VERSION => $dataMaxVersion,
Resources::ACCEPT_HEADER => $accept,
Resources::CONTENT_TYPE => $contentType,
Resources::USER_AGENT => $userAgent,
);

$headersFilter = new HeadersFilter($headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use WindowsAzure\Common\Internal\Filters\HeadersFilter;
use WindowsAzure\Common\Internal\Http\HttpClient;
use WindowsAzure\Common\Internal\Resources;

/**
* Unit tests for class HeadersFilter.
Expand Down Expand Up @@ -56,8 +57,11 @@ public function testHandleRequestEmptyHeaders()
// Test
$request = $filter->handleRequest($channel);

// Assert. there is one header returned back
$this->assertCount(1, $request->getHeaders());
$headers = $request->getHeaders();
// Assert. there are two header returned back
// 'User-Agent' and 'expect'.
$this->assertCount(2, $headers);
$this->assertEquals(Resources::SDK_USER_AGENT, $headers['user-agent']);
}

/**
Expand Down
28 changes: 14 additions & 14 deletions tests/unit/WindowsAzure/Common/Internal/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ public function testSetHeaders()
$channel->setHeaders($headers);

// Assert
$channelHeaders = $channel->getHeaders();
$this->assertCount(3, $channelHeaders);
$channelHeaders = $channel->getHeaders();
$this->assertCount(4, $channelHeaders);
$this->assertEquals($value1, $channelHeaders[$header1]);
$this->assertEquals($value2, $channelHeaders[$header2]);
}
Expand All @@ -168,7 +168,7 @@ public function testGetHeaders()
$headers = $channel->getHeaders();

// Assert
$this->assertCount(3, $headers);
$this->assertCount(4, $headers);
$this->assertEquals($value1, $headers[$header1]);
$this->assertEquals($value2, $headers[$header2]);
}
Expand All @@ -186,7 +186,7 @@ public function testSetHeaderNewHeader()

// Assert
$headers = $channel->getHeaders();
$this->assertCount(2, $headers);
$this->assertCount(3, $headers);
$this->assertEquals(TestResources::HEADER1_VALUE, $headers[TestResources::HEADER1]);
}

Expand All @@ -204,7 +204,7 @@ public function testSetHeaderExistingHeaderReplace()

// Assert
$headers = $channel->getHeaders();
$this->assertCount(2, $headers);
$this->assertCount(3, $headers);
$this->assertEquals(TestResources::HEADER2_VALUE, $headers[TestResources::HEADER1]);
}

Expand All @@ -223,7 +223,7 @@ public function testSetHeaderExistingHeaderAppend()

// Assert
$headers = $channel->getHeaders();
$this->assertCount(2, $headers);
$this->assertCount(3, $headers);
$this->assertEquals($expected, $headers[TestResources::HEADER1]);
}

Expand All @@ -234,7 +234,7 @@ public function testSendSimple()
{
// Setup
$channel = new HttpClient();
$url = new Url('http://www.microsoft.com/');
$url = new Url('http://example.com/');
$channel->setExpectedStatusCode('200');

// Test
Expand All @@ -251,11 +251,11 @@ public function testSendWithContent()
{
// Setup
$channel = new HttpClient();
$url = new Url('http://www.microsoft.com/');
$url = new Url('http://example.com/');
$channel->setExpectedStatusCode('200');
$channel->setBody('This is body');
$channel->setMethod('PUT');
$this->setExpectedException(get_class(new ServiceException('404')));
$this->setExpectedException(get_class(new ServiceException('405')));

// Test
$channel->send(array(), $url);
Expand All @@ -268,7 +268,7 @@ public function testSendWithOneFilter()
{
// Setup
$channel = new HttpClient();
$url = new Url('http://www.microsoft.com/');
$url = new Url('http://example.com/');
$channel->setExpectedStatusCode('200');
$expectedHeader = TestResources::HEADER1;
$expectedResponseSubstring = TestResources::HEADER1_VALUE;
Expand All @@ -291,7 +291,7 @@ public function testSendWithMultipleFilters()
{
// Setup
$channel = new HttpClient();
$url = new Url('http://www.microsoft.com/');
$url = new Url('http://example.com/');
$channel->setExpectedStatusCode('200');
$expectedHeader1 = TestResources::HEADER1;
$expectedResponseSubstring1 = TestResources::HEADER1_VALUE;
Expand Down Expand Up @@ -319,7 +319,7 @@ public function testSendFail()
{
// Setup
$channel = new HttpClient();
$url = new Url('http://www.microsoft.com/');
$url = new Url('http://example.com/');
$channel->setExpectedStatusCode('201');
$this->setExpectedException(get_class(new ServiceException('200')));

Expand Down Expand Up @@ -456,7 +456,7 @@ public function test__clone()

// Test
$actual = clone $channel;
$channel->setUrl(new Url('http://www.microsoft.com'));
$channel->setUrl(new Url('http://example.com/'));
$channel->setHeader('headerx', 'valuex');

// Assert
Expand All @@ -471,7 +471,7 @@ public function testGetResponse()
{
// Setup
$channel = new HttpClient();
$url = new Url('http://www.microsoft.com/');
$url = new Url('http://example.com/');
$channel->setExpectedStatusCode('200');
$channel->send(array(), $url);

Expand Down

0 comments on commit 36e79cb

Please sign in to comment.