1
0
mirror of https://github.com/danbee/micropub.git synced 2025-03-04 08:59:13 +00:00

Add first JSON spec

This commit is contained in:
Daniel Barber 2019-11-16 16:58:10 -05:00
parent dca44465e1
commit 084511e8b8
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 21 additions and 1 deletions

View File

@ -26,7 +26,7 @@ module Micropub
post "/micropub/main" do post "/micropub/main" do
if valid_token? if valid_token?
post = Post.new(params) post = Post.new(post_params)
if github.post!(post) if github.post!(post)
headers "Location" => "#{ENV.fetch("SITE_URL")}#{post.path}" headers "Location" => "#{ENV.fetch("SITE_URL")}#{post.path}"
@ -39,6 +39,15 @@ module Micropub
end end
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? def valid_token?
token = Indieauth::Token.new(endpoints.token_endpoint) token = Indieauth::Token.new(endpoints.token_endpoint)

View File

@ -61,5 +61,16 @@ describe "create post" do
end end
it "creates a post with JSON" do 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
end end