Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow json upload to be a json body #580

Merged
merged 3 commits into from
Oct 30, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Purging a queue with a lot of messages blocked LavinMQ from other operations.
- Message timestamps not being updated when dead lettered breaking TTL.

### Changed

- Definitions uploads can now be JSON body [#580](https://github.com/cloudamqp/lavinmq/pull/580)

## [1.2.4] - 2023-09-26

### Fixed
Expand Down
9 changes: 9 additions & 0 deletions spec/api/definitions_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,15 @@ describe LavinMQ::HTTP::Server do
Server.vhosts["uploaded_vhost"]?.should_not be_nil
Server.vhosts["uploaded_vhost"].should be_a(LavinMQ::VHost)
end

it "imports definitions from json body" do
body = {vhosts: [{name: "uploaded_vhost"}]}.to_json
headers = {"Content-Type" => "application/json"}
response = post("/api/definitions/upload", headers: headers, body: body)
response.status_code.should eq 200
Server.vhosts["uploaded_vhost"]?.should_not be_nil
Server.vhosts["uploaded_vhost"].should be_a(LavinMQ::VHost)
end
end

it "should update existing user on import" do
Expand Down
13 changes: 9 additions & 4 deletions src/lavinmq/http/controller/definitions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ module LavinMQ

post "/api/definitions/upload" do |context, _params|
refuse_unless_administrator(context, user(context))
::HTTP::FormData.parse(context.request) do |part|
if part.name == "file"
body = JSON.parse(part.body)
GlobalDefinitions.new(@amqp_server).import(body)
if context.request.headers["Content-Type"] == "application/json"
body = parse_body(context)
GlobalDefinitions.new(@amqp_server).import(body)
else
::HTTP::FormData.parse(context.request) do |part|
if part.name == "file"
body = JSON.parse(part.body)
GlobalDefinitions.new(@amqp_server).import(body)
end
end
end
redirect_back(context) if context.request.headers["Referer"]?
Expand Down