|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Message; |
| 4 | + |
| 5 | +use Http\Message\Exception\UnexpectedValueException; |
| 6 | +use PhpSpec\ObjectBehavior; |
| 7 | + |
| 8 | +class CookieUtilSpec extends ObjectBehavior |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @dataProvider getCookieStrings |
| 12 | + */ |
| 13 | + function it_parses_cookie_date_string($cookieDateString, $expectedString) |
| 14 | + { |
| 15 | + $this->beConstructedThrough('parseDate', [$cookieDateString]); |
| 16 | + $this->shouldHaveType('\DateTime'); |
| 17 | + $this->format('l, d-M-Y H:i:s O')->shouldReturn($expectedString); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @dataProvider getInvalidCookieDateStrings |
| 22 | + */ |
| 23 | + function it_throws_an_exception_if_cookie_date_string_is_unparseable($cookieDateString) |
| 24 | + { |
| 25 | + $this->beConstructedThrough('parseDate', [$cookieDateString]); |
| 26 | + $this->shouldThrow('Http\Message\Exception\UnexpectedValueException'); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Provides examples for valid cookie date string. |
| 31 | + * |
| 32 | + * @return array |
| 33 | + */ |
| 34 | + public function getCookieStrings() |
| 35 | + { |
| 36 | + return [ |
| 37 | + ['Friday, 31 Jul 20 08:49:37 GMT', 'Friday, 31-Jul-2020 08:49:37 +0000'], |
| 38 | + ['Friday, 31-Jul-20 08:49:37 GMT', 'Friday, 31-Jul-2020 08:49:37 +0000'], |
| 39 | + ['Fri, 31-Jul-2020 08:49:37 GMT', 'Friday, 31-Jul-2020 08:49:37 +0000'], |
| 40 | + ['Fri, 31 Jul 2020 08:49:37 GMT', 'Friday, 31-Jul-2020 08:49:37 +0000'], |
| 41 | + ['Fri, 31-07-2020 08:49:37 GMT', 'Friday, 31-Jul-2020 08:49:37 +0000'], |
| 42 | + ['Fri, 31-07-20 08:49:37 GMT', 'Friday, 31-Jul-2020 08:49:37 +0000'], |
| 43 | + ['Friday, 31-Jul-20 08:49:37 GMT', 'Friday, 31-Jul-2020 08:49:37 +0000'], |
| 44 | + ['Fri Jul 31 08:49:37 2020', 'Friday, 31-Jul-2020 08:49:37 +0000'], |
| 45 | + ['Friday July 31st 2020, 08:49:37 GMT', 'Friday, 31-Jul-2020 08:49:37 +0000'], |
| 46 | + // https://github.com/salesforce/tough-cookie/blob/master/test/date_test.js#L52 |
| 47 | + ['Wed, 09 Jun 2021 10:18:14 GMT', 'Wednesday, 09-Jun-2021 10:18:14 +0000'], |
| 48 | + ['Wed, 09 Jun 2021 22:18:14 GMT', 'Wednesday, 09-Jun-2021 22:18:14 +0000'], |
| 49 | + ['Tue, 18 Oct 2011 07:42:42.123 GMT', 'Tuesday, 18-Oct-2011 07:42:42 +0000'], |
| 50 | + ['18 Oct 2011 07:42:42 GMT', 'Tuesday, 18-Oct-2011 07:42:42 +0000'], |
| 51 | + ['8 Oct 2011 7:42:42 GMT', 'Saturday, 08-Oct-2011 07:42:42 +0000'], |
| 52 | + ['8 Oct 2011 7:2:42 GMT', 'Saturday, 08-Oct-2011 07:02:42 +0000'], |
| 53 | + ['Oct 18 2011 07:42:42 GMT', 'Tuesday, 18-Oct-2011 07:42:42 +0000'], |
| 54 | + ['Tue Oct 18 2011 07:05:03 GMT+0000 (GMT)', 'Tuesday, 18-Oct-2011 07:05:03 +0000'], |
| 55 | + ['09 Jun 2021 10:18:14 GMT', 'Wednesday, 09-Jun-2021 10:18:14 +0000'], |
| 56 | + ['01 Jan 1970 00:00:00 GMT', 'Thursday, 01-Jan-1970 00:00:00 +0000'], |
| 57 | + ['01 Jan 1601 00:00:00 GMT', 'Monday, 01-Jan-1601 00:00:00 +0000'], |
| 58 | + ['10 Feb 81 13:00:00 GMT', 'Tuesday, 10-Feb-1981 13:00:00 +0000'], // implicit year |
| 59 | + ['Thu, 17-Apr-2014 02:12:29 GMT', 'Thursday, 17-Apr-2014 02:12:29 +0000'], // dashes |
| 60 | + ['Thu, 17-Apr-2014 02:12:29 UTC', 'Thursday, 17-Apr-2014 02:12:29 +0000'], // dashes and UTC |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Provides examples for invalid cookie date string. |
| 66 | + * |
| 67 | + * @return array |
| 68 | + */ |
| 69 | + public function getInvalidCookieDateStrings() |
| 70 | + { |
| 71 | + return [ |
| 72 | + ['Flursday July 31st 2020, 08:49:37 GMT'], |
| 73 | + ['99 Jix 3038 48:86:72 ZMT'], |
| 74 | + ]; |
| 75 | + } |
| 76 | +} |
0 commit comments