Permalink
Browse files

Send webmention successfully

  • Loading branch information...
adamdawkins committed Jan 26, 2019
1 parent c9b28fd commit 4e27f7f5fbd0ea8f87829263d7f0f260bb77b96f
Showing with 26 additions and 10 deletions.
  1. +13 −4 app/services/webmention_service.rb
  2. +13 −6 spec/services/webmention_service_spec.rb
@@ -6,11 +6,19 @@
class WebmentionService
attr_reader :endpoint

def initialize(url)
@url = url
@response = HTTParty.get(@url)
@endpoint = to_absolute_url(@url, discover_endpoint)
def initialize(source, target)
@source = source
@target = target
@response = HTTParty.get(@target)
@endpoint = to_absolute_url(@target, discover_endpoint)
end

def send
return if @endpoint.nil?
response = HTTParty.post(@endpoint, { body: {target: @target, source: @source }})
pp response
end

private

def discover_endpoint
@@ -36,6 +44,7 @@ def discover_endpoint_from_document
href = @doc.at_css('[rel~="webmention"][href]').andand[:href]
end


def to_absolute_url(origin, target)
Parser.parse(origin).merge(Parser.parse(target)).to_s
end
@@ -9,16 +9,17 @@
def webmention_rocks_endpoint_test(test_numbers = [])
test_numbers.each do |number|
expect(
WebmentionService.new("https://webmention.rocks/test/#{number}").endpoint
WebmentionService.new("adamdawkins.uk/1", "https://webmention.rocks/test/#{number}").endpoint
).to eq "https://webmention.rocks/test/#{number}/webmention"
end
end

RSpec.describe WebmentionService do
describe "Sending Webmentions" do
describe "Sender discovers receiver Webmention endpoint", :vcr do
let(:source) { "http://adamdawkins.uk/1" }
it "*must* fetch the link" do
WebmentionService.new("https://webmention.rocks/test/1")
WebmentionService.new(source, "https://webmention.rocks/test/1")
expect(a_request(:get, "https://webmention.rocks/test/1")).to have_been_made
end
it "*must* follow all redirects" do
@@ -68,7 +69,7 @@ def webmention_rocks_endpoint_test(test_numbers = [])

# 15. uses a <link> with an empty string, testing that the page itself is the endpoint
expect(
WebmentionService.new("https://webmention.rocks/test/15").endpoint
WebmentionService.new(source, "https://webmention.rocks/test/15").endpoint
).to eq "https://webmention.rocks/test/15"
end

@@ -103,12 +104,18 @@ def webmention_rocks_endpoint_test(test_numbers = [])
end

describe "Sender notifies receiver", :vcr do
it "*must* preserve query string parameters" do
https://webmention.rocks/test/21
let(:source) { "http://1c011a7e.ngrok.io/2019/1/26/a-test-note-for-webmentionr-cks-https-webmention-rocks-test-1-https-webmention-rocks-test-2-https-webmention-rocks-test-3-https-webment" }
let(:target) { "https://webmention.rocks/test/1" }
let(:webmention_endpoint) { "https://webmention.rocks/test/1/webmention" }

it "*must* post x-www-form-urlencoded source and target to endpoint" do
WebmentionService.new(source, target).send
expect(a_request(:post, webmention_endpoint).with(body: { source: source, target: target }))
end
it "*must* post x-www-form-urlencoded source and target to endpoint"

context "endpoint contains query string parameter" do
it "*must* preserve query string parameters and not send them in the POST body"
# https://webmention.rocks/test/21
end

it "*must* consider any 2xx responses a success"

0 comments on commit 4e27f7f

Please sign in to comment.