From 084511e8b861ef4da7d6e276db3d3a7aa9a23a12 Mon Sep 17 00:00:00 2001 From: Daniel Barber Date: Sat, 16 Nov 2019 16:58:10 -0500 Subject: [PATCH] Add first JSON spec --- lib/micropub/webserver.rb | 11 ++++++++++- test/requests/create_post_test.rb | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/micropub/webserver.rb b/lib/micropub/webserver.rb index 1cf6004..b706f7b 100644 --- a/lib/micropub/webserver.rb +++ b/lib/micropub/webserver.rb @@ -26,7 +26,7 @@ module Micropub post "/micropub/main" do if valid_token? - post = Post.new(params) + post = Post.new(post_params) if github.post!(post) headers "Location" => "#{ENV.fetch("SITE_URL")}#{post.path}" @@ -39,6 +39,15 @@ module Micropub end end + def post_params + if request.env["CONTENT_TYPE"] == "application/json" + request.body.rewind + JSON.parse(request.body.read) + else + params + end + end + def valid_token? token = Indieauth::Token.new(endpoints.token_endpoint) diff --git a/test/requests/create_post_test.rb b/test/requests/create_post_test.rb index 42b48f2..4adb149 100644 --- a/test/requests/create_post_test.rb +++ b/test/requests/create_post_test.rb @@ -61,5 +61,16 @@ describe "create post" do end it "creates a post with JSON" do + post_json = { + content: "Hello, World!", + category: ["one", "two", "three"], + }.to_json + + post "/micropub/main", post_json, { "CONTENT_TYPE" => "application/json" } + + date = Time.now.strftime("%Y/%m/%d") + assert last_response.accepted? + assert_equal last_response.headers["Location"], + "https://test.danbarber.me/blog/#{date}/hello-world" end end