Permalink
Browse files

Curl#output takes a host

  • Loading branch information...
1 parent 7831f75 commit e82c4de89ed269b0a4b8f1426cf74c05acf8b750 @oestrich oestrich committed Jan 25, 2012
Showing with 25 additions and 17 deletions.
  1. +13 −6 lib/rspec_api_documentation/curl.rb
  2. +12 −11 spec/curl_spec.rb
@@ -1,21 +1,24 @@
require 'active_support/core_ext/object/to_query'
module RspecApiDocumentation
- class Curl < Struct.new(:method, :host, :path, :data, :headers)
- def output
+ class Curl < Struct.new(:method, :path, :data, :headers)
+ attr_accessor :host
+
+ def output(config_host)
+ self.host = config_host
send(method.downcase)
end
def post
- "curl #{post_data} #{url} -X POST #{headers}"
+ "curl #{url} #{post_data} -X POST #{headers}"
end
def get
- "curl #{url}?#{data.to_query} -X GET #{headers}"
+ "curl #{url}#{get_data} -X GET #{headers}"
end
def put
- "curl #{post_data} #{url} -X PUT #{headers}"
+ "curl #{url} #{post_data} -X PUT #{headers}"
end
def delete
@@ -32,8 +35,12 @@ def headers
end.join(" ")
end
+ def get_data
+ "?#{data}" unless data.blank?
+ end
+
def post_data
- "-d \"" + data.to_query.split("&").join("\" -d \"") + "\""
+ "-d \"#{data}\""
end
private
View
@@ -1,34 +1,33 @@
require 'spec_helper'
describe RspecApiDocumentation::Curl do
let(:host) { "http://example.com" }
- let(:curl) { RspecApiDocumentation::Curl.new(method, host, path, data, headers) }
+ let(:curl) { RspecApiDocumentation::Curl.new(method, path, data, headers) }
- subject { curl.output }
+ subject { curl.output(host) }
describe "POST" do
let(:method) { "POST" }
let(:path) { "/orders" }
- let(:data) { { :order => { :size => "large", :type => "cart" } } }
+ let(:data) { "order%5Bsize%5D=large&order%5Btype%5D=cart" }
let(:headers) { {"HTTP_ACCEPT" => "application/json", "HTTP_X_HEADER" => "header"} }
it { should =~ /^curl/ }
it { should =~ /http:\/\/example\.com\/orders/ }
- it { should =~ /-d "order%5Bsize%5D=large"/ }
- it { should =~ /-d "order%5Btype%5D=cart"/ }
+ it { should =~ /-d "order%5Bsize%5D=large&order%5Btype%5D=cart"/ }
it { should =~ /-X POST/ }
it { should =~ /-H "Accept: application\/json"/ }
it { should =~ /-H "X-Header: header"/ }
it "should call post" do
curl.should_receive(:post)
- curl.output
+ curl.output(host)
end
end
describe "GET" do
let(:method) { "GET" }
let(:path) { "/orders" }
- let(:data) { { :size => "large" } }
+ let(:data) { "size=large" }
let(:headers) { {"HTTP_ACCEPT" => "application/json", "HTTP_X_HEADER" => "header"} }
it { should =~ /^curl/ }
@@ -39,14 +38,14 @@
it "should call get" do
curl.should_receive(:get)
- curl.output
+ curl.output(host)
end
end
describe "PUT" do
let(:method) { "PUT" }
let(:path) { "/orders/1" }
- let(:data) { { :size => "large" } }
+ let(:data) { "size=large" }
let(:headers) { {"HTTP_ACCEPT" => "application/json", "HTTP_X_HEADER" => "header"} }
it { should =~ /^curl/ }
@@ -58,7 +57,7 @@
it "should call put" do
curl.should_receive(:put)
- curl.output
+ curl.output(host)
end
end
@@ -71,10 +70,12 @@
it { should =~ /^curl/ }
it { should =~ /http:\/\/example\.com\/orders\/1/ }
it { should =~ /-X DELETE/ }
+ it { should =~ /-H "Accept: application\/json"/ }
+ it { should =~ /-H "X-Header: header"/ }
it "should call delete" do
curl.should_receive(:delete)
- curl.output
+ curl.output(host)
end
end
end

0 comments on commit e82c4de

Please sign in to comment.