|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | +use Ramsey\Uuid\Uuid; |
| 7 | +use JSHayes\FakeRequests\MockHandler; |
| 8 | +use Ramsey\Uuid\UuidFactoryInterface; |
| 9 | +use JSHayes\FakeRequests\ClientFactory; |
| 10 | +use SoapBox\SignedRequests\Configurations\CustomConfiguration; |
| 11 | +use SoapBox\SignedRequests\Middlewares\Guzzle\GenerateSignature; |
| 12 | + |
| 13 | +class ClientTest extends TestCase |
| 14 | +{ |
| 15 | + private function expectUuid4(string $uuid): void |
| 16 | + { |
| 17 | + Uuid::setFactory(new class($uuid) implements UuidFactoryInterface { |
| 18 | + public function __construct(string $uuid) |
| 19 | + { |
| 20 | + $this->uuid = $uuid; |
| 21 | + } |
| 22 | + |
| 23 | + public function uuid1($node = null, $clockSeq = null) |
| 24 | + { |
| 25 | + return null; |
| 26 | + } |
| 27 | + |
| 28 | + public function uuid3($ns, $name) |
| 29 | + { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + public function uuid4() |
| 34 | + { |
| 35 | + return $this->uuid; |
| 36 | + } |
| 37 | + |
| 38 | + public function uuid5($ns, $name) |
| 39 | + { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + public function fromBytes($bytes) |
| 44 | + { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + public function fromString($uuid) |
| 49 | + { |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + public function fromInteger($integer) |
| 54 | + { |
| 55 | + return null; |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + protected function setUp() |
| 61 | + { |
| 62 | + parent::setUp(); |
| 63 | + |
| 64 | + $factory = new ClientFactory(); |
| 65 | + $factory->setHandler($this->handler = new MockHandler()); |
| 66 | + |
| 67 | + $algorithmHeader = 'Algorithm'; |
| 68 | + $signatureHeader = 'Signature'; |
| 69 | + $signingAlgorithm = 'sha256'; |
| 70 | + $signingKey = 'key'; |
| 71 | + |
| 72 | + $config = new CustomConfiguration( |
| 73 | + $algorithmHeader, |
| 74 | + $signatureHeader, |
| 75 | + $signingAlgorithm, |
| 76 | + $signingKey |
| 77 | + ); |
| 78 | + |
| 79 | + $middleware = new GenerateSignature($config); |
| 80 | + |
| 81 | + $this->client = $factory->make(); |
| 82 | + $this->client->getConfig('handler')->push($middleware); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @test |
| 87 | + */ |
| 88 | + public function it_generates_a_signature_with_a_simple_json_payload() |
| 89 | + { |
| 90 | + Carbon::setTestNow('2001-01-01 00:00:00'); |
| 91 | + $this->expectUuid4('303103f5-3dca-4704-96ad-860717769ec9'); |
| 92 | + |
| 93 | + $uri = 'https://localhost'; |
| 94 | + |
| 95 | + $this->handler->expects('POST', $uri) |
| 96 | + ->inspectRequest(function ($request) use ($uri) { |
| 97 | + $this->assertTrue($request->hasHeader('Algorithm')); |
| 98 | + $this->assertTrue($request->hasHeader('Signature')); |
| 99 | + $this->assertSame( |
| 100 | + 'b9f912a4fc4b2952a48380579d3e4a1c55c0537ce583b3da7cc9f6c67fe4caa7', |
| 101 | + $request->getHeader('Signature')[0] |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + $this->client->post($uri, ['json' => ['test' => 'test']]); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @test |
| 110 | + */ |
| 111 | + public function it_generates_a_signature_with_a_simple_json_payload_containing_ã() |
| 112 | + { |
| 113 | + Carbon::setTestNow('2001-01-01 00:00:00'); |
| 114 | + $this->expectUuid4('303103f5-3dca-4704-96ad-860717769ec9'); |
| 115 | + |
| 116 | + $uri = 'https://localhost'; |
| 117 | + |
| 118 | + $this->handler->expects('POST', $uri) |
| 119 | + ->inspectRequest(function ($request) use ($uri) { |
| 120 | + $this->assertTrue($request->hasHeader('Algorithm')); |
| 121 | + $this->assertTrue($request->hasHeader('Signature')); |
| 122 | + $this->assertSame( |
| 123 | + 'bf0f2eb48acf86cf72a87b48393f71fb2eebbb2c11fa0d838cbb127d74a0a00e', |
| 124 | + $request->getHeader('Signature')[0] |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + $this->client->post($uri, ['json' => ['test' => 'ã']]); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @test |
| 133 | + */ |
| 134 | + public function it_generates_a_signature_with_a_simple_json_payload_containing_好() |
| 135 | + { |
| 136 | + Carbon::setTestNow('2001-01-01 00:00:00'); |
| 137 | + $this->expectUuid4('303103f5-3dca-4704-96ad-860717769ec9'); |
| 138 | + |
| 139 | + $uri = 'https://localhost'; |
| 140 | + |
| 141 | + $this->handler->expects('POST', $uri) |
| 142 | + ->inspectRequest(function ($request) use ($uri) { |
| 143 | + $this->assertTrue($request->hasHeader('Algorithm')); |
| 144 | + $this->assertTrue($request->hasHeader('Signature')); |
| 145 | + $this->assertSame( |
| 146 | + '10b165e59775d1a564be49046edd60137d40fcecebbdf59f41b01568ca07db63', |
| 147 | + $request->getHeader('Signature')[0] |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + $this->client->post($uri, ['json' => ['test' => '好']]); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @test |
| 156 | + */ |
| 157 | + public function it_generates_a_signature_with_a_simple_json_payload_containing_a_uri() |
| 158 | + { |
| 159 | + Carbon::setTestNow('2001-01-01 00:00:00'); |
| 160 | + $this->expectUuid4('303103f5-3dca-4704-96ad-860717769ec9'); |
| 161 | + |
| 162 | + $uri = 'https://localhost'; |
| 163 | + |
| 164 | + $this->handler->expects('POST', $uri) |
| 165 | + ->inspectRequest(function ($request) use ($uri) { |
| 166 | + $this->assertTrue($request->hasHeader('Algorithm')); |
| 167 | + $this->assertTrue($request->hasHeader('Signature')); |
| 168 | + $this->assertSame( |
| 169 | + '8c36d7384111d27336c410ebfec38c7da2eca9ec4779216f9cb8f921a08c4572', |
| 170 | + $request->getHeader('Signature')[0] |
| 171 | + ); |
| 172 | + }); |
| 173 | + |
| 174 | + $this->client->post($uri, ['json' => ['test' => $uri]]); |
| 175 | + } |
| 176 | +} |
0 commit comments