Error "unexpected status code" not reported when error "response does not match regex" occur
Command line example:
$ httpunit https://www.google.com/foo 216.58.209.132 200 asdf
1 of 1 done in 199.789431ms
==== https://www.google.com/foo: https://www.google.com/foo IP=216.58.209.132 (200 R)
ERROR: response does not match regex [asdf]: "<!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n <title>Error 404 (Not Found)!!1</title>\n <style>\n *{margin:0;padding:0}html,code{font:15px/22px arial,sa"
TOML file example:
$ cat google.toml
[[plan]]
code = 200
regex = "asdf"
label = "google"
url = "https://www.google.com/foo"
$ httpunit -toml google.toml
==== google: https://www.google.com/foo DNS=216.58.209.132 (200 R)
ERROR: response does not match regex [asdf]: "<!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n <title>Error 404 (Not Found)!!1</title>\n <style>\n *{margin:0;padding:0}html,code{font:15px/22px arial,sa"
==== google: https://www.google.com/foo DNS=2a00:1450:400f:804::2004 (200 R)
ERROR: dial tcp [2a00:1450:400f:804::2004]:443: connect: no route to host
"unexpected status code" error is reported when there's no regex check:
$ httpunit https://www.google.com/foo
2 of 2 done in 197.267064ms
==== https://www.google.com/foo: https://www.google.com/foo DNS=216.58.209.132 (200)
ERROR: unexpected status code: 404
==== https://www.google.com/foo: https://www.google.com/foo DNS=2a00:1450:400f:804::2004 (200)
ERROR: dial tcp [2a00:1450:400f:804::2004]:443: connect: no route to host
"unexpected status code" error is also reported when the regex check doesn't fail:
$ httpunit https://www.google.com/foo 216.58.209.132 200 Error
1 of 1 done in 211.893315ms
==== https://www.google.com/foo: https://www.google.com/foo IP=216.58.209.132 (200 R)
ERROR: unexpected status code: 404
(So this case maybe isn't that common to have)
What would be the expected behavior in these cases?
Report both cases? I guess it would look like
$ httpunit https://www.google.com/foo 216.58.209.132 200 asdf
1 of 1 done in 199.789431ms
==== https://www.google.com/foo: https://www.google.com/foo IP=216.58.209.132 (200 R)
ERROR: response does not match regex [asdf]: "<!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n <title>Error 404 (Not Found)!!1</title>\n <style>\n *{margin:0;padding:0}html,code{font:15px/22px arial,sa"
==== https://www.google.com/foo: https://www.google.com/foo IP=216.58.209.132 (200 R)
ERROR: unexpected status code: 404
The tests are done in sequence and only the first error is reported. This is by design. Usually there is no need to check the regex if, for example, a 404 was received.
Is there a use-case you are trying to achieve that is blocked by this design?
I had the need to do thousands of requests to an API, and check the output on 200 responses. When the regex didn't match, I was not able understand if it was a correct negative match (200 response) or incorrect (non 200 response). I used httpunit because it makes HTTP requests really fast (thank you!).
I wrote code to generate httpunit configs, that code also had to resolve the DNS hostname and put a single IP address into the generated config, as httpunit always make requests to every IP from DNS. I can understand that you didn't have this use-case in mind when building the tool. I do feel like I'm abusing httpunit a bit. :-)
Ah, that is a use case we haven't experienced before! I can see how that would be useful.
Would you be interested in writing a PR to add this feature? Perhaps a flag that enables enhanced reporting>
You might want to add another flag that enables "only use the first A record returned in DNS"
We'd love to receive a PR for either of these features.
My need is not that pressing (my task was a one-off thing), so to be honest, it is unlikely that I will be writing an PR for this right now. I'll let you know if that changes, or if I find some spare time I don't know what to do with. :-)