如何模拟不同状态码的HTTP请求?

logger

摸鱼经验|2023-9-28|最后更新: 2023-9-28|
slug
http_stat_code
status
Published
type
Post
date
Sep 28, 2023
summary
tags
技术
category
摸鱼经验
password
Property
Sep 28, 2023 09:06 AM
icon
偶尔会遇到这种场景,业务逻辑测试时,需要访问一个HTTP请求,根据请求返回的不同的状态码进行不同的处理。
如,2xx时,需要安装成功做相应的处理动。
但是,我们的业务逻辑可能已经具备了,如何模拟一个可以具备不同状态码的HTTP请求呢?
当然,自己code编写并启动一个HTTP Server是可以的。
不过,我们也可以用一些现成的工具。
比如:http://httpstat.us/,在这个URL后面加上不同的状态码,就可以获取到不同的HTTP状态码返回。
$ curl -I http://httpstat.us/200 HTTP/1.1 200 OK Content-Length: 6 Content-Type: text/plain Date: Thu, 28 Sep 2023 07:54:25 GMT Server: Kestrel Set-Cookie: ARRAffinity=30020627b4609efb836376960caeb050f66bafde1d41727872414db781c5e955;Path=/;HttpOnly;Domain=httpstat.us Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53 $ curl -I http://httpstat.us/201 HTTP/1.1 201 Created Content-Length: 11 Content-Type: text/plain Date: Thu, 28 Sep 2023 07:55:07 GMT Server: Kestrel Set-Cookie: ARRAffinity=30020627b4609efb836376960caeb050f66bafde1d41727872414db781c5e955;Path=/;HttpOnly;Domain=httpstat.us Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53 $ curl -I http://httpstat.us/404 HTTP/1.1 404 Not Found Content-Length: 13 Content-Type: text/plain Date: Thu, 28 Sep 2023 07:55:25 GMT Server: Kestrel Set-Cookie: ARRAffinity=30020627b4609efb836376960caeb050f66bafde1d41727872414db781c5e955;Path=/;HttpOnly;Domain=httpstat.us Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53 $ curl -I http://httpstat.us/502 HTTP/1.1 502 Bad Gateway Content-Length: 15 Content-Type: text/plain Date: Thu, 28 Sep 2023 07:55:40 GMT Server: Kestrel Set-Cookie: ARRAffinity=30020627b4609efb836376960caeb050f66bafde1d41727872414db781c5e955;Path=/;HttpOnly;Domain=httpstat.us Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53 $ curl -I http://httpstat.us/501 HTTP/1.1 501 Not Implemented Content-Length: 19 Content-Type: text/plain Date: Thu, 28 Sep 2023 07:55:50 GMT Server: Kestrel Set-Cookie: ARRAffinity=30020627b4609efb836376960caeb050f66bafde1d41727872414db781c5e955;Path=/;HttpOnly;Domain=httpstat.us Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53
 
  • Waline
  • Giscus
  • Cusdis