Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/kong/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def create
super
end

# update resource
# Create or update resource
def create_or_update
flatten_config
super
end

# Update resource
def update
flatten_config
super
Expand Down
18 changes: 18 additions & 0 deletions spec/kong/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@
end
end

describe '#create_or_update' do
it 'transforms config keys to config.key format' do
headers = { 'Content-Type' => 'application/json' }
attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' }
expect(Kong::Client.instance).to receive(:put).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes)
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } })
subject.create_or_update
end

it 'transforms nested config keys to config.key format' do
headers = { 'Content-Type' => 'application/json' }
attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345', 'config.first.second' => '1' }
expect(Kong::Client.instance).to receive(:put).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes)
subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } })
subject.create_or_update
end
end

describe '#update' do
it 'transforms config keys to config.key format' do
headers = { 'Content-Type' => 'application/json' }
Expand Down