Permalink
Browse files

Merge pull request #26 from zipmark/remove_rack_test

Remove rack test
  • Loading branch information...
oestrich committed Apr 24, 2012
2 parents 83a2b3d + 1dd6c2e commit 335dbe3f16cf73496730489066b2b0ed7fc5d751
View
@@ -7,7 +7,6 @@ PATH
i18n (>= 0.1.0)
json (>= 1.4.0)
mustache (>= 0.99.0)
- rack-test (>= 0.5.5)
rspec (>= 2.6.0)
webmock (>= 1.7.0)
@@ -35,44 +34,50 @@ GEM
ffi (~> 1.0.6)
coderay (1.0.7.rc1)
crack (0.3.1)
- cucumber (1.1.8)
+ cucumber (1.1.9)
builder (>= 2.1.2)
diff-lcs (>= 1.1.2)
- gherkin (~> 2.8.0)
+ gherkin (~> 2.9.0)
json (>= 1.4.6)
term-ansicolor (>= 1.0.6)
diff-lcs (1.1.3)
fakefs (0.4.0)
ffi (1.0.11)
- gherkin (2.8.0)
+ gherkin (2.9.3)
json (>= 1.4.6)
i18n (0.6.0)
- json (1.6.5)
- mime-types (1.17.2)
- multi_json (1.0.4)
+ json (1.6.6)
+ libwebsocket (0.1.3)
+ addressable
+ mime-types (1.18)
+ multi_json (1.2.0)
mustache (0.99.4)
- nokogiri (1.5.0)
- rack (1.3.5)
+ nokogiri (1.5.2)
+ rack (1.4.1)
+ rack-protection (1.2.0)
+ rack
rack-test (0.6.1)
rack (>= 1.0)
rake (0.9.2.2)
- rspec (2.8.0)
- rspec-core (~> 2.8.0)
- rspec-expectations (~> 2.8.0)
- rspec-mocks (~> 2.8.0)
- rspec-core (2.8.0)
- rspec-expectations (2.8.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.8.0)
- rubyzip (0.9.6.1)
- selenium-webdriver (2.19.0)
+ rspec (2.9.0)
+ rspec-core (~> 2.9.0)
+ rspec-expectations (~> 2.9.0)
+ rspec-mocks (~> 2.9.0)
+ rspec-core (2.9.0)
+ rspec-expectations (2.9.1)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.9.0)
+ rubyzip (0.9.7)
+ selenium-webdriver (2.21.0)
childprocess (>= 0.2.5)
- ffi (~> 1.0.9)
- multi_json (~> 1.0.4)
+ ffi (~> 1.0)
+ libwebsocket (~> 0.1.3)
+ multi_json (~> 1.0)
rubyzip
- sinatra (1.2.6)
- rack (~> 1.1)
- tilt (>= 1.2.2, < 2.0)
+ sinatra (1.3.2)
+ rack (~> 1.3, >= 1.3.6)
+ rack-protection (~> 1.2)
+ tilt (~> 1.3, >= 1.3.3)
term-ansicolor (1.0.7)
tilt (1.3.3)
webmock (1.8.6)
View
@@ -1,10 +1,13 @@
Feature: Document callbacks
Background:
- Given a file named "app.rb" with:
+ Given a file named "app_spec.rb" with:
"""
- class App
- def self.call(env)
+ require "rspec_api_documentation"
+ require "rspec_api_documentation/dsl"
+
+ RspecApiDocumentation.configure do |config|
+ config.app = lambda do
uri = URI.parse("http://example.net/callback")
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Post.new(uri.path)
@@ -16,32 +19,26 @@ Feature: Document callbacks
[200, {}, []]
end
end
- """
- And a file named "app_spec.rb" with:
- """
- require "rspec_api_documentation"
- require "rspec_api_documentation/dsl"
-
- RspecApiDocumentation.configure do |config|
- config.app = App
- end
resource "Interesting Thing" do
callback "/interesting_thing" do
let(:callback_url) { "http://example.net/callback" }
trigger_callback do
- post "/interesting_thing"
+ app.call
end
example "Receiving a callback when interesting things happen" do
do_callback
+ request_method.should eq("POST")
+ request_headers["Content-Type"].should eq("application/json")
+ request_headers["User-Agent"].should eq("InterestingThingApp")
+ request_body.should eq('{"message":"Something interesting happened!"}')
end
end
end
"""
-
- When I run `rspec app_spec.rb --require ./app.rb --format RspecApiDocumentation::ApiFormatter`
+ When I run `rspec app_spec.rb --format RspecApiDocumentation::ApiFormatter`
Scenario: Output helpful progress to the console
Then the output should contain:
@@ -29,6 +29,7 @@ Feature: Generate HTML documentation from test examples
example "Greeting your favorite gem" do
do_request :target => "rspec_api_documentation"
+ response_headers["Content-Type"].should eq("application/json")
status.should eq(200)
response_body.should eq('{"hello":"rspec_api_documentation"}')
end
@@ -68,9 +69,7 @@ Feature: Generate HTML documentation from test examples
| Host | example.org |
| Cookie | |
And I should see the following query parameters:
- """
- target: rspec_api_documentation
- """
+ | target | rspec_api_documentation |
Scenario: Example HTML documentation includes the response information
When I open the index
@@ -27,8 +27,11 @@
page.should have_css(".request.route", :text => route)
end
-Then /^I should see the following query parameters:$/ do |query_parameters|
- page.should have_css("pre.request.query_parameters"), :text => query_parameters
+Then /^I should see the following query parameters:$/ do |table|
+ text = page.find("pre.request.query_parameters").text
+ actual = text.split("\n")
+ expected = table.raw.map { |row| row.join(": ") }
+ actual.should =~ expected
end
Then /^I should see the response status is "([^"]*)"$/ do |status|
@@ -16,10 +16,13 @@ module RspecApiDocumentation
autoload :Example
autoload :ExampleGroup
autoload :Index
- autoload :TestClient
+ autoload :ClientBase
+ autoload :Headers
+ autoload :Syntax
end
autoload :DSL
+ autoload :RackTestClient
autoload :TestServer
autoload :HtmlWriter
autoload :JsonWriter
@@ -0,0 +1,71 @@
+module RspecApiDocumentation
+ class ClientBase < Struct.new(:context, :options)
+ include Headers
+ include Syntax
+
+ delegate :example, :app, :to => :context
+ delegate :metadata, :to => :example
+
+ def get(*args)
+ process :get, *args
+ end
+
+ def post(*args)
+ process :post, *args
+ end
+
+ def put(*args)
+ process :put, *args
+ end
+
+ def delete(*args)
+ process :delete, *args
+ end
+
+ private
+
+ def process(method, path, params = {})
+ do_request(method, path, params)
+ document_example(method.to_s.upcase, path, params)
+ end
+
+ def document_example(method, path, params)
+ return unless metadata[:document]
+
+ input = last_request.env["rack.input"]
+ input.rewind
+ request_body = input.read
+
+ request_metadata = {}
+
+ request_metadata[:request_method] = method
+ request_metadata[:request_path] = path
+ request_metadata[:request_body] = highlight_syntax(request_body, content_type, true)
+ request_metadata[:request_headers] = format_headers(request_headers)
+ request_metadata[:request_query_parameters] = format_query_hash(query_hash)
+ request_metadata[:response_status] = status
+ request_metadata[:response_status_text] = Rack::Utils::HTTP_STATUS_CODES[status]
+ request_metadata[:response_body] = highlight_syntax(response_body, response_headers['Content-Type'])
+ request_metadata[:response_headers] = format_headers(response_headers)
+ request_metadata[:curl] = Curl.new(method, path, request_body, request_headers)
+
+ metadata[:requests] ||= []
+ metadata[:requests] << request_metadata
+ end
+
+ def query_hash
+ strings = query_string.split("&")
+ arrays = strings.map do |segment|
+ segment.split("=")
+ end
+ Hash[arrays]
+ end
+
+ def format_query_hash(query_hash)
+ return if query_hash.blank?
+ query_hash.map do |key, value|
+ "#{key}: #{CGI.unescape(value)}"
+ end.join("\n")
+ end
+ end
+end
@@ -6,6 +6,9 @@ module RspecApiDocumentation
module DSL
extend ActiveSupport::Concern
+ delegate :response_headers, :status, :response_body, :to => :client
+ delegate :request_method, :request_headers, :request_body, :to => :destination
+
module ClassMethods
def self.define_action(method)
define_method method do |*args, &block|
@@ -89,7 +92,7 @@ def parameter_keys
end
def client
- @client ||= TestClient.new(self)
+ @client ||= RackTestClient.new(self)
end
def destination
@@ -173,14 +176,6 @@ def explanation(text)
example.metadata[:explanation] = text
end
- def status
- client.last_response.status
- end
-
- def response_body
- client.last_response.body
- end
-
private
def extra_params
return {} if @extra_params.nil?
@@ -220,5 +215,4 @@ def self.resource(*args, &block)
end
RSpec.configuration.include RspecApiDocumentation::DSL, :api_docs_dsl => true
-RSpec.configuration.include Rack::Test::Methods, :api_docs_dsl => true
RSpec.configuration.backtrace_clean_patterns << %r{lib/rspec_api_documentation/dsl\.rb}
@@ -0,0 +1,23 @@
+module RspecApiDocumentation
+ module Headers
+ private
+
+ def env_to_headers(env)
+ headers = {}
+ env.each do |key, value|
+ # HTTP_ACCEPT_CHARSET => Accept-Charset
+ if key =~ /^(HTTP_|CONTENT_TYPE)/
+ header = key.gsub(/^HTTP_/, '').titleize.split.join("-")
+ headers[header] = value
+ end
+ end
+ headers
+ end
+
+ def format_headers(headers)
+ headers.map do |key, value|
+ "#{key}: #{value}"
+ end.join("\n")
+ end
+ end
+end
@@ -0,0 +1,59 @@
+require "coderay"
+
+module RspecApiDocumentation
+ class RackTestClient < ClientBase
+
+ delegate :last_request, :last_response, :to => :rack_test_session
+ private :last_request, :last_response
+
+ def request_headers
+ env_to_headers(last_request.env)
+ end
+
+ def response_headers
+ last_response.headers
+ end
+
+ def query_string
+ last_request.env["QUERY_STRING"]
+ end
+
+ def status
+ last_response.status
+ end
+
+ def response_body
+ last_response.body
+ end
+
+ def content_type
+ last_request.content_type
+ end
+
+ protected
+
+ def do_request(method, path, params)
+ rack_test_session.send(method, path, params, headers(method, path, params))
+ end
+
+ private
+
+ def rack_test_session
+ @rack_test_session ||= Struct.new(:app) do
+ begin
+ include Rack::Test::Methods
+ rescue LoadError
+ raise "#{self.class.name} requires Rack::Test >= 0.5.5. Please add it to your test dependencies."
+ end
+ end.new(app)
+ end
+
+ def headers(method, path, params)
+ if options && options[:headers]
+ options[:headers]
+ else
+ {}
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 335dbe3

Please sign in to comment.