spec/unit/pacto/stubs/uri_pattern_spec.rb in pacto-0.3.1 vs spec/unit/pacto/stubs/uri_pattern_spec.rb in pacto-0.4.0.rc1
- old
+ new
@@ -1,28 +1,56 @@
+# -*- encoding : utf-8 -*-
require 'spec_helper'
module Pacto
describe UriPattern do
context 'with non-strict matchers' do
- it 'returns a regex containing the host and path' do
+ before(:each) do
Pacto.configuration.strict_matchers = false
- request = double(host: 'myhost.com', path: '/stuff')
- expect(UriPattern.for(request).to_s).to eq(/myhost\.com\/stuff/.to_s)
end
- it 'turns segments preceded by : into wildcards' do
- Pacto.configuration.strict_matchers = false
- request = double(host: 'myhost.com', path: '/:id')
- wildcard = '[^\/\?#]+'
- expect(UriPattern.for(request).to_s).to eq(/myhost\.com\/#{wildcard}/.to_s)
+ it 'appends host if path is an Addressable::Template' do
+ path_pattern = '/{account}/data{.format}{?page,per_page}'
+ path = Addressable::Template.new path_pattern
+ request = Fabricate(:request_clause, host: 'https://www.example.com', path: path)
+ expect(UriPattern.for(request).pattern).to include(Addressable::Template.new("https://www.example.com#{path_pattern}").pattern)
end
+
+ it 'returns a URITemplate containing the host and path and wildcard vars' do
+ request = Fabricate(:request_clause, host: 'myhost.com', path: '/stuff')
+ uri_pattern = UriPattern.for(request)
+ expect(uri_pattern.pattern).to eql('{scheme}://myhost.com/stuff{?anyvars*}')
+ end
+
+ it 'convers segments preceded by : into variables', :deprecated do
+ request = Fabricate(:request_clause, host: 'myhost.com', path: '/:id')
+ uri_pattern = UriPattern.for(request)
+ expect(uri_pattern.keys).to include('id')
+ expect(uri_pattern.pattern).to_not include(':id')
+ end
+
+ it 'creates a regex that does not allow additional path elements' do
+ request = Fabricate(:request_clause, host: 'myhost.com', path: '/:id')
+ pattern = UriPattern.for(request)
+ expect(pattern).to match('http://myhost.com/foo')
+ expect(pattern).to_not match('http://myhost.com/foo/bar')
+ end
+
+ it 'creates a regex that does allow query parameters', :deprecated do
+ request = Fabricate(:request_clause, host: 'myhost.com', path: '/:id')
+ pattern = UriPattern.for(request)
+ expect(pattern.match('http://myhost.com/foo?a=b')). to be_truthy
+ expect(pattern.match('http://myhost.com/foo?a=b&c=d')).to be_truthy
+ end
end
- context 'with strict matchers' do
+ # Strict/relaxed matching should be done against the full URI or path only
+ context 'with strict matchers', deprecated: true do
it 'returns a string with the host and path' do
Pacto.configuration.strict_matchers = true
- request = double(host: 'myhost.com', path: '/stuff')
- expect(UriPattern.for(request)).to eq('myhost.com/stuff')
+ request = Fabricate(:request_clause, host: 'myhost.com', path: '/stuff')
+ uri_pattern = UriPattern.for(request)
+ expect(uri_pattern.pattern).to eq('{scheme}://myhost.com/stuff')
end
end
end
end