forked from ahx/openapi_first
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi_first_spec.rb
More file actions
147 lines (122 loc) · 4.28 KB
/
openapi_first_spec.rb
File metadata and controls
147 lines (122 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true
require_relative 'spec_helper'
require 'rack'
require 'rack/test'
require 'openapi_first'
RSpec.describe OpenapiFirst do
it 'has a version number' do
expect(OpenapiFirst::VERSION).not_to be nil
end
include Rack::Test::Methods
let(:spec_path) { './spec/data/petstore-expanded.yaml' }
describe '.parse' do
it 'loads a Hash' do
definition = OpenapiFirst.parse(YAML.safe_load_file('./spec/data/petstore.yaml'))
expect(definition.paths.first).to eq('/pets')
end
it 'supports :only' do
hash = YAML.safe_load_file('./spec/data/petstore.yaml')
only = ->(path) { path == '/pets' }
definition = OpenapiFirst.parse(hash, only:)
paths = definition.paths
expect(paths).to include('/pets')
expect(paths).not_to include('/pets/{petId}')
end
it 'supports :path_prefix' do
hash = YAML.safe_load_file('./spec/data/petstore.yaml')
path_prefix = '/api/v1'
definition = OpenapiFirst.parse(hash, path_prefix:)
expect(definition.path_prefix).to eq(path_prefix)
end
it 'loads a Hash' do
definition = OpenapiFirst.parse(YAML.safe_load_file('./spec/data/petstore.yaml'))
expect(definition.paths).to include('/pets')
end
end
describe '.configure' do
it 'does not reset .configuration' do
old_config_instance = described_class.configuration
described_class.configure do |_|
24
end
expect(described_class.configuration).to be(old_config_instance)
end
end
describe '.load' do
begin
require 'multi_json'
before do
MultiJson.load_options = { symbolize_keys: true }
end
after do
MultiJson.load_options = { symbolize_keys: false }
end
rescue LoadError # rubocop:disable Lint/SuppressedException
end
it 'returns a Definition' do
expect(OpenapiFirst.load(spec_path)).to be_a OpenapiFirst::Definition
end
it 'works with a lot of references' do
definition = OpenapiFirst.load('./spec/data/fullofrefs.yaml')
expect(definition.paths).to include('/foo')
end
it 'works with numeric statuses' do
definition = OpenapiFirst.load('./spec/data/numeric-status.yaml')
expect(definition.paths).to include('/roles')
end
it 'works with YAML' do
definition = OpenapiFirst.load('./spec/data/petstore.yaml')
expect(definition.paths).to include('/pets')
end
it 'works with JSON' do
definition = OpenapiFirst.load('./spec/data/petstore.json')
expect(definition.paths).to include('/pets')
end
it 'returns the same definition when a Definition object is passed in' do
original_definition = OpenapiFirst.load('./spec/data/petstore.yaml')
returned_definition = OpenapiFirst.load(original_definition)
expect(returned_definition).to be(original_definition)
end
require 'benchmark'
it 'works with a large document' do
time = Benchmark.realtime do
Timeout.timeout(2) do
OpenapiFirst.load('./spec/data/large.yaml')
end
end
expect(time).to be < 1
end
context 'with a symbol' do
it 'raises an exception if OAD has not been registered' do
expect { OpenapiFirst.load(:unknown) }.to raise_error(OpenapiFirst::NotRegisteredError)
end
it 'returns the registered OAD' do
OpenapiFirst.register(spec_path)
oad = OpenapiFirst.load(:default)
expect(oad.key).to eq(spec_path)
end
end
describe 'only option' do
specify 'with empty filter' do
definition = OpenapiFirst.load(spec_path, only: nil)
expected = %w[/pets /pets/{id}]
expect(definition.paths).to eq expected
end
specify 'filtering paths' do
definition = OpenapiFirst.load spec_path, only: ->(path) { path == '/pets' }
expected = %w[/pets]
expect(definition.paths).to eq expected
end
end
describe 'path_prefix option' do
specify 'without a path prefix' do
definition = OpenapiFirst.load(spec_path, path_prefix: nil)
expect(definition.path_prefix).to be_nil
end
specify 'with a path prefix' do
definition = OpenapiFirst.load(spec_path, path_prefix: '/api/v1')
expect(definition.path_prefix).to eq('/api/v1')
end
end
end
end