Skip to content

Commit

Permalink
Merge pull request #6 from xp-forge/feature/user-agent
Browse files Browse the repository at this point in the history
Allow overwriting user agent via headers
  • Loading branch information
thekid authored Dec 2, 2023
2 parents 747026b + a6ecdff commit 2996bef
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/php/com/amazon/aws/ServiceEndpoint.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function request(string $method, string $target, array $headers= [], stri
$signed= [
'Host' => $host,
'X-Amz-Date' => $this->signature->datetime($time),
'X-Amz-User-Agent' => $this->userAgent,
'X-Amz-User-Agent' => $headers['User-Agent'] ?? $this->userAgent,
];

// Automatically include security token if available
Expand Down
74 changes: 74 additions & 0 deletions src/test/php/com/amazon/aws/unittest/RequestSigningTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php namespace com\amazon\aws\unittest;

use com\amazon\aws\{ServiceEndpoint, Credentials};
use test\{Assert, Test};

class RequestSigningTest {
const TEST_TIME= 1678835684;

/** Executes a given request handler */
private function execute($handler, $session= null) {
return (new ServiceEndpoint('test', new Credentials('key', 'secret', $session)))
->connecting(function($uri) use($handler) { return new TestConnection(['/' => $handler]); })
->request('GET', '/', ['User-Agent' => 'xp-aws/1.0.0 OS/Test/1.0 lang/php/8.3.0'], null, self::TEST_TIME)
;
}

#[Test]
public function host() {
$handler= function($request) {
return ['HTTP/1.1 200 OK', '', $request->headers['Host'][0]];
};

Assert::equals('test.amazonaws.com', $this->execute($handler)->content());
}

#[Test]
public function amz_date() {
$handler= function($request) {
return ['HTTP/1.1 200 OK', '', $request->headers['X-Amz-Date'][0]];
};

Assert::equals('20230314T231444Z', $this->execute($handler)->content());
}

#[Test]
public function amz_user_agent() {
$handler= function($request) {
return ['HTTP/1.1 200 OK', '', $request->headers['X-Amz-User-Agent'][0]];
};

Assert::matches(
'/xp-aws\/[0-9.]+ OS\/.+\/.+ lang\/php\/[0-9.]+/',
$this->execute($handler)->content()
);
}

#[Test]
public function authorization() {
$handler= function($request) {
return ['HTTP/1.1 200 OK', '', $request->headers['Authorization'][0]];
};

Assert::equals(
'AWS4-HMAC-SHA256 Credential=key/20230314/*/test/aws4_request, '.
'SignedHeaders=host;x-amz-date;x-amz-user-agent, '.
'Signature=252720f9823080fb461b7a311b52ce4dea9ed7e28c16cfa288054737e388786f',
$this->execute($handler)->content()
);
}

#[Test]
public function authorization_with_session() {
$handler= function($request) {
return ['HTTP/1.1 200 OK', '', $request->headers['Authorization'][0]];
};

Assert::equals(
'AWS4-HMAC-SHA256 Credential=key/20230314/*/test/aws4_request, '.
'SignedHeaders=host;x-amz-date;x-amz-security-token;x-amz-user-agent, '.
'Signature=c026606276b2854bcf04371f086c1e6339dbd01ce3ac04da51566521e7afc87f',
$this->execute($handler, 'session')->content()
);
}
}

0 comments on commit 2996bef

Please sign in to comment.