Search code examples
nginxserverspec

Testing redirects using serverspec


I'm looking to create a test in serverspec for an nginx rewrite call like the one below (it re-directs requests for the ^/folder/file.json URL to the @redirect_url variable):

rewrite ^/folder/file.json <%= @redirect_url %> permanent;

For a Chef cookbook that I'm updating. Unfortunately, from what I have been able to find on the internet, it has either underlined my complete lack of understanding of nginx and serverspec terminology or the fact that you cannot do it. Is there anyone able to help?


Solution

  • You can check the response code directly using Ruby's Net::HTTP

    describe Net::HTTP.get_response(URI('http://localhost:port/path')) do
      its(:code) { should eq '301' }
      its(:msg) { should match 'Moved Permanently' }
    end