govuk-dummy_content_store 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +10 -0
- data/bin/dummy_content_store +7 -0
- data/config.ru +5 -2
- data/govuk-dummy_content_store.gemspec +1 -0
- data/lib/govuk/dummy_content_store.rb +1 -0
- data/lib/govuk/dummy_content_store/content.rb +16 -1
- data/lib/govuk/dummy_content_store/live_repository.rb +25 -0
- data/lib/govuk/dummy_content_store/version.rb +1 -1
- data/spec/integration/dummy_content_store_spec.rb +24 -0
- data/spec/spec_helper.rb +12 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3df0103431cbcbe787a7f8f0b1809fbd6e8ff281
|
4
|
+
data.tar.gz: 067f997fbe692d9cab432cf0cb710b6517ac75aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f66a5d0e1a5f07de603ddd8310b8b479a41e5d5bfc9228c982871ef2e8bad6d6559431389817211d7eb34d10e4e01777c56cd28b8e3f574022c0d026bed6b6e
|
7
|
+
data.tar.gz: 19f5ff83592c91e7901f2271da16ebf454208d8d6bb496ad8be492dccf67d4a2ad77967f7575d842e8823947925ffff13ab28ff9721b3fd466dca1121d603a10
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -22,6 +22,16 @@ It will look for the schema files in the following locations:
|
|
22
22
|
2. in the current directory
|
23
23
|
3. at the path specified in the first argument
|
24
24
|
|
25
|
+
If a fallback url is specified on startup:
|
26
|
+
|
27
|
+
$ dummy_content_store -u https://www.gov.uk/api/content
|
28
|
+
|
29
|
+
or
|
30
|
+
|
31
|
+
$ DUMMY_CONTENT_STORE_FALLBACK_URL=https://www.gov.uk/api/content dummy_content_store
|
32
|
+
|
33
|
+
it will look also for live contents at the specified url, if the example content is not found.
|
34
|
+
|
25
35
|
## Configuration
|
26
36
|
|
27
37
|
By default the dummy content store runs on port 3068 which is the same port as
|
data/bin/dummy_content_store
CHANGED
@@ -10,6 +10,13 @@ OptionParser.new do |opts|
|
|
10
10
|
puts opts
|
11
11
|
exit
|
12
12
|
end
|
13
|
+
|
14
|
+
opts.on('-u', '--url URL', "Fallback URL for non available examples") do |url|
|
15
|
+
# e.g. https://www.gov.uk/api/content
|
16
|
+
ENV["DUMMY_CONTENT_STORE_FALLBACK_URL"] = url
|
17
|
+
puts "fallback URL = #{url}"
|
18
|
+
end
|
19
|
+
|
13
20
|
end.parse!
|
14
21
|
|
15
22
|
if ARGV.size == 1
|
data/config.ru
CHANGED
@@ -2,7 +2,10 @@ $LOAD_PATH << File.dirname(__FILE__) + "/lib"
|
|
2
2
|
require 'govuk/dummy_content_store'
|
3
3
|
|
4
4
|
govuk_content_schemas_path = ENV.fetch('GOVUK_CONTENT_SCHEMAS_PATH', ".")
|
5
|
+
govuk_live_content_path = ENV['DUMMY_CONTENT_STORE_FALLBACK_URL']
|
6
|
+
|
5
7
|
repository = Govuk::DummyContentStore::ExampleRepository.new(govuk_content_schemas_path)
|
8
|
+
live_repository = Govuk::DummyContentStore::LiveRepository.new(govuk_live_content_path) if govuk_live_content_path
|
6
9
|
|
7
10
|
map '/' do
|
8
11
|
run Govuk::DummyContentStore::Index.new(repository)
|
@@ -18,10 +21,10 @@ end
|
|
18
21
|
|
19
22
|
# Serve examples
|
20
23
|
map '/content' do
|
21
|
-
run Govuk::DummyContentStore::Content.new(repository)
|
24
|
+
run Govuk::DummyContentStore::Content.new(repository, live_repository)
|
22
25
|
end
|
23
26
|
map '/api/content' do
|
24
|
-
run Govuk::DummyContentStore::Content.new(repository)
|
27
|
+
run Govuk::DummyContentStore::Content.new(repository, live_repository)
|
25
28
|
end
|
26
29
|
|
27
30
|
|
@@ -5,6 +5,7 @@ module Govuk
|
|
5
5
|
autoload :Content, "govuk/dummy_content_store/content"
|
6
6
|
autoload :ExampleContentItem, "govuk/dummy_content_store/example_content_item"
|
7
7
|
autoload :ExampleRepository, "govuk/dummy_content_store/example_repository"
|
8
|
+
autoload :LiveRepository, "govuk/dummy_content_store/live_repository"
|
8
9
|
autoload :Index, "govuk/dummy_content_store/index"
|
9
10
|
end
|
10
11
|
end
|
@@ -1,20 +1,25 @@
|
|
1
1
|
require 'pathname'
|
2
2
|
require 'json'
|
3
3
|
require 'govuk/dummy_content_store'
|
4
|
+
require 'net/http'
|
4
5
|
|
5
6
|
module Govuk
|
6
7
|
module DummyContentStore
|
7
8
|
class Content
|
8
9
|
attr_reader :repository
|
10
|
+
attr_reader :live_repository
|
9
11
|
|
10
|
-
def initialize(repository)
|
12
|
+
def initialize(repository, live_repository = nil)
|
11
13
|
@repository = repository
|
14
|
+
@live_repository = live_repository
|
12
15
|
end
|
13
16
|
|
14
17
|
def call(env)
|
15
18
|
example = repository.find_by_base_path(env["PATH_INFO"])
|
16
19
|
if example
|
17
20
|
present_example(example)
|
21
|
+
elsif live_repository && res = live_repository.find_by_base_path(env["PATH_INFO"])
|
22
|
+
present_live(res.body)
|
18
23
|
else
|
19
24
|
present_not_found
|
20
25
|
end
|
@@ -31,6 +36,16 @@ module Govuk
|
|
31
36
|
[200, headers, [example.raw_data]]
|
32
37
|
end
|
33
38
|
|
39
|
+
def present_live(body)
|
40
|
+
headers = {
|
41
|
+
'Content-Type' => 'application/json; charset=utf-8',
|
42
|
+
'Content-Length' => body.size.to_s,
|
43
|
+
'Cache-control' => 'no-cache'
|
44
|
+
}
|
45
|
+
|
46
|
+
[200, headers, [body]]
|
47
|
+
end
|
48
|
+
|
34
49
|
def present_not_found
|
35
50
|
body = 'Not found'
|
36
51
|
headers = {
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Govuk
|
2
|
+
module DummyContentStore
|
3
|
+
class LiveRepository
|
4
|
+
attr_reader :live_content_path
|
5
|
+
|
6
|
+
def initialize(live_content_path)
|
7
|
+
@live_content_path = live_content_path
|
8
|
+
end
|
9
|
+
|
10
|
+
def find_by_base_path(base_path)
|
11
|
+
res = http_get(base_path)
|
12
|
+
res.code == "404" ? nil : res
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def http_get(base_path)
|
17
|
+
url = URI.parse(live_content_path + base_path)
|
18
|
+
req = Net::HTTP::Get.new(url.to_s)
|
19
|
+
http = Net::HTTP.new(url.host, url.port)
|
20
|
+
http.use_ssl = true if url.scheme == "https"
|
21
|
+
http.request(req)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -65,4 +65,28 @@ RSpec.describe "Dummy content store rack application" do
|
|
65
65
|
expect(last_response).to be_ok
|
66
66
|
end
|
67
67
|
end
|
68
|
+
|
69
|
+
|
70
|
+
context "a fallback url is set" do
|
71
|
+
before(:each) { ENV['DUMMY_CONTENT_STORE_FALLBACK_URL'] = "https://www.gov.uk/api/content" }
|
72
|
+
|
73
|
+
it "serves the live content if the example is not available" do
|
74
|
+
live_file_base_path = "/available"
|
75
|
+
live_content_body = "stubbed response"
|
76
|
+
|
77
|
+
get "/api/content#{live_file_base_path}"
|
78
|
+
expect(last_response).to be_ok
|
79
|
+
expect(last_response.body).to eq(live_content_body)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "responds not found if both live and example are not available" do
|
83
|
+
live_file_base_path = "/not-available"
|
84
|
+
not_found_body = "Not found"
|
85
|
+
|
86
|
+
get "/api/content#{live_file_base_path}"
|
87
|
+
expect(last_response).not_to be_ok
|
88
|
+
expect(last_response.status).to eq(404)
|
89
|
+
expect(last_response.body).to eq(not_found_body)
|
90
|
+
end
|
91
|
+
end
|
68
92
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'webmock/rspec'
|
2
|
+
|
1
3
|
RSpec.configure do |config|
|
2
4
|
config.expect_with :rspec do |expectations|
|
3
5
|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
@@ -18,4 +20,14 @@ RSpec.configure do |config|
|
|
18
20
|
config.order = :random
|
19
21
|
|
20
22
|
Kernel.srand config.seed
|
23
|
+
|
24
|
+
config.before(:each) do
|
25
|
+
stub_request(:get, "https://www.gov.uk/api/content/available").
|
26
|
+
with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
27
|
+
to_return(status: 200, body: "stubbed response", headers: {})
|
28
|
+
|
29
|
+
stub_request(:get, "https://www.gov.uk/api/content/not-available").
|
30
|
+
with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
31
|
+
to_return(status: 404, body: "not found", headers: {})
|
32
|
+
end
|
21
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: govuk-dummy_content_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heath
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -122,6 +122,20 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: webmock
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
125
139
|
description: ''
|
126
140
|
email:
|
127
141
|
- david.heath@digital.cabinet-office.gov.uk
|
@@ -147,6 +161,7 @@ files:
|
|
147
161
|
- lib/govuk/dummy_content_store/example_content_item.rb
|
148
162
|
- lib/govuk/dummy_content_store/example_repository.rb
|
149
163
|
- lib/govuk/dummy_content_store/index.rb
|
164
|
+
- lib/govuk/dummy_content_store/live_repository.rb
|
150
165
|
- lib/govuk/dummy_content_store/templates/index.html.erb
|
151
166
|
- lib/govuk/dummy_content_store/version.rb
|
152
167
|
- public/styles.css
|