spec/unit/pacto/generator_spec.rb in pacto-0.3.0.pre vs spec/unit/pacto/generator_spec.rb in pacto-0.3.0
- old
+ new
@@ -1,75 +1,55 @@
module Pacto
describe Generator do
- let(:record_host) {
+ let(:record_host) do
'http://example.com'
- }
+ end
+
let(:request) do
- Pacto::Request.new(record_host, {
- 'method' => 'GET',
- 'path' => '/abcd',
- 'headers' => {
+ Faraday::Request.create :get do |req|
+ req.path = '/abcd'
+ req.params = { 'apikey' => "<%= ENV['MY_API_KEY'] %>" }
+ req.headers = {
'Content-Length' => [1234],
'Via' => ['Some Proxy'],
'User-Agent' => ['rspec']
- },
- 'params' => []
- })
+ }
+ end
end
+
let(:response_adapter) do
- Pacto::ResponseAdapter.new(
- OpenStruct.new({
- 'status' => 200,
- 'headers' => {
- 'Date' => [Time.now],
- 'Server' => ['Fake Server'],
- 'Content-Type' => ['application/json']
- },
- 'body' => double('dummy body')
- })
+ Faraday::Response.new(
+ :status => 200,
+ :response_headers => {
+ 'Date' => [Time.now],
+ 'Server' => ['Fake Server'],
+ 'Content-Type' => ['application/json'],
+ 'Vary' => ['User-Agent']
+ },
+ :body => 'dummy body' # body is just a string
)
end
+ let(:filtered_request_headers) { double('filtered_response_headers') }
+ let(:filtered_response_headers) { double('filtered_response_headers') }
let(:response_body_schema) { '{"message": "dummy generated schema"}' }
let(:version) { 'draft3' }
let(:schema_generator) { double('schema_generator') }
let(:validator) { double('validator') }
+ let(:filters) { double :filters }
let(:request_file) { 'request.json' }
- let(:generator) { described_class.new version, schema_generator, validator }
+ let(:options) { Pacto.configuration.generator_options }
+ let(:generator) { described_class.new version, schema_generator, validator, options, filters }
- def pretty obj
+ def pretty(obj)
MultiJson.encode(obj, :pretty => true).gsub(/^$\n/, '')
end
- describe '#generate' do
- let(:request_contract) {
- double({
- :request => request,
- })
- }
- let(:generated_contract) { double('generated contract') }
+ describe '#save' do
before do
- Pacto.should_receive(:build_from_file).with(request_file, record_host).and_return request_contract
- request.should_receive(:execute).and_return response_adapter
+ filters.should_receive(:filter_request_headers).with(request, response_adapter).and_return filtered_request_headers
+ filters.should_receive(:filter_response_headers).with(request, response_adapter).and_return filtered_response_headers
end
-
- it 'parses the request' do
- generator.should_receive(:save).with(request_file, request, anything)
- generator.generate request_file, record_host
- end
-
- it 'fetches a response' do
- generator.should_receive(:save).with(request_file, anything, response_adapter)
- generator.generate request_file, record_host
- end
-
- it 'saves the result' do
- generator.should_receive(:save).with(request_file, request, response_adapter).and_return generated_contract
- expect(generator.generate request_file, record_host).to eq(generated_contract)
- end
- end
-
- describe '#save' do
context 'invalid schema' do
it 'raises an error if schema generation fails' do
JSON::SchemaGenerator.should_receive(:generate).and_raise ArgumentError.new('Could not generate schema')
expect { generator.save request_file, request, response_adapter }.to raise_error
end
@@ -80,15 +60,15 @@
expect { generator.save request_file, request, response_adapter }.to raise_error
end
end
context 'valid schema' do
- let(:raw_contract) {
- JSON::SchemaGenerator.should_receive(:generate).with(request_file, response_adapter.body, 'draft3').and_return response_body_schema
+ let(:raw_contract) do
+ JSON::SchemaGenerator.should_receive(:generate).with(request_file, response_adapter.body, Pacto.configuration.generator_options).and_return response_body_schema
validator.should_receive(:validate).and_return true
generator.save request_file, request, response_adapter
- }
+ end
subject(:generated_contract) { JSON.parse raw_contract }
it 'sets the body to the generated json-schema' do
expect(subject['response']['body']).to eq(JSON.parse response_body_schema)
end
@@ -97,41 +77,22 @@
generated_request = subject['request']
expect(generated_request['params']).to eq(request.params)
expect(generated_request['path']).to eq(request.path)
end
- it 'keeps important request headers' do
- saved_headers = subject['request']['headers']
- expect(saved_headers.keys).to include 'User-Agent'
+ it 'preserves ERB in the request params' do
+ generated_request = subject['request']
+ expect(generated_request['params']['apikey']).to eq("<%= ENV['MY_API_KEY'] %>")
end
- it 'filters informational request headers' do
- saved_headers = subject['request']['headers']
- expect(saved_headers).not_to include 'Date'
- expect(saved_headers).not_to include 'Server'
- expect(saved_headers).not_to include 'Content-Length'
- expect(saved_headers).not_to include 'Connection'
- end
-
it 'normalizes the request method' do
generated_request = subject['request']
expect(generated_request['method']).to eq(request.method.downcase.to_s)
end
it 'sets the response attributes' do
generated_response = subject['response']
expect(generated_response['status']).to eq(response_adapter.status)
- end
-
- it 'keeps important response headers' do
- saved_headers = subject['response']['headers']
- expect(saved_headers.keys).to include 'Content-Type'
- end
-
- it 'filters informational response headers' do
- saved_headers = subject['response']['headers']
- expect(saved_headers).not_to include 'Content-Length'
- expect(saved_headers).not_to include 'Via'
end
it 'generates pretty JSON' do
expect(raw_contract).to eq(pretty(subject))
end