Version 1
[yaffs-website] / vendor / jcalderonzumba / gastonjs / tests / unit / BrowserHeadersTest.php
1 <?php
2
3 namespace Zumba\GastonJS\Tests;
4
5 /**
6  * Class BrowserHeadersTest
7  * @package Zumba\GastonJS\Tests
8  */
9 class BrowserHeadersTest extends BrowserCommandsTestCase {
10
11   public function testHeadersEmpty() {
12     $this->assertCount(0, $this->browser->getHeaders());
13   }
14
15   public function testHeadersNotEmpty() {
16     $this->visitUrl($this->getTestPageBaseUrl() . "/static/basic.html");
17     $responseHeaders = $this->browser->responseHeaders();
18     $this->assertTrue(is_array($responseHeaders));
19     $this->assertCount(4, $responseHeaders);
20   }
21
22   public function testHeaderAddPermanent() {
23     $this->assertTrue($this->browser->addHeader(array("X-Permanent-Test" => "x_permanent_value"), true));
24     $this->visitUrl($this->getTestPageBaseUrl() . "/check-request-headers/");
25     $requestResponse = json_decode(strip_tags($this->browser->getBody()), true);
26     $this->assertEquals("x_permanent_value", $requestResponse["x-permanent-test"][0]);
27
28     //After reload, the header should still be there
29     $this->visitUrl($this->getTestPageBaseUrl() . "/check-request-headers/");
30     $requestResponse = json_decode(strip_tags($this->browser->getBody()), true);
31     $this->assertEquals("x_permanent_value", $requestResponse["x-permanent-test"][0]);
32   }
33
34   public function testHeaderAddTemp() {
35     $this->assertTrue($this->browser->addHeader(array("X-Permanent-Test" => "x_permanent_value")));
36     $this->visitUrl($this->getTestPageBaseUrl() . "/check-request-headers/");
37     $requestResponse = json_decode(strip_tags($this->browser->getBody()), true);
38     $this->assertEquals("x_permanent_value", $requestResponse["x-permanent-test"][0]);
39
40     //After the visit the header should not be present in the request
41     $this->visitUrl($this->getTestPageBaseUrl() . "/check-request-headers/");
42     $requestResponse = json_decode(strip_tags($this->browser->getBody()), true);
43     $this->assertArrayNotHasKey("x-permanent-test", $requestResponse);
44   }
45
46   public function testAddHeaders() {
47     $customHeaders = array("X-Header-One" => "one", "X-Header-Two" => "two");
48     $this->assertTrue($this->browser->addHeaders($customHeaders));
49     $this->visitUrl($this->getTestPageBaseUrl() . "/check-request-headers/");
50     $requestResponse = json_decode(strip_tags($this->browser->getBody()), true);
51     $this->assertArrayHasKey("x-header-one", $requestResponse);
52     $this->assertArrayHasKey("x-header-two", $requestResponse);
53
54     //now we will issue another header and the previous should be there too
55     $this->assertTrue($this->browser->addHeaders(array("X-Header-Three" => "three")));
56     $this->visitUrl($this->getTestPageBaseUrl() . "/check-request-headers/");
57     $requestResponse = json_decode(strip_tags($this->browser->getBody()), true);
58     $this->assertArrayHasKey("x-header-one", $requestResponse);
59     $this->assertArrayHasKey("x-header-two", $requestResponse);
60     $this->assertArrayHasKey("x-header-three", $requestResponse);
61   }
62
63
64   public function testSetHeaders() {
65     $customHeaders = array("X-Header-One" => "one", "X-Header-Two" => "two");
66     $this->assertTrue($this->browser->setHeaders($customHeaders));
67     $this->visitUrl($this->getTestPageBaseUrl() . "/check-request-headers/");
68     $requestResponse = json_decode(strip_tags($this->browser->getBody()), true);
69     $this->assertArrayHasKey("x-header-one", $requestResponse);
70     $this->assertArrayHasKey("x-header-two", $requestResponse);
71
72     //now we will issue another header and the previous should NOT be here
73     $this->assertTrue($this->browser->setHeaders(array("X-Header-Three" => "three")));
74     $this->visitUrl($this->getTestPageBaseUrl() . "/check-request-headers/");
75     $requestResponse = json_decode(strip_tags($this->browser->getBody()), true);
76     $this->assertArrayHasKey("x-header-three", $requestResponse);
77     $this->assertArrayNotHasKey("x-header-one", $requestResponse);
78     $this->assertArrayNotHasKey("x-header-two", $requestResponse);
79   }
80
81 }