-2

I have fetched multiple URL's using grabMultiple() method. I want to check its response to check whether it is broken(i.e.404) or not.

Can I use HTTP Response in Codeception ? If yes what is the syntax ?

Naktibalda
  • 13,705
  • 5
  • 35
  • 51

2 Answers2

3

Use seeResponseCodeIs method.

$links = $I->grabMultiple('div.test-info a', 'href');
foreach ($links as $link) {
  $I->amOnPage($link);
  $I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
}
Naktibalda
  • 13,705
  • 5
  • 35
  • 51
0

This worked for me -

function check_url($link){

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);

$code = end($matches[1]);

if($code==404) 
{
    echo "Bad url";
}
else{
    echo "Good url";
}

}