|
1 | 1 | require 'spec_helper'
|
2 | 2 | require 'rack/test'
|
| 3 | +require 'capybara' |
| 4 | +require 'capybara/server' |
| 5 | +require 'sinatra/base' |
| 6 | +require 'webmock' |
| 7 | +require 'support/stub_app' |
3 | 8 |
|
4 | 9 | describe RspecApiDocumentation::HttpTestClient do
|
5 | 10 | before(:all) do
|
6 |
| - WebMock.allow_net_connect! |
| 11 | + WebMock.allow_net_connect! |
7 | 12 |
|
8 |
| - $external_test_app_pid = spawn("ruby ./spec/support/external_test_app.rb") |
9 |
| - Process.detach $external_test_app_pid |
10 |
| - sleep 3 #Wait until the test app is up |
| 13 | + Capybara.server do |app, port| |
| 14 | + require 'rack/handler/thin' |
| 15 | + Thin::Logging.silent = true |
| 16 | + Rack::Handler::Thin.run(app, :Port => port) |
| 17 | + end |
| 18 | + |
| 19 | + server = Capybara::Server.new(StubApp.new, 8888) |
| 20 | + server.boot |
11 | 21 | end
|
12 | 22 |
|
13 | 23 | after(:all) do
|
14 | 24 | WebMock.disable_net_connect!
|
15 |
| - |
16 |
| - Process.kill('TERM', $external_test_app_pid) |
17 | 25 | end
|
18 | 26 |
|
19 |
| - let(:client_context) { double(example: example, app_root: 'nowhere') } |
20 |
| - let(:target_host) { 'http://localhost:4567' } |
| 27 | + let(:client_context) { |example| double(example: example, app_root: 'nowhere') } |
| 28 | + let(:target_host) { 'http://localhost:8888' } |
21 | 29 | let(:test_client) { RspecApiDocumentation::HttpTestClient.new(client_context, {host: target_host}) }
|
22 | 30 |
|
23 | 31 | subject { test_client }
|
24 | 32 |
|
25 | 33 | it { should be_a(RspecApiDocumentation::HttpTestClient) }
|
26 | 34 |
|
27 | 35 | its(:context) { should equal(client_context) }
|
28 |
| - its(:example) { should equal(example) } |
29 |
| - its(:metadata) { should equal(example.metadata) } |
| 36 | + its(:example) { |example| should equal(example) } |
| 37 | + its(:metadata) { |example| should equal(example.metadata) } |
30 | 38 |
|
31 | 39 | describe "xml data", :document => true do
|
32 | 40 | before do
|
33 | 41 | test_client.get "/xml"
|
34 | 42 | end
|
35 | 43 |
|
36 | 44 | it "should handle xml data" do
|
37 |
| - test_client.response_headers["Content-Type"].should =~ /application\/xml/ |
| 45 | + expect(test_client.response_headers["Content-Type"]).to match(/application\/xml/) |
38 | 46 | end
|
39 | 47 |
|
40 |
| - it "should log the request" do |
41 |
| - example.metadata[:requests].first[:response_body].should be_present |
| 48 | + it "should log the request" do |example| |
| 49 | + expect(example.metadata[:requests].first[:response_body]).to be_present |
42 | 50 | end
|
43 | 51 | end
|
44 | 52 |
|
|
48 | 56 | end
|
49 | 57 |
|
50 | 58 | it 'should contain the query_string' do
|
51 |
| - test_client.query_string.should == "query_string=true" |
| 59 | + expect(test_client.query_string).to eq("query_string=true") |
52 | 60 | end
|
53 | 61 | end
|
54 | 62 |
|
|
58 | 66 | end
|
59 | 67 |
|
60 | 68 | it "should contain all the headers" do
|
61 |
| - test_client.request_headers.should eq({ |
| 69 | + expect(test_client.request_headers).to eq({ |
62 | 70 | "Accept" => "application/json",
|
63 | 71 | "Content-Type" => "application/json"
|
64 | 72 | })
|
|
71 | 79 | end
|
72 | 80 |
|
73 | 81 | context "when examples should be documented", :document => true do
|
74 |
| - it "should still argument the metadata" do |
| 82 | + it "should still argument the metadata" do |example| |
75 | 83 | metadata = example.metadata[:requests].first
|
76 |
| - metadata[:request_query_parameters].should == {'query' => "", 'other' => 'exists'} |
| 84 | + expect(metadata[:request_query_parameters]).to eq({'query' => "", 'other' => 'exists'}) |
77 | 85 | end
|
78 | 86 | end
|
79 | 87 | end
|
|
87 | 95 | let(:headers) { { "Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value" } }
|
88 | 96 |
|
89 | 97 | context "when examples should be documented", :document => true do
|
90 |
| - it "should augment the metadata with information about the request" do |
| 98 | + it "should augment the metadata with information about the request" do |example| |
91 | 99 | metadata = example.metadata[:requests].first
|
92 |
| - metadata[:request_method].should eq("POST") |
93 |
| - metadata[:request_path].should eq("/greet?query=test+query") |
94 |
| - metadata[:request_body].should be_present |
95 |
| - metadata[:request_headers].should include({'CONTENT_TYPE' => 'application/json;charset=utf-8'}) |
96 |
| - metadata[:request_headers].should include({'HTTP_X_CUSTOM_HEADER' => 'custom header value'}) |
97 |
| - metadata[:request_query_parameters].should == {"query" => "test query"} |
98 |
| - metadata[:request_content_type].should match(/application\/json/) |
99 |
| - metadata[:response_status].should eq(200) |
100 |
| - metadata[:response_body].should be_present |
101 |
| - metadata[:response_headers]['Content-Type'].should match(/application\/json/) |
102 |
| - metadata[:response_headers]['Content-Length'].should == '18' |
103 |
| - metadata[:response_content_type].should match(/application\/json/) |
104 |
| - 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"})) |
| 100 | + expect(metadata[:request_method]).to eq("POST") |
| 101 | + expect(metadata[:request_path]).to eq("/greet?query=test+query") |
| 102 | + expect(metadata[:request_body]).to be_present |
| 103 | + expect(metadata[:request_headers]).to include({'CONTENT_TYPE' => 'application/json;charset=utf-8'}) |
| 104 | + expect(metadata[:request_headers]).to include({'HTTP_X_CUSTOM_HEADER' => 'custom header value'}) |
| 105 | + expect(metadata[:request_query_parameters]).to eq({"query" => "test query"}) |
| 106 | + expect(metadata[:request_content_type]).to match(/application\/json/) |
| 107 | + expect(metadata[:response_status]).to eq(200) |
| 108 | + expect(metadata[:response_body]).to be_present |
| 109 | + expect(metadata[:response_headers]['Content-Type']).to match(/application\/json/) |
| 110 | + expect(metadata[:response_headers]['Content-Length']).to eq('17') |
| 111 | + expect(metadata[:response_content_type]).to match(/application\/json/) |
| 112 | + expect(metadata[:curl]).to eq(RspecApiDocumentation::Curl.new("POST", "/greet?query=test+query", post_data, {"Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value"})) |
105 | 113 | end
|
106 | 114 |
|
107 | 115 | context "when post data is not json" do
|
108 | 116 | let(:post_data) { { :target => "nurse", :email => "[email protected]" } }
|
| 117 | + let(:headers) { { "X-Custom-Header" => "custom header value" } } |
109 | 118 |
|
110 |
| - it "should not nil out request_body" do |
| 119 | + it "should not nil out request_body" do |example| |
111 | 120 | body = example.metadata[:requests].first[:request_body]
|
112 |
| - body.should =~ /target=nurse/ |
113 |
| - body.should =~ /email=email%40example\.com/ |
| 121 | + expect(body).to match(/target=nurse/) |
| 122 | + expect(body).to match(/email=email%40example\.com/) |
114 | 123 | end
|
115 | 124 | end
|
116 | 125 |
|
117 | 126 | context "when post data is nil" do
|
118 | 127 | let(:post_data) { }
|
119 | 128 |
|
120 |
| - it "should nil out request_body" do |
121 |
| - example.metadata[:requests].first[:request_body].should be_nil |
| 129 | + it "should nil out request_body" do |example| |
| 130 | + expect(example.metadata[:requests].first[:request_body]).to be_nil |
122 | 131 | end
|
123 | 132 | end
|
124 | 133 | end
|
|
0 commit comments