/** * @dataProvider settingHeadersDataProvider */ public function testOperatingOnHeaders($replace, $headerValues) { $headersNum = count($headerValues); foreach ($headerValues as $value) { $this->object->setHeader(self::TEST_HEADER_NAME, $value, $replace); } $header = $this->object->getHeader(self::TEST_HEADER_NAME); if ($replace) { $this->assertEquals(1, count($this->object->getHeaders())); $this->assertEquals(1, count($header)); $this->assertEquals(self::TEST_HEADER_VALUE2, $header[0]['value']); } else { $this->assertEquals($headersNum, count($this->object->getHeaders())); $this->assertEquals($headersNum, count($header)); $i = 0; foreach ($headerValues as $value) { $this->assertEquals($value, $header[$i]['value']); $i++; } } // there is one additional header to be send, content-type $headersNum = $replace ? 2 : $headersNum + 1; $this->object->expects($this->exactly($headersNum))->method('sendHeader'); $this->object->sendHeaders(); }
public function testShouldProperlySetHeadersWithPreExistingValues() { $dummyResponse = new WikiaResponse("tmp"); $dummyResponse->setHeader(CrossOriginResourceSharingHeaderHelper::ALLOW_ORIGIN_HEADER_NAME, "t,m,p"); $cors = new CrossOriginResourceSharingHeaderHelper(); $cors->setAllowOrigin(["a", "b", "t"])->setHeaders($dummyResponse); $headers = $dummyResponse->getHeader(CrossOriginResourceSharingHeaderHelper::ALLOW_ORIGIN_HEADER_NAME); $this->assertEquals($headers[0]['value'], 'a,b,t,t,m,p'); }
/** * This method sets all configured CORS related headers * * @param WikiaResponse $response response object to set headers to * @param bool $mergeExisting */ public function setHeaders(WikiaResponse $response, $mergeExisting = true) { foreach ($this->allowValues as $headerName => $values) { if (!empty($values)) { $valuesToSet = $values; $headers = $response->getHeader($headerName); if (!empty($headers) && $mergeExisting) { $response->removeHeader($headerName); foreach ($headers as $header) { $valuesToSet = array_merge($valuesToSet, explode(self::HEADER_DELIMETER, $header['value'])); } } $response->setHeader($headerName, implode(self::HEADER_DELIMETER, $valuesToSet)); } } return $this; }