7
7

More than 5 years have passed since last update.

サーバが特定UAから接続可能であることをテストする

Posted at

serverspecを使うと簡単に特定ポートをlistenしていることを確認できるが、このポートに特定のUAでアクセスできることをテストするmatcherを書いた。

RSpec::Matchers.define :be_reachable_by do |ua|
  match do |port|
    if /(\d+)/ =~ port.to_s
      b = backend.backend_for('ssh')
      b.run_command("curl -A #{ua} localhost:#{Regexp.last_match(1)}").exit_status == 0
    else
      false
    end
  end
end

例えばELBのヘルスチェッカーから接続可能であることをテストする場合。

describe port(80) do
  it { should be_listening }
  it { should be_reachable_by 'ELB-HealthChecker/1.0' }
  it { should_not be_reachable_by 'BAD-USER-AGENT' }
end
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7