govuk-client-url_arbiter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +13 -0
- data/govuk-client-url_arbiter.gemspec +28 -0
- data/jenkins.sh +14 -0
- data/jenkins_branches.sh +20 -0
- data/lib/govuk/client/errors.rb +61 -0
- data/lib/govuk/client/response.rb +34 -0
- data/lib/govuk/client/test_helpers/url_arbiter.rb +69 -0
- data/lib/govuk/client/url_arbiter.rb +61 -0
- data/lib/govuk/client/url_arbiter/version.rb +7 -0
- data/spec/spec_helper.rb +77 -0
- data/spec/url_arbiter_spec.rb +117 -0
- metadata +183 -0
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.9.3
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 HM Government (Government Digital Service)
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Govuk::Client::UrlArbiter
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'govuk-client-url_arbiter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install govuk-client-url_arbiter
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/govuk-client-url_arbiter/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rspec/core/rake_task'
|
2
|
+
|
3
|
+
RSpec::Core::RakeTask.new(:spec)
|
4
|
+
|
5
|
+
task :default => :spec
|
6
|
+
|
7
|
+
require "gem_publisher"
|
8
|
+
|
9
|
+
desc "Publish gem to RubyGems.org"
|
10
|
+
task :publish_gem do |t|
|
11
|
+
gem = GemPublisher.publish_if_updated("govuk-client-url_arbiter.gemspec", :rubygems)
|
12
|
+
puts "Published #{gem}" if gem
|
13
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'govuk/client/url_arbiter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "govuk-client-url_arbiter"
|
8
|
+
spec.version = GOVUK::Client::URLArbiter::VERSION
|
9
|
+
spec.authors = ["Alex Tomlins"]
|
10
|
+
spec.email = ["alex.tomlins@digital.cabinet-office.gov.uk"]
|
11
|
+
spec.summary = %q{API client for the url-arbiter}
|
12
|
+
spec.homepage = "https://github.com/alphagov/govuk-client-url_arbiter"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rest-client", '~> 1.6'
|
21
|
+
spec.add_dependency "multi_json", "~> 1.0"
|
22
|
+
spec.add_dependency "plek", '~> 1.8'
|
23
|
+
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "gem_publisher", "~> 1.4"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
spec.add_development_dependency "webmock", "~> 1.18.0"
|
28
|
+
end
|
data/jenkins.sh
ADDED
data/jenkins_branches.sh
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
set -e
|
3
|
+
|
4
|
+
VENV_PATH="${HOME}/venv/${JOB_NAME}"
|
5
|
+
|
6
|
+
[ -x ${VENV_PATH}/bin/pip ] || virtualenv ${VENV_PATH}
|
7
|
+
. ${VENV_PATH}/bin/activate
|
8
|
+
|
9
|
+
pip install -q ghtools
|
10
|
+
|
11
|
+
REPO="alphagov/govuk-client-url_arbiter"
|
12
|
+
gh-status "$REPO" "$GIT_COMMIT" pending -d "\"Build #${BUILD_NUMBER} is running on Jenkins\"" -u "$BUILD_URL" >/dev/null
|
13
|
+
|
14
|
+
if ./jenkins.sh; then
|
15
|
+
gh-status "$REPO" "$GIT_COMMIT" success -d "\"Build #${BUILD_NUMBER} succeeded on Jenkins\"" -u "$BUILD_URL" >/dev/null
|
16
|
+
exit 0
|
17
|
+
else
|
18
|
+
gh-status "$REPO" "$GIT_COMMIT" failure -d "\"Build #${BUILD_NUMBER} failed on Jenkins\"" -u "$BUILD_URL" >/dev/null
|
19
|
+
exit 1
|
20
|
+
fi
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
|
3
|
+
require 'govuk/client/response'
|
4
|
+
|
5
|
+
module GOVUK
|
6
|
+
module Client
|
7
|
+
module Errors
|
8
|
+
|
9
|
+
# Map rest-client exceptions onto our own exception hierarchy in order to
|
10
|
+
# insulate users from the details of the HTTP library we're using.
|
11
|
+
#
|
12
|
+
# @api private
|
13
|
+
def self.create_for(restclient_exception)
|
14
|
+
if restclient_exception.http_code
|
15
|
+
case restclient_exception.http_code
|
16
|
+
when 409
|
17
|
+
Conflict.new(restclient_exception)
|
18
|
+
when 422
|
19
|
+
UnprocessableEntity.new(restclient_exception)
|
20
|
+
else
|
21
|
+
HTTPError.new(restclient_exception)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
case restclient_exception
|
25
|
+
when RestClient::RequestTimeout
|
26
|
+
Timeout.new(restclient_exception.message)
|
27
|
+
else
|
28
|
+
BaseError.new(restclient_exception.message)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class BaseError < StandardError; end
|
34
|
+
|
35
|
+
class Timeout < BaseError; end
|
36
|
+
|
37
|
+
class HTTPError < BaseError
|
38
|
+
# @api private
|
39
|
+
def initialize(restclient_exception)
|
40
|
+
super(restclient_exception.message)
|
41
|
+
@wrapped_exception = restclient_exception
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [Integer] The HTTP status code associated with this exception.
|
45
|
+
def code
|
46
|
+
@wrapped_exception.http_code
|
47
|
+
end
|
48
|
+
|
49
|
+
# @return [Response] The response that triggered this exception.
|
50
|
+
def response
|
51
|
+
@response ||= Response.new(code, @wrapped_exception.response)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class Conflict < HTTPError; end
|
56
|
+
|
57
|
+
class UnprocessableEntity < HTTPError; end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
module GOVUK
|
5
|
+
module Client
|
6
|
+
|
7
|
+
# An API response. This delegates to a hash containing the parsed
|
8
|
+
# response body. It also has methods for accessing the response metadata.
|
9
|
+
#
|
10
|
+
# This is expected to represent a HTTP response with a JSON body, but in
|
11
|
+
# the case where the body is not JSON (eg for some error responses), this
|
12
|
+
# will delegate to an empty Hash. The raw response can then be accessed
|
13
|
+
# via the {#raw_body} accessor.
|
14
|
+
class Response < SimpleDelegator
|
15
|
+
|
16
|
+
# @param code [Integer] The http status code
|
17
|
+
# @param body_str [String] The JSON encoded response body.
|
18
|
+
def initialize(code, body_str)
|
19
|
+
@code = code
|
20
|
+
@raw_body = body_str
|
21
|
+
super(MultiJson.load(@raw_body))
|
22
|
+
rescue MultiJson::ParseError
|
23
|
+
# Delegate to empty hash so that this instance still quacks like a hash.
|
24
|
+
super({})
|
25
|
+
end
|
26
|
+
|
27
|
+
# @return [Integer] The HTTP response code
|
28
|
+
attr_reader :code
|
29
|
+
|
30
|
+
# @return [String] The raw HTTP response body
|
31
|
+
attr_reader :raw_body
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'plek'
|
2
|
+
|
3
|
+
module GOVUK
|
4
|
+
module Client
|
5
|
+
module TestHelpers
|
6
|
+
# Some test helpers for the url-arbiter client. This module is expected
|
7
|
+
# to be mixed in to test classes.
|
8
|
+
#
|
9
|
+
# These rely on WebMock being available in the test suite.
|
10
|
+
module URLArbiter
|
11
|
+
URL_ARBITER_ENDPOINT = Plek.new.find('url-arbiter')
|
12
|
+
|
13
|
+
# Stub out some sensible default url-arbiter responses.
|
14
|
+
#
|
15
|
+
# - all +GET+ requests for paths to return a 404.
|
16
|
+
# - all +PUT+ requests to register a path return a 201 along with corresponding sample data.
|
17
|
+
def stub_default_url_arbiter_responses
|
18
|
+
stub_request(:get, %r[\A#{URL_ARBITER_ENDPOINT}/paths/]).
|
19
|
+
to_return(:status => 404)
|
20
|
+
|
21
|
+
stub_request(:put, %r[\A#{URL_ARBITER_ENDPOINT}/paths/]).to_return { |request|
|
22
|
+
base_path = request.uri.path.sub(%r{\A/paths}, '')
|
23
|
+
{:status => 201, :body => url_arbiter_data_for(base_path).to_json, :headers => {:content_type => "application/json"}}
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
# Stub out calls to simulate webmock having registration information
|
28
|
+
# for a given path.
|
29
|
+
#
|
30
|
+
# - +GET+ requests for the path return corresponding sample data.
|
31
|
+
# - +PUT+ requests with a matching publishing_app return 200 along with the sample data.
|
32
|
+
# - +PUT+ requests with a different publishing_app will return a 409 and include error data in the response.
|
33
|
+
#
|
34
|
+
# @param path [String] The path to be reserved.
|
35
|
+
# @param publishing_app [String] The app the path should be registered to.
|
36
|
+
def url_arbiter_has_registration_for(path, publishing_app)
|
37
|
+
data = url_arbiter_data_for(path, "publishing_app" => publishing_app)
|
38
|
+
error_data = data.merge({
|
39
|
+
"errors" => {"base" => ["is already reserved by the #{publishing_app} application"]},
|
40
|
+
})
|
41
|
+
|
42
|
+
stub_request(:get, "#{URL_ARBITER_ENDPOINT}/paths#{path}").
|
43
|
+
to_return(:status => 200, :body => data.to_json, :headers => {:content_type => "application/json"})
|
44
|
+
|
45
|
+
stub_request(:put, "#{URL_ARBITER_ENDPOINT}/paths#{path}").
|
46
|
+
to_return(:status => 409, :body => error_data.to_json, :headers => {:content_type => "application/json"})
|
47
|
+
|
48
|
+
stub_request(:put, "#{URL_ARBITER_ENDPOINT}/paths#{path}").
|
49
|
+
with(:body => {"publishing_app" => publishing_app}).
|
50
|
+
to_return(:status => 200, :body => data.to_json, :headers => {:content_type => "application/json"})
|
51
|
+
end
|
52
|
+
|
53
|
+
# Generate sample url-arbiter data for a given path.
|
54
|
+
#
|
55
|
+
# @param path [String] The path being requested
|
56
|
+
# @param override_attributes [Hash] Any specific attributes to override the defaults.
|
57
|
+
def url_arbiter_data_for(path, override_attributes = {})
|
58
|
+
now = Time.now.utc.iso8601
|
59
|
+
{
|
60
|
+
"path" => path,
|
61
|
+
"publishing_app" => "foo-publisher",
|
62
|
+
"created_at" => now,
|
63
|
+
"updated_at" => now,
|
64
|
+
}.merge(override_attributes)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "govuk/client/url_arbiter/version"
|
2
|
+
require "govuk/client/response"
|
3
|
+
require "govuk/client/errors"
|
4
|
+
|
5
|
+
require "plek"
|
6
|
+
require "rest-client"
|
7
|
+
require "multi_json"
|
8
|
+
|
9
|
+
module GOVUK
|
10
|
+
module Client
|
11
|
+
class URLArbiter
|
12
|
+
|
13
|
+
# @param base_url [String] the base URL for the service (eg
|
14
|
+
# https://url-arbiter.example.com). If unspecified, this will be
|
15
|
+
# looked up with {https://github.com/alphagov/plek Plek}.
|
16
|
+
def initialize(base_url = nil)
|
17
|
+
base_url ||= Plek.new.find('url-arbiter')
|
18
|
+
@base_url = URI.parse(base_url)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Fetch details of a path
|
22
|
+
#
|
23
|
+
# @param path [String] the path to fetch
|
24
|
+
# @return [Response, nil] Details of the reserved path, or +nil+ if the path wasn't found.
|
25
|
+
def path(path)
|
26
|
+
get_json("/paths#{path}")
|
27
|
+
end
|
28
|
+
|
29
|
+
# Reserve a path
|
30
|
+
#
|
31
|
+
# @param path [String] the path to reserve.
|
32
|
+
# @param details [Hash] the request data to be sent to url-arbiter.
|
33
|
+
# @return [Response] Details of the reserved path.
|
34
|
+
# @raise [Errors::Conflict] if the path is already reserved by another app.
|
35
|
+
# @raise [Errors::UnprocessableEntity] for any validation errors.
|
36
|
+
def reserve_path(path, details)
|
37
|
+
put_json!("/paths#{path}", details)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def get_json(path)
|
43
|
+
response = RestClient.get(@base_url.merge(path).to_s)
|
44
|
+
Response.new(response.code, response.body)
|
45
|
+
rescue RestClient::ResourceNotFound, RestClient::Gone
|
46
|
+
nil
|
47
|
+
rescue RestClient::Exception => e
|
48
|
+
raise Errors.create_for(e)
|
49
|
+
end
|
50
|
+
|
51
|
+
def put_json!(path, data)
|
52
|
+
json = MultiJson.dump(data)
|
53
|
+
response = RestClient.put(@base_url.merge(path).to_s, json, {:content_type => 'application/json'})
|
54
|
+
Response.new(response.code, response.body)
|
55
|
+
rescue RestClient::Exception => e
|
56
|
+
raise Errors.create_for(e)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, make a
|
10
|
+
# separate helper file that requires this one and then use it only in the specs
|
11
|
+
# that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
|
18
|
+
require 'webmock/rspec'
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
# These two settings work together to allow you to limit a spec run
|
22
|
+
# to individual examples or groups you care about by tagging them with
|
23
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
24
|
+
# get run.
|
25
|
+
config.filter_run :focus
|
26
|
+
config.run_all_when_everything_filtered = true
|
27
|
+
|
28
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
29
|
+
# file, and it's useful to allow more verbose output when running an
|
30
|
+
# individual spec file.
|
31
|
+
if config.files_to_run.one?
|
32
|
+
# Use the documentation formatter for detailed output,
|
33
|
+
# unless a formatter has already been configured
|
34
|
+
# (e.g. via a command-line flag).
|
35
|
+
config.default_formatter = 'doc'
|
36
|
+
end
|
37
|
+
|
38
|
+
# Print the 10 slowest examples and example groups at the
|
39
|
+
# end of the spec run, to help surface which specs are running
|
40
|
+
# particularly slow.
|
41
|
+
#config.profile_examples = 10
|
42
|
+
|
43
|
+
# Run specs in random order to surface order dependencies. If you find an
|
44
|
+
# order dependency and want to debug it, you can fix the order by providing
|
45
|
+
# the seed, which is printed after each run.
|
46
|
+
# --seed 1234
|
47
|
+
config.order = :random
|
48
|
+
|
49
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
50
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
51
|
+
# test failures related to randomization by passing the same `--seed` value
|
52
|
+
# as the one that triggered the failure.
|
53
|
+
Kernel.srand config.seed
|
54
|
+
|
55
|
+
# rspec-expectations config goes here. You can use an alternate
|
56
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
57
|
+
# assertions if you prefer.
|
58
|
+
config.expect_with :rspec do |expectations|
|
59
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
60
|
+
# For more details, see:
|
61
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
62
|
+
expectations.syntax = :expect
|
63
|
+
end
|
64
|
+
|
65
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
66
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
67
|
+
config.mock_with :rspec do |mocks|
|
68
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
69
|
+
# For more details, see:
|
70
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
71
|
+
mocks.syntax = :expect
|
72
|
+
|
73
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
74
|
+
# a real object. This is generally recommended.
|
75
|
+
mocks.verify_partial_doubles = true
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
|
2
|
+
require "govuk/client/url_arbiter"
|
3
|
+
require "govuk/client/test_helpers/url_arbiter"
|
4
|
+
|
5
|
+
describe GOVUK::Client::URLArbiter do
|
6
|
+
include GOVUK::Client::TestHelpers::URLArbiter
|
7
|
+
|
8
|
+
let(:base_url) { "http://url-arbiter.example.com" }
|
9
|
+
let(:client) { GOVUK::Client::URLArbiter.new(base_url) }
|
10
|
+
|
11
|
+
describe "fetching details of a reserved path" do
|
12
|
+
it "should return the details as a hash" do
|
13
|
+
data = url_arbiter_data_for("/foo/bar")
|
14
|
+
stub_request(:get, "#{base_url}/paths/foo/bar").
|
15
|
+
to_return(:status => 200, :body => data.to_json, :headers => {'Content-Type' => 'application/json'})
|
16
|
+
|
17
|
+
response = client.path("/foo/bar")
|
18
|
+
expect(response).to be_a(GOVUK::Client::Response)
|
19
|
+
expect(response).to eq(data)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return nil on 404" do
|
23
|
+
stub_request(:get, "#{base_url}/paths/foo/bar").
|
24
|
+
to_return(:status => 404)
|
25
|
+
|
26
|
+
response = client.path("/foo/bar")
|
27
|
+
expect(response).to be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return nil on 410" do
|
31
|
+
stub_request(:get, "#{base_url}/paths/foo/bar").
|
32
|
+
to_return(:status => 410)
|
33
|
+
|
34
|
+
response = client.path("/foo/bar")
|
35
|
+
expect(response).to be_nil
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should raise an exception on other HTTP errors" do
|
39
|
+
stub_request(:get, "#{base_url}/paths/foo/bar").
|
40
|
+
to_return(:status => 500, :body => "Computer says no!")
|
41
|
+
|
42
|
+
expect {
|
43
|
+
response = client.path("/foo/bar")
|
44
|
+
}.to raise_error(GOVUK::Client::Errors::HTTPError)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should raise a timeout exception on timeouts" do
|
48
|
+
stub_request(:get, "#{base_url}/paths/foo/bar").to_timeout
|
49
|
+
|
50
|
+
expect {
|
51
|
+
response = client.path("/foo/bar")
|
52
|
+
}.to raise_error(GOVUK::Client::Errors::Timeout)
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "reserving a path" do
|
58
|
+
it "should reserve the path and return the details as a hash" do
|
59
|
+
data = url_arbiter_data_for("/foo/bar")
|
60
|
+
stub_request(:put, "#{base_url}/paths/foo/bar").
|
61
|
+
with(:body => {"publishing_app" => "foo_publisher"}, :headers => {"Content-Type" => 'application/json'}).
|
62
|
+
to_return(:status => 201, :body => data.to_json, :headers => {'Content-Type' => 'application/json'})
|
63
|
+
|
64
|
+
response = client.reserve_path("/foo/bar", "publishing_app" => "foo_publisher")
|
65
|
+
expect(response).to be_a(GOVUK::Client::Response)
|
66
|
+
expect(response.code).to eq(201)
|
67
|
+
expect(response).to eq(data)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should raise a conflict error if the path is already reserved" do
|
71
|
+
data = url_arbiter_data_for("/foo/bar").merge({
|
72
|
+
"errors" => {"base" => ["is already reserved by the 'bar_publisher' app"]},
|
73
|
+
})
|
74
|
+
stub_request(:put, "#{base_url}/paths/foo/bar").
|
75
|
+
with(:body => {"publishing_app" => "foo_publisher"}, :headers => {"Content-Type" => 'application/json'}).
|
76
|
+
to_return(:status => 409, :body => data.to_json, :headers => {'Content-Type' => 'application/json'})
|
77
|
+
|
78
|
+
expect {
|
79
|
+
response = client.reserve_path("/foo/bar", "publishing_app" => "foo_publisher")
|
80
|
+
}.to raise_error(GOVUK::Client::Errors::Conflict) { |error|
|
81
|
+
expect(error.code).to eq(409)
|
82
|
+
expect(error.response).to eq(data)
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should raise an unprocessable entity error if there are validation errors" do
|
87
|
+
data = url_arbiter_data_for("/foo/bar").merge({
|
88
|
+
"errors" => {"publishing_app" => ["can't be blank"]},
|
89
|
+
})
|
90
|
+
stub_request(:put, "#{base_url}/paths/foo/bar").
|
91
|
+
with(:body => {"publishing_app" => ""}, :headers => {"Content-Type" => 'application/json'}).
|
92
|
+
to_return(:status => 422, :body => data.to_json, :headers => {'Content-Type' => 'application/json'})
|
93
|
+
|
94
|
+
expect {
|
95
|
+
response = client.reserve_path("/foo/bar", "publishing_app" => "")
|
96
|
+
}.to raise_error(GOVUK::Client::Errors::UnprocessableEntity) { |error|
|
97
|
+
expect(error.code).to eq(422)
|
98
|
+
expect(error.response).to eq(data)
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
# FIXME: extract this test into separate unit tests for the generic JSON
|
103
|
+
# client stuff when that is extracted into a separate gem.
|
104
|
+
it "should handle error responses that don't include a JSON body" do
|
105
|
+
stub_request(:put, "#{base_url}/paths/foo/bar").
|
106
|
+
to_return(:status => 500, :body => "Computer says no!")
|
107
|
+
|
108
|
+
expect {
|
109
|
+
response = client.reserve_path("/foo/bar", "publishing_app" => "foo_publisher")
|
110
|
+
}.to raise_error(GOVUK::Client::Errors::HTTPError) { |error|
|
111
|
+
expect(error.code).to eq(500)
|
112
|
+
expect(error.response.raw_body).to eq("Computer says no!")
|
113
|
+
expect(error.response).to eq({})
|
114
|
+
}
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
metadata
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: govuk-client-url_arbiter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alex Tomlins
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-09-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.6'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.6'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: multi_json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: plek
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.8'
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.8'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: gem_publisher
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '1.4'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '1.4'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: rspec
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '3.0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ~>
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '3.0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: webmock
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.18.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 1.18.0
|
126
|
+
description:
|
127
|
+
email:
|
128
|
+
- alex.tomlins@digital.cabinet-office.gov.uk
|
129
|
+
executables: []
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- .rspec
|
135
|
+
- .ruby-version
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- govuk-client-url_arbiter.gemspec
|
141
|
+
- jenkins.sh
|
142
|
+
- jenkins_branches.sh
|
143
|
+
- lib/govuk/client/errors.rb
|
144
|
+
- lib/govuk/client/response.rb
|
145
|
+
- lib/govuk/client/test_helpers/url_arbiter.rb
|
146
|
+
- lib/govuk/client/url_arbiter.rb
|
147
|
+
- lib/govuk/client/url_arbiter/version.rb
|
148
|
+
- spec/spec_helper.rb
|
149
|
+
- spec/url_arbiter_spec.rb
|
150
|
+
homepage: https://github.com/alphagov/govuk-client-url_arbiter
|
151
|
+
licenses:
|
152
|
+
- MIT
|
153
|
+
post_install_message:
|
154
|
+
rdoc_options: []
|
155
|
+
require_paths:
|
156
|
+
- lib
|
157
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
+
none: false
|
159
|
+
requirements:
|
160
|
+
- - ! '>='
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
segments:
|
164
|
+
- 0
|
165
|
+
hash: 1429177817123946626
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
none: false
|
168
|
+
requirements:
|
169
|
+
- - ! '>='
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
segments:
|
173
|
+
- 0
|
174
|
+
hash: 1429177817123946626
|
175
|
+
requirements: []
|
176
|
+
rubyforge_project:
|
177
|
+
rubygems_version: 1.8.23.2
|
178
|
+
signing_key:
|
179
|
+
specification_version: 3
|
180
|
+
summary: API client for the url-arbiter
|
181
|
+
test_files:
|
182
|
+
- spec/spec_helper.rb
|
183
|
+
- spec/url_arbiter_spec.rb
|