Skip to content

Instantly share code, notes, and snippets.

@jcoyne
Last active August 29, 2015 14:04
Show Gist options
  • Save jcoyne/68459510767a57405e4f to your computer and use it in GitHub Desktop.
Save jcoyne/68459510767a57405e4f to your computer and use it in GitHub Desktop.
Trigger 412 ETag mismatch
#!/usr/bin/env ruby
require 'faraday'
URL = 'http://localhost:8983/fedora/rest'
def main
client = Faraday.new(url: URL)
create_retreve_update(client)
end
def create_retreve_update(client)
100.times do |n|
subject = create(client, content)
etag = retrieve(client, subject)
create_subnode(client, "#{subject}/fcr:content", "Foo bar");
begin
update(client, subject, etag, new_content(subject))
rescue => e
puts "On iteration #{n}:"
raise e
end
end
end
def content
"<> <http://fedora.info/definitions/v4/rels-ext#hasModel> \"DummyAsset\" .\n"
end
def new_content(subject)
"<#{subject}> <http://purl.org/dc/terms/title> \"Bar\"^^<http://www.w3.org/2001/XMLSchema#string> .\n"
end
def update(client, subject, etag, new_content)
resp = client.put subject, new_content do |req|
req.headers['If-Match'] = etag
req.headers['Content-Type'] = 'text/turtle'
end
raise "Status was #{resp.status} #{resp.body}" unless resp.status == 204
end
def retrieve(client, subject)
resp = client.get subject do |req|
req.headers["Prefer"] = "return=representation"
end
resp.headers['ETag']
end
def create(client, content)
resp = client.post("#{URL}/test", content) do |req|
req.headers['Content-Type'] = 'text/turtle'
end
resp.headers['Location']
end
def create_subnode(client, url, content)
resp = client.post url, content, 'Content-Type' => 'text/plain'
end
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment