Andrew Walker
Andrew Walker

Reputation: 17

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?

Upvotes: 0

Views: 298

Answers (2)

Matt Thornback
Matt Thornback

Reputation: 46

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

Upvotes: 3

minamijoyo
minamijoyo

Reputation: 3445

A redirect severspec using curl command is like this:

redirect_url = "..."
describe command("curl -I http://example.com/folder/file.json") do
  its(:stdout) { should match(%r|HTTP/1.1 301 Moved Permanently|) }
  its(:stdout) { should match(%r|Location: #{Regexp.escape(redirect_url)}|) }
end

Upvotes: 1

Related Questions