Skip to content

Commit

Permalink
add curl_command as serverspec extension
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Jan 14, 2025
1 parent c142404 commit 446435e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/voxpupuli/acceptance/serverspec_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'serverspec'
require_relative 'serverspec_extensions/curl_command'

module Serverspec
module Helper
module Type
def curl_command(*args)
Voxpupuli::Acceptance::ServerspecExtensions::CurlCommand.new(*args)
end
end
end
end
56 changes: 56 additions & 0 deletions lib/voxpupuli/acceptance/serverspec_extensions/curl_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# written by https://github.com/ekohl
# https://github.com/mizzy/serverspec/pull/611 was rejected so adding it here.

require 'serverspec'

module Voxpupuli
module Acceptance
module ServerspecExtensions
class CurlCommand < Serverspec::Type::Command
def response_code
m = %r{Response-Code: (?<code>\d+)}.match(stderr)
return 0 unless m

m[:code].to_i
end

def body
command_result.stdout
end

def body_as_json
MultiJson.load(body)
end

private

def curl_command
command = "curl --silent --write-out '%{stderr}Response-Code: %{response_code}\\n' '#{@name}'"

@options.each do |option, value|
case option
when :cacert, :cert, :key
command += " --#{option} '#{value}'"
when :headers
value.each do |header, header_value|
command += if header_value
" --header '#{header}: #{header_value}'"
else
" --header '#{header};'"
end
end
else
raise "Unknown option #{option} (value: #{value})"
end
end

command
end

def command_result
@command_result ||= @runner.run_command(curl_command)
end
end
end
end
end

0 comments on commit 446435e

Please sign in to comment.