|
| 1 | +require 'spec_helper' |
| 2 | +require 'rack/test' |
| 3 | +require 'capybara' |
| 4 | +require 'capybara/server' |
| 5 | +require 'sinatra/base' |
| 6 | +require 'webmock' |
| 7 | +require 'support/stub_app' |
| 8 | + |
| 9 | +describe RspecApiDocumentation::HttpTestClient do |
| 10 | + before(:all) do |
| 11 | + WebMock.allow_net_connect! |
| 12 | + |
| 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 |
| 21 | + end |
| 22 | + |
| 23 | + after(:all) do |
| 24 | + WebMock.disable_net_connect! |
| 25 | + end |
| 26 | + |
| 27 | + let(:client_context) { |example| double(example: example, app_root: 'nowhere') } |
| 28 | + let(:target_host) { 'http://localhost:8888' } |
| 29 | + let(:test_client) { RspecApiDocumentation::HttpTestClient.new(client_context, {host: target_host}) } |
| 30 | + |
| 31 | + subject { test_client } |
| 32 | + |
| 33 | + it { should be_a(RspecApiDocumentation::HttpTestClient) } |
| 34 | + |
| 35 | + its(:context) { should equal(client_context) } |
| 36 | + its(:example) { |example| should equal(example) } |
| 37 | + its(:metadata) { |example| should equal(example.metadata) } |
| 38 | + |
| 39 | + describe "xml data", :document => true do |
| 40 | + before do |
| 41 | + test_client.get "/xml" |
| 42 | + end |
| 43 | + |
| 44 | + it "should handle xml data" do |
| 45 | + expect(test_client.response_headers["Content-Type"]).to match(/application\/xml/) |
| 46 | + end |
| 47 | + |
| 48 | + it "should log the request" do |example| |
| 49 | + expect(example.metadata[:requests].first[:response_body]).to be_present |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + describe "#query_string" do |
| 54 | + before do |
| 55 | + test_client.get "/?query_string=true" |
| 56 | + end |
| 57 | + |
| 58 | + it 'should contain the query_string' do |
| 59 | + expect(test_client.query_string).to eq("query_string=true") |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe "#request_headers" do |
| 64 | + before do |
| 65 | + test_client.get "/", {}, { "Accept" => "application/json", "Content-Type" => "application/json" } |
| 66 | + end |
| 67 | + |
| 68 | + it "should contain all the headers" do |
| 69 | + expect(test_client.request_headers).to eq({ |
| 70 | + "Accept" => "application/json", |
| 71 | + "Content-Type" => "application/json" |
| 72 | + }) |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context "when doing request without parameter value" do |
| 77 | + before do |
| 78 | + test_client.post "/greet?query=&other=exists" |
| 79 | + end |
| 80 | + |
| 81 | + context "when examples should be documented", :document => true do |
| 82 | + it "should still argument the metadata" do |example| |
| 83 | + metadata = example.metadata[:requests].first |
| 84 | + expect(metadata[:request_query_parameters]).to eq({'query' => "", 'other' => 'exists'}) |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + context "after a request is made" do |
| 90 | + before do |
| 91 | + test_client.post "/greet?query=test+query", post_data, headers |
| 92 | + end |
| 93 | + |
| 94 | + let(:post_data) { { :target => "nurse" }.to_json } |
| 95 | + let(:headers) { { "Content-Type" => "application/json;charset=utf-8", "X-Custom-Header" => "custom header value" } } |
| 96 | + |
| 97 | + context "when examples should be documented", :document => true do |
| 98 | + it "should augment the metadata with information about the request" do |example| |
| 99 | + metadata = example.metadata[:requests].first |
| 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({'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"})) |
| 113 | + end |
| 114 | + |
| 115 | + context "when post data is not json" do |
| 116 | + let(:post_data) { { :target => "nurse", :email => "[email protected]" } } |
| 117 | + let(:headers) { { "X-Custom-Header" => "custom header value" } } |
| 118 | + |
| 119 | + it "should not nil out request_body" do |example| |
| 120 | + body = example.metadata[:requests].first[:request_body] |
| 121 | + expect(body).to match(/target=nurse/) |
| 122 | + expect(body).to match(/email=email%40example\.com/) |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + context "when post data is nil" do |
| 127 | + let(:post_data) { } |
| 128 | + |
| 129 | + it "should nil out request_body" do |example| |
| 130 | + expect(example.metadata[:requests].first[:request_body]).to be_nil |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | +end |
0 commit comments