WordPress index.php seems to perform unwanted redirect 301

It sounds like both Home URL and Site URL for your blog are set to http://test.blog, while your server accepts both http://test.blog and http://www.test.blog.

If you access the latter, WordPress will redirect you because of the mismatch. You might be able to set the Home URL and Site URL dynamically in your wp-config.php like this:

define( 'WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] );
define( 'WP_HOME',    'https://' . $_SERVER['HTTP_HOST'] );

This way, there shouldn’t be a redirect. But this has of course complications for things like URLs in post content. WordPress does not use relative URLs for that. That’s why it’s generally better to settle one 1 domain. But there are also other reasons like SEO (duplicate content). Your web server should either redirect www.test.blog to test.blog or vice-versa, it’s the easiest way to solve this. The site will obviously still be available via either host, it’s just that you have to settle for one.

Note: $_SERVER['HTTP_HOST'] is not set on the command line, so if you want to use WP-CLI you’d have to set that variable.

Please let me know if that answers your question or if I misunderstood you.