forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversioning_examples.rb
121 lines (105 loc) · 3.64 KB
/
versioning_examples.rb
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
shared_examples_for 'versioning' do
it 'sets the API version' do
subject.format :txt
subject.version 'v1', macro_options
subject.get :hello do
"Version: #{request.env['api.version'] }"
end
versioned_get '/hello', 'v1', macro_options
expect(last_response.body).to eql "Version: v1"
end
it 'adds the prefix before the API version' do
subject.format :txt
subject.prefix 'api'
subject.version 'v1', macro_options
subject.get :hello do
"Version: #{request.env['api.version'] }"
end
versioned_get '/hello', 'v1', macro_options.merge(prefix: 'api')
expect(last_response.body).to eql "Version: v1"
end
it 'is able to specify version as a nesting' do
subject.version 'v2', macro_options
subject.get '/awesome' do
"Radical"
end
subject.version 'v1', macro_options do
get '/legacy' do
"Totally"
end
end
versioned_get '/awesome', 'v1', macro_options
expect(last_response.status).to eql 404
versioned_get '/awesome', 'v2', macro_options
expect(last_response.status).to eql 200
versioned_get '/legacy', 'v1', macro_options
expect(last_response.status).to eql 200
versioned_get '/legacy', 'v2', macro_options
expect(last_response.status).to eql 404
end
it 'is able to specify multiple versions' do
subject.version 'v1', 'v2', macro_options
subject.get 'awesome' do
"I exist"
end
versioned_get '/awesome', 'v1', macro_options
expect(last_response.status).to eql 200
versioned_get '/awesome', 'v2', macro_options
expect(last_response.status).to eql 200
versioned_get '/awesome', 'v3', macro_options
expect(last_response.status).to eql 404
end
context 'with different versions for the same endpoint' do
context 'without a prefix' do
it 'allows the same endpoint to be implemented' do
subject.format :txt
subject.version 'v2', macro_options
subject.get 'version' do
request.env['api.version']
end
subject.version 'v1', macro_options do
get 'version' do
"version " + request.env['api.version']
end
end
versioned_get '/version', 'v2', macro_options
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('v2')
versioned_get '/version', 'v1', macro_options
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('version v1')
end
end
context 'with a prefix' do
it 'allows the same endpoint to be implemented' do
subject.format :txt
subject.prefix 'api'
subject.version 'v2', macro_options
subject.get 'version' do
request.env['api.version']
end
subject.version 'v1', macro_options do
get 'version' do
"version " + request.env['api.version']
end
end
versioned_get '/version', 'v1', macro_options.merge(prefix: subject.prefix)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('version v1')
versioned_get '/version', 'v2', macro_options.merge(prefix: subject.prefix)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('v2')
end
end
end
it 'does not overwrite version parameter with API version' do
subject.format :txt
subject.version 'v1', macro_options
subject.params { requires :version }
subject.get :api_version_with_version_param do
params[:version]
end
versioned_get '/api_version_with_version_param?version=1', 'v1', macro_options
expect(last_response.body).to eql '1'
end
end