简体   繁体   中英

guzzle `http_errors` changes error from 500 to 404

i have a function im testing which suppose to return error 500 but after adding 'http_errors' => 'false' to the put definition, the returned error changes from 500 to 404. this is my function:

public function testApiAd_updateWithIllegalGroupId($adId) 
{
    $client = new Client(['base_uri' => self::$base_url]);
    try {
        $response = $client->put(self::$path.$adId, ['form_params' => [
          'name' => 'bellow content - guzzle testing',
          'description' => 'guzzle testing ad - demo',
          'group_id' => '999999999',
          ]]);
    } catch (Guzzle\Http\Exception\BadResponseException $e) {
        //Here i want to compare received error to 500
    }
}

right now this function will return server error: 500 but it also stops the class from executing rest of the tests and i can't assert it. how can i use the guzzle getStatusCode() in my function while getting error 500 and not 404 as i mentioned above

The BadResponseException contains the original Request and the Response object. So you can, the catch block the following assertion:

} catch (Guzzle\Http\Exception\BadResponseException $e) {
        //Here i want to compare received error to 500
        $responseCode = $e->getResponse()->getStatusCode();
        $this->assertEquals(500, $responseCode, "Server Error");
    }

Further info in the doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM