I'm trying to force a request to my PHP API (based on a home-built framework with nginx) to result in a 502 bad gateway error so that I can test that it is handled gracefully in the application that calls the API. Is there an easy way to have the API cause a 502 error so I can test it?
Other errors (404, 500) are easy enough to produce and test, but I have no idea how to test a 502. If I understand correctly, it can be produced by sending an incomplete/invalid response but how do I trigger that?
Or would it be easier to temporarily configure nginx
to produce it?
Update
Thanks for suggesting http_response_code
in the comments, but it doesn't seem to be doing what I want. The one time the API actually resulted in a real 502 error, it broke the application that called it. I suspect setting the response code like this doesn't seem to cause a true 502 response since the application isn't acting the same way. I tried the following:
http_response_code(502);
http_response_code(502); die();
header('bad gateway', true, 502); die();
header('bad gateway', true, 502);