Skip to content
Browse files

Merge pull request #26 from zipmark/remove_rack_test

Remove rack test
  • Loading branch information...
2 parents 83a2b3d + 1dd6c2e commit 335dbe3f16cf73496730489066b2b0ed7fc5d751 @oestrich oestrich committed
View
53 Gemfile.lock
@@ -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
27 features/callbacks.feature
@@ -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:
View
5 features/html_documentation.feature
@@ -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
View
7 features/step_definitions/html_steps.rb
@@ -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|
View
5 lib/rspec_api_documentation.rb
@@ -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
View
71 lib/rspec_api_documentation/client_base.rb
@@ -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
View
14 lib/rspec_api_documentation/dsl.rb
@@ -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}
View
23 lib/rspec_api_documentation/headers.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
View
59 lib/rspec_api_documentation/rack_test_client.rb
@@ -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
View
33 lib/rspec_api_documentation/syntax.rb
@@ -0,0 +1,33 @@
+require "coderay"
+
+module RspecApiDocumentation
+ module Syntax
+ private
+
+ def highlight_syntax(body, content_type, is_query_string = false)
+ return if body.blank?
+ begin
+ case content_type
+ when /json/
+ CodeRay.scan(JSON.pretty_generate(JSON.parse(body)), :json).div
+ when /html/
+ CodeRay.scan(body, :html).div
+ when /javascript/
+ CodeRay.scan(body, :java_script).div
+ when /xml/
+ CodeRay.scan(body, :xml).div
+ else
+ body = prettify_request_body(body) if is_query_string
+ "<pre>#{body}</pre>"
+ end
+ rescue Exception => e
+ "<pre>#{e.inspect}</pre>"
+ end
+ end
+
+ def prettify_request_body(string)
+ return if string.blank?
+ CGI.unescape(string.split("&").join("\n"))
+ end
+ end
+end
View
129 lib/rspec_api_documentation/test_client.rb
@@ -1,129 +0,0 @@
-require "coderay"
-
-module RspecApiDocumentation
- class TestClient < Struct.new(:session, :options)
- attr_accessor :user
-
- delegate :example, :last_response, :last_request, :to => :session
- 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
-
- def sign_in(user)
- @user = user
- end
-
- def last_headers
- headers = last_request.env.select do |k, v|
- k =~ /^(HTTP_|CONTENT_TYPE)/
- end
- Hash[headers]
- end
-
- def last_query_string
- last_request.env["QUERY_STRING"]
- end
-
- def last_query_hash
- strings = last_query_string.split("&")
- arrays = strings.map do |segment|
- segment.split("=")
- end
- Hash[arrays]
- end
-
- def headers(method, action, params)
- if options && options[:headers]
- options[:headers]
- else
- {}
- end
- end
-
- private
- def process(method, action, params = {})
- session.send(method, action, params, headers(method, action, params))
-
- document_example(method, action, params)
- end
-
- def document_example(method, action, params)
- return unless metadata[:document]
-
- input = last_request.env["rack.input"]
- input.rewind
- request_body = input.read
-
- request_metadata = {}
-
- request_metadata[:request_method] = method.to_s.upcase
- request_metadata[:request_path] = action
- request_metadata[:request_body] = highlight_syntax(request_body, last_request.content_type, true)
- request_metadata[:request_headers] = format_headers(last_headers)
- request_metadata[:request_query_parameters] = format_query_hash(last_query_hash)
- request_metadata[:response_status] = last_response.status
- request_metadata[:response_status_text] = Rack::Utils::HTTP_STATUS_CODES[last_response.status]
- request_metadata[:response_body] = highlight_syntax(last_response.body, last_response.headers['Content-Type'])
- request_metadata[:response_headers] = format_headers(last_response.headers)
- request_metadata[:curl] = Curl.new(method.to_s, action, request_body, last_headers)
-
- metadata[:requests] ||= []
- metadata[:requests] << request_metadata
- end
-
- def format_headers(headers)
- headers.map do |key, value|
- # HTTP_ACCEPT_CHARSET => Accept-Charset
- formatted_key = key.gsub(/^HTTP_/, '').titleize.split.join("-")
- "#{formatted_key}: #{value}"
- end.join("\n")
- 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
-
- def highlight_syntax(body, content_type, is_query_string = false)
- return if body.blank?
- begin
- case content_type
- when /json/
- CodeRay.scan(JSON.pretty_generate(JSON.parse(body)), :json).div
- when /html/
- CodeRay.scan(body, :html).div
- when /javascript/
- CodeRay.scan(body, :java_script).div
- when /xml/
- CodeRay.scan(body, :xml).div
- else
- body = prettify_request_body(body) if is_query_string
- "<pre>#{body}</pre>"
- end
- rescue
- "<pre>#{body}</pre>"
- end
- end
-
- def prettify_request_body(string)
- return if string.blank?
- CGI.unescape(string.split("&").join("\n"))
- end
- end
-end
View
44 lib/rspec_api_documentation/test_server.rb
@@ -1,44 +1,32 @@
module RspecApiDocumentation
- class TestServer < Struct.new(:session)
- delegate :example, :last_request, :last_response, :to => :session
+ class TestServer < Struct.new(:context)
+ include Headers
+ include Syntax
+
+ delegate :example, :to => :context
delegate :metadata, :to => :example
+ attr_reader :request_method, :request_headers, :request_body
+
def call(env)
- env["rack.input"].rewind
+ input = env["rack.input"]
+ input.rewind
+
+ @request_method = env["REQUEST_METHOD"]
+ @request_headers = env_to_headers(env)
+ @request_body = input.read
request_metadata = {}
- request_metadata[:request_method] = env["REQUEST_METHOD"]
+ request_metadata[:request_method] = request_method
request_metadata[:request_path] = env["PATH_INFO"]
- request_metadata[:request_body] = prettify_json(env["rack.input"].read)
- request_metadata[:request_headers] = headers(env)
+ request_metadata[:request_body] = highlight_syntax(request_body, request_headers["Content-Type"], true)
+ request_metadata[:request_headers] = format_headers(@request_headers)
metadata[:requests] ||= []
metadata[:requests] << request_metadata
return [200, {}, [""]]
end
-
- private
-
- def headers(env)
- env.
- select do |k, v|
- k =~ /^(HTTP_|CONTENT_TYPE)/
- end.
- map do |key, value|
- # HTTP_ACCEPT_CHARSET => Accept-Charset
- formatted_key = key.gsub(/^HTTP_/, '').titleize.split.join("-")
- "#{formatted_key}: #{value}"
- end.join("\n")
- end
-
- def prettify_json(json)
- begin
- JSON.pretty_generate(JSON.parse(json))
- rescue
- nil
- end
- end
end
end
View
1 rspec_api_documentation.gemspec
@@ -17,7 +17,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "rspec", ">= 2.6.0"
s.add_runtime_dependency "activesupport", ">= 3.0.0"
s.add_runtime_dependency "i18n", ">= 0.1.0"
- s.add_runtime_dependency "rack-test", ">= 0.5.5"
s.add_runtime_dependency "mustache", ">= 0.99.0"
s.add_runtime_dependency "webmock", ">= 1.7.0"
s.add_runtime_dependency "json", ">= 1.4.0"
View
11 spec/dsl_spec.rb
@@ -5,15 +5,10 @@
describe "Non-api documentation specs" do
it "should not be polluted by the rspec api dsl" do
example.example_group.should_not include(RspecApiDocumentation::DSL)
- example.example_group.should_not include(Rack::Test::Methods)
end
end
resource "Order" do
- it "should include Rack::Test::Methods" do
- example.example_group.should include(Rack::Test::Methods)
- end
-
describe "example metadata" do
subject { example.metadata }
@@ -23,7 +18,7 @@
describe "example context" do
it "should provide a client" do
- client.should be_a(RspecApiDocumentation::TestClient)
+ client.should be_a(RspecApiDocumentation::RackTestClient)
end
it "should return the same client every time" do
@@ -439,12 +434,12 @@
context "last_response helpers" do
put "/orders" do
it "status" do
- self.stub!(:last_response).and_return(stub(:status => 200))
+ client.stub!(:last_response).and_return(stub(:status => 200))
status.should == 200
end
it "response_body" do
- self.stub!(:last_response).and_return(stub(:body => "the body"))
+ client.stub!(:last_response).and_return(stub(:body => "the body"))
response_body.should == "the body"
end
end
View
85 spec/test_client_spec.rb → spec/rack_test_client_spec.rb
@@ -29,41 +29,25 @@ class StubApp < Sinatra::Base
end
end
-describe RspecApiDocumentation::TestClient do
- include Rack::Test::Methods
-
- let(:app) { StubApp }
- let(:test_client) { RspecApiDocumentation::TestClient.new(self) }
+describe RspecApiDocumentation::RackTestClient do
+ let(:context) { stub(:app => StubApp, :example => example) }
+ let(:test_client) { RspecApiDocumentation::RackTestClient.new(context, {}) }
subject { test_client }
- it { should be_a(RspecApiDocumentation::TestClient) }
+ it { should be_a(RspecApiDocumentation::RackTestClient) }
- its(:session) { should equal(self) }
+ its(:context) { should equal(context) }
its(:example) { should equal(example) }
its(:metadata) { should equal(example.metadata) }
- describe "#last_response" do
- before do
- test_client.get "/"
- end
-
- it "should expose the last request" do
- test_client.last_request.should equal(last_request)
- end
-
- it "should expose the last response" do
- test_client.last_response.should equal(last_response)
- end
- end
-
describe "xml data", :document => true do
before do
test_client.get "/xml"
end
it "should handle xml data" do
- test_client.last_response.headers["Content-Type"].should =~ /application\/xml/
+ test_client.response_headers["Content-Type"].should =~ /application\/xml/
end
it "should log the request" do
@@ -71,74 +55,67 @@ class StubApp < Sinatra::Base
end
end
- describe "#last_query_string" do
+ describe "#query_string" do
before do
test_client.get "/?query_string=true"
end
it 'should contain the query_string' do
- test_client.last_query_string.should == "query_string=true"
+ test_client.query_string.should == "query_string=true"
end
end
- describe "#last_query_hash" do
+ describe "#request_headers" do
before do
- test_client.get "/?query_hash=true"
- end
-
- it 'should contain the query_hash' do
- test_client.last_query_hash.should == { "query_hash" => "true" }
- end
- end
-
- describe "#last_headers" do
- before do
- header "Accept", "application/json"
- header "Content-Type", "application/json"
-
+ test_client.options[:headers] = {
+ "HTTP_ACCEPT" => "application/json",
+ "CONTENT_TYPE" => "application/json"
+ }
test_client.get "/"
end
it "should contain all the headers" do
- test_client.last_headers.should eq({
- "HTTP_ACCEPT" => "application/json",
- "CONTENT_TYPE" => "application/json",
- "HTTP_HOST" => "example.org",
- "HTTP_COOKIE" => ""
+ test_client.request_headers.should eq({
+ "Accept" => "application/json",
+ "Content-Type" => "application/json",
+ "Host" => "example.org",
+ "Cookie" => ""
})
end
end
describe "#headers" do
before do
- test_client.stub!(:headers).and_return({"HTTP_X_CUSTOM_HEADER" => "custom header value"})
+ test_client.options[:headers] = { "HTTP_X_CUSTOM_HEADER" => "custom header value" }
test_client.get "/"
end
it "can be overridden to add headers to the request" do
- test_client.last_headers["HTTP_X_CUSTOM_HEADER"].should eq("custom header value")
+ test_client.request_headers["X-Custom-Header"].should eq("custom header value")
end
end
describe "setup default headers" do
- it "should let you set default headers when creating a new TestClient" do
- test_client = RspecApiDocumentation::TestClient.new(self, :headers => { "HTTP_MY_HEADER" => "hello" })
+ it "should let you set default headers when creating a new RackTestClient" do
+ test_client = RspecApiDocumentation::RackTestClient.new(context, :headers => { "HTTP_MY_HEADER" => "hello" })
test_client.get "/"
- test_client.last_headers["HTTP_MY_HEADER"].should == "hello"
- test_client.last_headers.should have(3).headers
+ test_client.request_headers["My-Header"].should == "hello"
+ test_client.request_headers.should have(3).headers
end
it "should be blank if not set" do
- test_client = RspecApiDocumentation::TestClient.new(self)
+ test_client = RspecApiDocumentation::RackTestClient.new(context)
test_client.get "/"
- test_client.last_headers.should have(2).headers
+ test_client.request_headers.should have(2).headers
end
end
context "after a request is made" do
before do
- header "Content-Type", "application/json;charset=utf-8"
- header "X-Custom-Header", "custom header value"
+ test_client.options[:headers] = {
+ "CONTENT_TYPE" => "application/json;charset=utf-8",
+ "HTTP_X_CUSTOM_HEADER" => "custom header value"
+ }
test_client.post "/greet?query=test+query", post_data
end
@@ -158,7 +135,7 @@ class StubApp < Sinatra::Base
metadata[:response_body].should be_present
metadata[:response_headers].should match(/^Content-Type: application\/json/)
metadata[:response_headers].should match(/^Content-Length: 17$/)
- metadata[:curl].should eq(RspecApiDocumentation::Curl.new("post", "/greet?query=test+query", post_data, {"CONTENT_TYPE" => "application/json;charset=utf-8", "HTTP_X_CUSTOM_HEADER" => "custom header value", "HTTP_HOST" => "example.org", "HTTP_COOKIE" => ""}))
+ metadata[:curl].should eq(RspecApiDocumentation::Curl.new("POST", "/greet?query=test+query", post_data, {"Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value", "Host" => "example.org", "Cookie" => ""}))
end
context "when post data is not json" do
View
52 spec/test_server_spec.rb
@@ -1,52 +0,0 @@
-require 'spec_helper'
-require 'webmock/rspec'
-require 'rack/test'
-
-describe RspecApiDocumentation::TestServer do
- let(:test_server) { described_class.new(self) }
-
- subject { test_server }
-
- its(:session) { should equal(self) }
- its(:example) { should equal(example) }
-
- context "being called as a rack application" do
- include Rack::Test::Methods
-
- let(:app) { test_server }
- let(:method) { :post }
- let(:path) { "/path" }
- let(:body) { {:foo => "bar", :baz => "quux"}.to_json }
- let(:headers) {{
- "Content-Type" => "application/json",
- "X-Custom-Header" => "custom header value"
- }}
-
- before {
- headers.each { |k, v| header k, v }
- send(method, path, body)
- }
-
- it "should expose the last request" do
- test_server.last_request.should equal(last_request)
- end
-
- it "should expose the last response" do
- test_server.last_response.should equal(last_response)
- end
-
- it "should always return 200" do
- last_response.status.should eq(200)
- end
-
- context "when examples should be documentated", :document => true do
- it "should augment the metadata with information about the request" do
- metadata = example.metadata[:requests].first
- metadata[:request_method].should eq("POST")
- metadata[:request_path].should eq(path)
- metadata[:request_body].should eq(JSON.pretty_generate(JSON.parse(body)))
- metadata[:request_headers].split("\n").sort.should eq("Content-Type: application/json\nX-Custom-Header: custom header value\nHost: example.org\nCookie: ".split("\n").sort)
- end
- end
- end
-end

0 comments on commit 335dbe3

Please sign in to comment.
Something went wrong with that request. Please try again.