|
6 | 6 |
|
7 | 7 | use Deviantintegral\Har\Cache; |
8 | 8 | use Deviantintegral\Har\Content; |
| 9 | +use Deviantintegral\Har\Cookie; |
9 | 10 | use Deviantintegral\Har\Creator; |
10 | 11 | use Deviantintegral\Har\Entry; |
11 | 12 | use Deviantintegral\Har\Har; |
@@ -566,6 +567,94 @@ public function testRedactBodyFieldsJsonPreservesSlashesAndUnicode(): void |
566 | 567 | $this->assertEquals('[REDACTED]', $data['password']); |
567 | 568 | } |
568 | 569 |
|
| 570 | + public function testRedactCookies(): void |
| 571 | + { |
| 572 | + $har = $this->createHarWithRequestCookies([ |
| 573 | + 'session_id' => 'abc123', |
| 574 | + 'tracking' => 'xyz789', |
| 575 | + 'preferences' => 'dark_mode', |
| 576 | + ]); |
| 577 | + |
| 578 | + $sanitizer = new HarSanitizer(); |
| 579 | + $sanitizer->redactCookies(['session_id', 'tracking']); |
| 580 | + |
| 581 | + $sanitized = $sanitizer->sanitize($har); |
| 582 | + |
| 583 | + $cookies = $sanitized->getLog()->getEntries()[0]->getRequest()->getCookies(); |
| 584 | + $cookieMap = $this->cookiesToMap($cookies); |
| 585 | + |
| 586 | + $this->assertEquals('[REDACTED]', $cookieMap['session_id']); |
| 587 | + $this->assertEquals('[REDACTED]', $cookieMap['tracking']); |
| 588 | + $this->assertEquals('dark_mode', $cookieMap['preferences']); |
| 589 | + } |
| 590 | + |
| 591 | + public function testRedactResponseCookies(): void |
| 592 | + { |
| 593 | + $har = $this->createHarWithResponseCookies([ |
| 594 | + 'session_id' => 'secret-session', |
| 595 | + 'auth_token' => 'secret-token', |
| 596 | + 'locale' => 'en_US', |
| 597 | + ]); |
| 598 | + |
| 599 | + $sanitizer = new HarSanitizer(); |
| 600 | + $sanitizer->redactCookies(['session_id', 'auth_token']); |
| 601 | + |
| 602 | + $sanitized = $sanitizer->sanitize($har); |
| 603 | + |
| 604 | + $cookies = $sanitized->getLog()->getEntries()[0]->getResponse()->getCookies(); |
| 605 | + $cookieMap = $this->cookiesToMap($cookies); |
| 606 | + |
| 607 | + $this->assertEquals('[REDACTED]', $cookieMap['session_id']); |
| 608 | + $this->assertEquals('[REDACTED]', $cookieMap['auth_token']); |
| 609 | + $this->assertEquals('en_US', $cookieMap['locale']); |
| 610 | + } |
| 611 | + |
| 612 | + public function testRedactCookiesCaseInsensitive(): void |
| 613 | + { |
| 614 | + $har = $this->createHarWithRequestCookies([ |
| 615 | + 'SESSION_ID' => 'secret1', |
| 616 | + 'Session_Id' => 'secret2', |
| 617 | + ]); |
| 618 | + |
| 619 | + $sanitizer = new HarSanitizer(); |
| 620 | + $sanitizer->redactCookies(['session_id']); |
| 621 | + |
| 622 | + $sanitized = $sanitizer->sanitize($har); |
| 623 | + |
| 624 | + $cookies = $sanitized->getLog()->getEntries()[0]->getRequest()->getCookies(); |
| 625 | + $cookieMap = $this->cookiesToMap($cookies); |
| 626 | + |
| 627 | + $this->assertEquals('[REDACTED]', $cookieMap['SESSION_ID']); |
| 628 | + $this->assertEquals('[REDACTED]', $cookieMap['Session_Id']); |
| 629 | + } |
| 630 | + |
| 631 | + public function testRedactCookiesFluentInterface(): void |
| 632 | + { |
| 633 | + $sanitizer = new HarSanitizer(); |
| 634 | + |
| 635 | + $result = $sanitizer->redactCookies(['session_id']); |
| 636 | + |
| 637 | + $this->assertSame($sanitizer, $result); |
| 638 | + } |
| 639 | + |
| 640 | + public function testRedactCookiesOriginalUnmodified(): void |
| 641 | + { |
| 642 | + $har = $this->createHarWithRequestCookies([ |
| 643 | + 'session_id' => 'original-value', |
| 644 | + ]); |
| 645 | + |
| 646 | + $originalValue = $har->getLog()->getEntries()[0]->getRequest()->getCookies()[0]->getValue(); |
| 647 | + |
| 648 | + $sanitizer = new HarSanitizer(); |
| 649 | + $sanitizer->redactCookies(['session_id']); |
| 650 | + $sanitizer->sanitize($har); |
| 651 | + |
| 652 | + // Original should be unchanged |
| 653 | + $currentValue = $har->getLog()->getEntries()[0]->getRequest()->getCookies()[0]->getValue(); |
| 654 | + $this->assertEquals($originalValue, $currentValue); |
| 655 | + $this->assertEquals('original-value', $currentValue); |
| 656 | + } |
| 657 | + |
569 | 658 | public function testWithRealFixture(): void |
570 | 659 | { |
571 | 660 | $repository = $this->getHarFileRepository(); |
@@ -879,4 +968,67 @@ private function createHarWithTextResponse(string $text): Har |
879 | 968 |
|
880 | 969 | return $this->createHarWithResponse($response); |
881 | 970 | } |
| 971 | + |
| 972 | + /** |
| 973 | + * @param array<string, string> $cookies |
| 974 | + */ |
| 975 | + private function createHarWithRequestCookies(array $cookies): Har |
| 976 | + { |
| 977 | + $cookieObjects = []; |
| 978 | + foreach ($cookies as $name => $value) { |
| 979 | + $cookie = (new Cookie())->setName($name)->setValue($value); |
| 980 | + $cookieObjects[] = $cookie; |
| 981 | + } |
| 982 | + |
| 983 | + $request = (new Request()) |
| 984 | + ->setMethod('GET') |
| 985 | + ->setUrl(new Uri('https://example.com')) |
| 986 | + ->setHeaders([]) |
| 987 | + ->setCookies($cookieObjects) |
| 988 | + ->setHttpVersion('HTTP/1.1'); |
| 989 | + |
| 990 | + return $this->createHarWithRequest($request); |
| 991 | + } |
| 992 | + |
| 993 | + /** |
| 994 | + * @param array<string, string> $cookies |
| 995 | + */ |
| 996 | + private function createHarWithResponseCookies(array $cookies): Har |
| 997 | + { |
| 998 | + $cookieObjects = []; |
| 999 | + foreach ($cookies as $name => $value) { |
| 1000 | + $cookie = (new Cookie())->setName($name)->setValue($value); |
| 1001 | + $cookieObjects[] = $cookie; |
| 1002 | + } |
| 1003 | + |
| 1004 | + $content = (new Content()) |
| 1005 | + ->setSize(0) |
| 1006 | + ->setCompression(0); |
| 1007 | + |
| 1008 | + $response = (new Response()) |
| 1009 | + ->setStatus(200) |
| 1010 | + ->setStatusText('OK') |
| 1011 | + ->setHeaders([]) |
| 1012 | + ->setCookies($cookieObjects) |
| 1013 | + ->setHttpVersion('HTTP/1.1') |
| 1014 | + ->setContent($content) |
| 1015 | + ->setRedirectURL(new Uri('')); |
| 1016 | + |
| 1017 | + return $this->createHarWithResponse($response); |
| 1018 | + } |
| 1019 | + |
| 1020 | + /** |
| 1021 | + * @param Cookie[] $cookies |
| 1022 | + * |
| 1023 | + * @return array<string, string> |
| 1024 | + */ |
| 1025 | + private function cookiesToMap(array $cookies): array |
| 1026 | + { |
| 1027 | + $map = []; |
| 1028 | + foreach ($cookies as $cookie) { |
| 1029 | + $map[$cookie->getName()] = $cookie->getValue(); |
| 1030 | + } |
| 1031 | + |
| 1032 | + return $map; |
| 1033 | + } |
882 | 1034 | } |
0 commit comments