Skip to content

Instantly share code, notes, and snippets.

@hemalvarambhia
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save hemalvarambhia/d12883fe7c10546e1525 to your computer and use it in GitHub Desktop.

Select an option

Save hemalvarambhia/d12883fe7c10546e1525 to your computer and use it in GitHub Desktop.
The nginx Rspec matcher for serverspec
module NginxMatchers
RSpec::Matchers.define :permit do |ip_addresses|
match do |content|
escaped_url = Regexp.escape @url
ip_addresses.all?{ |ip_address|
content=~/location #{escaped_url} \{[^}]+allow #{ip_address};/m
}
end
chain :access_to do |url|
@url = url
end
failure_message_for_should do |content|
escaped_url = Regexp.escape @url
missing_ip_addresses = ip_addresses.select{ |ip_address|
(content=~/location #{escaped_url} \{[^}]+allow #{ip_address};/m).nil?
}
"expected Nginx to permit #{missing_ip_addresses.join(", ")} access to '#{@url}'"
end
end
RSpec::Matchers.define :upstream_to do |server|
match do |content|
escaped_server = Regexp.escape(server)
!(content=~/upstream #{@upstreamer} \{[^}]+server #{escaped_server};/m).nil?
end
chain :through do |upstreamer|
@upstreamer = upstreamer
end
failure_message_for_should do |content|
"expected Nginx to upstream to '#{server}' via '#{@upstreamer}'"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment