-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add curl_command as serverspec extension
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
lib/voxpupuli/acceptance/serverspec_extensions/curl_command.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |