slimmer 1.1.43 → 1.1.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/slimmer/app.rb +34 -68
- data/lib/slimmer/google_analytics_configurator.rb +2 -6
- data/lib/slimmer/search_path_setter.rb +19 -0
- data/lib/slimmer/skin.rb +35 -77
- data/lib/slimmer/version.rb +1 -1
- data/lib/slimmer.rb +2 -0
- data/test/search_path_setter_test.rb +39 -0
- data/test/skin_test.rb +17 -5
- data/test/test_helper.rb +4 -4
- data/test/typical_usage_test.rb +20 -12
- metadata +7 -4
data/lib/slimmer/app.rb
CHANGED
|
@@ -25,46 +25,35 @@ module Slimmer
|
|
|
25
25
|
|
|
26
26
|
def call(env)
|
|
27
27
|
logger.debug "Slimmer: capturing response"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
elsif skip_slimmer_header?(backend_response)
|
|
34
|
-
logger.debug "Slimmer: Asked to skip slimmer via HTTP header"
|
|
35
|
-
return backend_response
|
|
28
|
+
status, headers, body = @app.call(env)
|
|
29
|
+
response = Rack::Response.new(body, status, headers)
|
|
30
|
+
|
|
31
|
+
if response_can_be_rewritten?(response) && !skip_slimmer?(env, response)
|
|
32
|
+
rewrite_response(env, response)
|
|
36
33
|
else
|
|
37
|
-
|
|
34
|
+
[status, headers, body]
|
|
38
35
|
end
|
|
39
36
|
end
|
|
40
|
-
|
|
37
|
+
|
|
38
|
+
def response_can_be_rewritten?(response)
|
|
39
|
+
response.content_type =~ /text\/html/ && ![301, 302, 304].include?(response.status)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def skip_slimmer?(env, response)
|
|
43
|
+
(in_development? && skip_slimmer_param?(env)) || skip_slimmer_header?(response)
|
|
44
|
+
end
|
|
45
|
+
|
|
41
46
|
def in_development?
|
|
42
47
|
ENV['RAILS_ENV'] == 'development'
|
|
43
48
|
end
|
|
44
|
-
|
|
49
|
+
|
|
45
50
|
def skip_slimmer_param?(env)
|
|
46
51
|
skip = Rack::Request.new(env).params['skip_slimmer']
|
|
47
52
|
skip and skip.to_i > 0
|
|
48
53
|
end
|
|
49
|
-
|
|
50
|
-
def skip_slimmer_header?(backend_response)
|
|
51
|
-
!! backend_response[1][SKIP_HEADER]
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def on_success(source_request, request, body)
|
|
55
|
-
@skin.success(source_request, request, body)
|
|
56
|
-
end
|
|
57
54
|
|
|
58
|
-
def
|
|
59
|
-
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def on_error(request, status, body)
|
|
63
|
-
@skin.error(request, '500', body)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def on_404(request,body)
|
|
67
|
-
@skin.error(request, '404', body)
|
|
55
|
+
def skip_slimmer_header?(response)
|
|
56
|
+
!!response.headers[SKIP_HEADER]
|
|
68
57
|
end
|
|
69
58
|
|
|
70
59
|
def s(body)
|
|
@@ -73,57 +62,34 @@ module Slimmer
|
|
|
73
62
|
body.each {|a| b << a }
|
|
74
63
|
b
|
|
75
64
|
end
|
|
76
|
-
|
|
65
|
+
|
|
77
66
|
def content_length(rewritten_body)
|
|
78
67
|
size = 0
|
|
79
68
|
rewritten_body.each { |part| size += part.bytesize }
|
|
80
69
|
size.to_s
|
|
81
70
|
end
|
|
82
71
|
|
|
83
|
-
def rewrite_response(env,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
logger.debug "Slimmer: Content-Type: #{content_type}"
|
|
92
|
-
if content_type =~ /text\/html/
|
|
93
|
-
logger.debug "Slimmer: Status code = #{status}"
|
|
94
|
-
case status.to_i
|
|
95
|
-
when 200
|
|
96
|
-
logger.debug "Slimmer: I will rewrite this request"
|
|
97
|
-
logger.debug "Slimmer: #{TEMPLATE_HEADER} = #{headers[TEMPLATE_HEADER].inspect}"
|
|
98
|
-
logger.debug "Slimmer: Request path = #{source_request.path.inspect}"
|
|
99
|
-
if headers[TEMPLATE_HEADER] == 'admin' || source_request.path =~ /^\/admin(\/|$)/
|
|
100
|
-
logger.debug "Slimmer: Rewriting this request as an admin request"
|
|
101
|
-
rewritten_body = admin(request,s(app_body))
|
|
102
|
-
else
|
|
103
|
-
logger.debug "Slimmer: Rewriting this request as a public request"
|
|
104
|
-
rewritten_body = on_success(source_request, request, s(app_body))
|
|
105
|
-
end
|
|
106
|
-
when 301, 302, 304
|
|
107
|
-
logger.debug "Slimmer: I will not rewrite this request"
|
|
108
|
-
rewritten_body = app_body
|
|
109
|
-
when 404
|
|
110
|
-
logger.debug "Slimmer: Rewriting this request as a 404 error"
|
|
111
|
-
rewritten_body = on_404(request,s(app_body))
|
|
72
|
+
def rewrite_response(env, response)
|
|
73
|
+
request = Rack::Request.new(env)
|
|
74
|
+
|
|
75
|
+
rewritten_body = case response.status
|
|
76
|
+
when 200
|
|
77
|
+
if response.headers[TEMPLATE_HEADER] == 'admin' || request.path =~ /^\/admin(\/|$)/
|
|
78
|
+
@skin.admin s(response.body)
|
|
112
79
|
else
|
|
113
|
-
|
|
114
|
-
rewritten_body = on_error(request,status,s(app_body))
|
|
80
|
+
@skin.success request, response, s(response.body)
|
|
115
81
|
end
|
|
82
|
+
when 404
|
|
83
|
+
@skin.error '404', s(response.body)
|
|
116
84
|
else
|
|
117
|
-
|
|
118
|
-
rewritten_body = app_body
|
|
85
|
+
@skin.error '500', s(response.body)
|
|
119
86
|
end
|
|
120
|
-
rewritten_body = [rewritten_body] unless rewritten_body.respond_to?(:each)
|
|
121
87
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
[status, headers, rewritten_body]
|
|
88
|
+
rewritten_body = [rewritten_body] unless rewritten_body.respond_to?(:each)
|
|
89
|
+
response.body = rewritten_body
|
|
90
|
+
response.headers['Content-Length'] = content_length(response.body)
|
|
126
91
|
|
|
92
|
+
response.finish
|
|
127
93
|
rescue GdsApi::TimedOutException
|
|
128
94
|
[503, {"Content-Type" => "text/plain"}, ["GDS API request timed out."]]
|
|
129
95
|
end
|
|
@@ -11,8 +11,8 @@ module Slimmer
|
|
|
11
11
|
"ResultCount" => "X-SLIMMER-RESULT-COUNT"
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
def initialize(
|
|
15
|
-
@headers =
|
|
14
|
+
def initialize(response)
|
|
15
|
+
@headers = response.headers
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def filter(src, dest)
|
|
@@ -26,10 +26,6 @@ module Slimmer
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
private
|
|
29
|
-
def normalise_headers(headers)
|
|
30
|
-
Hash[headers.map { |k, v| [k.upcase, v] }]
|
|
31
|
-
end
|
|
32
|
-
|
|
33
29
|
def set_custom_var(slot, name, value)
|
|
34
30
|
return nil unless value
|
|
35
31
|
value.downcase!
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'gds_api/helpers'
|
|
2
|
+
|
|
3
|
+
module Slimmer
|
|
4
|
+
class SearchPathSetter
|
|
5
|
+
def initialize(response)
|
|
6
|
+
@response = response
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def filter(content_document, page_template)
|
|
10
|
+
if search_scope && page_template.at_css('form#search')
|
|
11
|
+
page_template.at_css('form#search').attributes["action"].value = search_scope
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def search_scope
|
|
16
|
+
@response.headers[SEARCH_PATH_HEADER]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
data/lib/slimmer/skin.rb
CHANGED
|
@@ -1,78 +1,35 @@
|
|
|
1
1
|
module Slimmer
|
|
2
2
|
class Skin
|
|
3
|
-
attr_accessor :use_cache
|
|
4
|
-
private :use_cache=, :use_cache
|
|
5
|
-
|
|
6
|
-
attr_accessor :templated_cache
|
|
7
|
-
private :templated_cache=, :templated_cache
|
|
8
|
-
|
|
9
|
-
attr_accessor :asset_host
|
|
10
|
-
private :asset_host=, :asset_host
|
|
11
|
-
|
|
12
|
-
attr_accessor :prefix
|
|
13
|
-
private :prefix=, :prefix
|
|
14
|
-
|
|
15
|
-
attr_accessor :logger
|
|
16
|
-
private :logger=, :logger
|
|
17
|
-
|
|
18
|
-
attr_accessor :strict
|
|
19
|
-
private :strict=, :strict
|
|
20
|
-
|
|
21
|
-
attr_accessor :options
|
|
22
|
-
private :options=, :options
|
|
3
|
+
attr_accessor :use_cache, :template_cache, :asset_host, :prefix, :logger, :strict, :options
|
|
23
4
|
|
|
24
5
|
# TODO: Extract the cache to something we can pass in instead of using
|
|
25
6
|
# true/false and an in-memory cache.
|
|
26
7
|
def initialize options = {}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
8
|
+
@options = options
|
|
9
|
+
@asset_host = options[:asset_host]
|
|
10
|
+
@template_cache = {}
|
|
11
|
+
@prefix = options[:prefix]
|
|
12
|
+
@use_cache = options[:use_cache] || false
|
|
13
|
+
@logger = options[:logger] || NullLogger.instance
|
|
14
|
+
@strict = options[:strict] || (%w{development test}.include?(ENV['RACK_ENV']))
|
|
34
15
|
end
|
|
35
16
|
|
|
36
17
|
def template(template_name)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
def template_cached? name
|
|
44
|
-
logger.debug "Slimmer: Checking cache for template '#{name}'"
|
|
45
|
-
cached = !cached_template(name).nil?
|
|
46
|
-
logger.debug "Slimmer: Cache hit = #{cached}"
|
|
47
|
-
cached
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
def cached_template name
|
|
51
|
-
logger.debug "Slimmer: Trying to load cached template '#{name}'"
|
|
52
|
-
templated_cache[name]
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def cache name, template
|
|
56
|
-
logger.debug "Slimmer: Asked to cache '#{name}'. use_cache = #{use_cache}"
|
|
57
|
-
return unless use_cache
|
|
58
|
-
logger.debug "Slimmer: performing caching"
|
|
59
|
-
templated_cache[name] = template
|
|
18
|
+
if use_cache
|
|
19
|
+
template_cache[template_name] ||= load_template(template_name)
|
|
20
|
+
else
|
|
21
|
+
load_template(template_name)
|
|
22
|
+
end
|
|
60
23
|
end
|
|
61
24
|
|
|
62
|
-
def load_template
|
|
63
|
-
|
|
64
|
-
url = template_url template_name
|
|
65
|
-
logger.debug "Slimmer: template lives at '#{url}'"
|
|
25
|
+
def load_template(template_name)
|
|
26
|
+
url = template_url(template_name)
|
|
66
27
|
source = open(url, "r:UTF-8", :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
|
|
67
28
|
if template_name =~ /\.raw/
|
|
68
|
-
logger.debug "Slimmer: reading the raw template"
|
|
69
29
|
template = source
|
|
70
30
|
else
|
|
71
|
-
logger.debug "Slimmer: Evaluating the template as ERB"
|
|
72
31
|
template = ERB.new(source).result binding
|
|
73
32
|
end
|
|
74
|
-
cache template_name, template
|
|
75
|
-
logger.debug "Slimmer: Returning evaluated template"
|
|
76
33
|
template
|
|
77
34
|
rescue OpenURI::HTTPError => e
|
|
78
35
|
raise TemplateNotFoundException, "Unable to fetch: '#{template_name}' from '#{url}' because #{e}", caller
|
|
@@ -82,23 +39,16 @@ module Slimmer
|
|
|
82
39
|
raise CouldNotRetrieveTemplate, "Unable to fetch: '#{template_name}' from '#{url}' because #{e}", caller
|
|
83
40
|
end
|
|
84
41
|
|
|
85
|
-
def template_url
|
|
42
|
+
def template_url(template_name)
|
|
86
43
|
host = asset_host.dup
|
|
87
44
|
host += '/' unless host =~ /\/$/
|
|
88
45
|
"#{host}templates/#{template_name}.html.erb"
|
|
89
46
|
end
|
|
90
47
|
|
|
91
|
-
def error(request, template_name, body)
|
|
92
|
-
processors = [
|
|
93
|
-
TitleInserter.new()
|
|
94
|
-
]
|
|
95
|
-
process(processors, body, template(template_name))
|
|
96
|
-
end
|
|
97
|
-
|
|
98
48
|
def report_parse_errors_if_strict!(nokogiri_doc, description_for_error_message)
|
|
99
49
|
nokogiri_doc
|
|
100
50
|
end
|
|
101
|
-
|
|
51
|
+
|
|
102
52
|
def parse_html(html, description_for_error_message)
|
|
103
53
|
doc = Nokogiri::HTML.parse(html)
|
|
104
54
|
if strict
|
|
@@ -114,7 +64,7 @@ module Slimmer
|
|
|
114
64
|
|
|
115
65
|
doc
|
|
116
66
|
end
|
|
117
|
-
|
|
67
|
+
|
|
118
68
|
def context(html, error)
|
|
119
69
|
context_size = 5
|
|
120
70
|
lines = [""] + html.split("\n")
|
|
@@ -125,7 +75,7 @@ module Slimmer
|
|
|
125
75
|
context.insert(context_size, marker)
|
|
126
76
|
context.join("\n")
|
|
127
77
|
end
|
|
128
|
-
|
|
78
|
+
|
|
129
79
|
def ignorable?(error)
|
|
130
80
|
ignorable_codes = [801]
|
|
131
81
|
ignorable_codes.include?(error.code) || error.message.match(/Element script embeds close tag/) || error.message.match(/Unexpected end tag : noscript/)
|
|
@@ -153,12 +103,12 @@ module Slimmer
|
|
|
153
103
|
end_time = Time.now
|
|
154
104
|
logger.debug "Slimmer: Skinning process completed at #{end_time} (#{end_time - start_time}s)"
|
|
155
105
|
|
|
156
|
-
# this is a horrible fix to Nokogiri removing the closing </noscript> tag required by Google Website Optimizer.
|
|
106
|
+
# this is a horrible fix to Nokogiri removing the closing </noscript> tag required by Google Website Optimizer.
|
|
157
107
|
# http://www.google.com/support/websiteoptimizer/bin/answer.py?hl=en_us&answer=64418
|
|
158
108
|
dest.to_html.sub(/<noscript rel=("|')placeholder("|')>/, "")
|
|
159
109
|
end
|
|
160
110
|
|
|
161
|
-
def admin(
|
|
111
|
+
def admin(body)
|
|
162
112
|
processors = [
|
|
163
113
|
TitleInserter.new(),
|
|
164
114
|
TagMover.new(),
|
|
@@ -167,10 +117,10 @@ module Slimmer
|
|
|
167
117
|
BodyInserter.new(),
|
|
168
118
|
BodyClassCopier.new,
|
|
169
119
|
]
|
|
170
|
-
process(processors,body,template('admin'))
|
|
120
|
+
process(processors, body, template('admin'))
|
|
171
121
|
end
|
|
172
122
|
|
|
173
|
-
def success(source_request,
|
|
123
|
+
def success(source_request, response, body)
|
|
174
124
|
processors = [
|
|
175
125
|
TitleInserter.new(),
|
|
176
126
|
TagMover.new(),
|
|
@@ -178,12 +128,20 @@ module Slimmer
|
|
|
178
128
|
BodyClassCopier.new,
|
|
179
129
|
HeaderContextInserter.new(),
|
|
180
130
|
SectionInserter.new(),
|
|
181
|
-
GoogleAnalyticsConfigurator.new(
|
|
131
|
+
GoogleAnalyticsConfigurator.new(response),
|
|
132
|
+
SearchPathSetter.new(response),
|
|
182
133
|
RelatedItemsInserter.new(template('related.raw'), source_request),
|
|
183
134
|
]
|
|
184
|
-
|
|
185
|
-
template_name =
|
|
186
|
-
process(processors,body,template(template_name))
|
|
135
|
+
|
|
136
|
+
template_name = response.headers[TEMPLATE_HEADER] || 'wrapper'
|
|
137
|
+
process(processors, body, template(template_name))
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def error(template_name, body)
|
|
141
|
+
processors = [
|
|
142
|
+
TitleInserter.new()
|
|
143
|
+
]
|
|
144
|
+
process(processors, body, template(template_name))
|
|
187
145
|
end
|
|
188
146
|
end
|
|
189
147
|
end
|
data/lib/slimmer/version.rb
CHANGED
data/lib/slimmer.rb
CHANGED
|
@@ -10,6 +10,7 @@ require 'slimmer/railtie' if defined? Rails
|
|
|
10
10
|
module Slimmer
|
|
11
11
|
TEMPLATE_HEADER = 'X-Slimmer-Template'
|
|
12
12
|
SKIP_HEADER = 'X-Slimmer-Skip'
|
|
13
|
+
SEARCH_PATH_HEADER = 'X-Slimmer-Search-Path'
|
|
13
14
|
|
|
14
15
|
autoload :Version, 'slimmer/version'
|
|
15
16
|
autoload :Railtie, 'slimmer/railtie'
|
|
@@ -28,6 +29,7 @@ module Slimmer
|
|
|
28
29
|
autoload :HeaderContextInserter, 'slimmer/header_context_inserter'
|
|
29
30
|
autoload :GoogleAnalyticsConfigurator, 'slimmer/google_analytics_configurator'
|
|
30
31
|
autoload :RelatedItemsInserter, 'slimmer/related_items_inserter'
|
|
32
|
+
autoload :SearchPathSetter, 'slimmer/search_path_setter'
|
|
31
33
|
|
|
32
34
|
class TemplateNotFoundException < StandardError; end
|
|
33
35
|
class CouldNotRetrieveTemplate < StandardError; end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
|
|
3
|
+
module GoogleAnalyticsTest
|
|
4
|
+
|
|
5
|
+
DOCUMENT_WITH_SEARCH = <<-END
|
|
6
|
+
<html>
|
|
7
|
+
<head>
|
|
8
|
+
</head>
|
|
9
|
+
<body class="body_class">
|
|
10
|
+
<div id="wrapper">
|
|
11
|
+
<form id="search" action="/path/to/search">
|
|
12
|
+
</form>
|
|
13
|
+
</div>
|
|
14
|
+
</body>
|
|
15
|
+
</html>
|
|
16
|
+
END
|
|
17
|
+
|
|
18
|
+
class WithHeaderTest < SlimmerIntegrationTest
|
|
19
|
+
headers = {
|
|
20
|
+
"X-Slimmer-Search-Path" => "/specialist/search",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
given_response 200, DOCUMENT_WITH_SEARCH, headers
|
|
24
|
+
|
|
25
|
+
def test_should_rewrite_search_action
|
|
26
|
+
search_action = Nokogiri::HTML.parse(last_response.body).at_css('#search')["action"]
|
|
27
|
+
assert_equal "/specialist/search", search_action
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class WithoutHeaderTest < SlimmerIntegrationTest
|
|
32
|
+
given_response 200, DOCUMENT_WITH_SEARCH, {}
|
|
33
|
+
|
|
34
|
+
def test_should_leave_original_search_action
|
|
35
|
+
search_action = Nokogiri::HTML.parse(last_response.body).at_css('#search')["action"]
|
|
36
|
+
assert_equal "/path/to/search", search_action
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
data/test/skin_test.rb
CHANGED
|
@@ -6,18 +6,30 @@ class SkinTest < MiniTest::Unit::TestCase
|
|
|
6
6
|
expected_url = "http://example.local/templates/example.html.erb"
|
|
7
7
|
stub_request(:get, expected_url).to_return :body => "<foo />"
|
|
8
8
|
|
|
9
|
-
template = skin.
|
|
9
|
+
template = skin.template 'example'
|
|
10
10
|
|
|
11
11
|
assert_requested :get, "http://example.local/templates/example.html.erb"
|
|
12
12
|
assert_equal "<foo />", template
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
+
def test_templates_can_be_cached
|
|
16
|
+
skin = Slimmer::Skin.new asset_host: "http://example.local/", use_cache: true
|
|
17
|
+
expected_url = "http://example.local/templates/example.html.erb"
|
|
18
|
+
stub_request(:get, expected_url).to_return :body => "<foo />"
|
|
19
|
+
|
|
20
|
+
first_access = skin.template 'example'
|
|
21
|
+
second_access = skin.template 'example'
|
|
22
|
+
|
|
23
|
+
assert_requested :get, "http://example.local/templates/example.html.erb", times: 1
|
|
24
|
+
assert_same first_access, second_access
|
|
25
|
+
end
|
|
26
|
+
|
|
15
27
|
def test_should_interpolate_values_for_prefix
|
|
16
28
|
skin = Slimmer::Skin.new asset_host: "http://example.local/", use_cache: false, prefix: "this-is-the-prefix"
|
|
17
29
|
expected_url = "http://example.local/templates/example.html.erb"
|
|
18
30
|
stub_request(:get, expected_url).to_return :body => "<p><%= prefix %></p>"
|
|
19
31
|
|
|
20
|
-
template = skin.
|
|
32
|
+
template = skin.template 'example'
|
|
21
33
|
assert_equal "<p>this-is-the-prefix</p>", template
|
|
22
34
|
end
|
|
23
35
|
|
|
@@ -27,7 +39,7 @@ class SkinTest < MiniTest::Unit::TestCase
|
|
|
27
39
|
stub_request(:get, expected_url).to_return(:status => '404')
|
|
28
40
|
|
|
29
41
|
assert_raises(Slimmer::TemplateNotFoundException) do
|
|
30
|
-
skin.
|
|
42
|
+
skin.template 'example'
|
|
31
43
|
end
|
|
32
44
|
end
|
|
33
45
|
|
|
@@ -37,7 +49,7 @@ class SkinTest < MiniTest::Unit::TestCase
|
|
|
37
49
|
stub_request(:get, expected_url).to_raise(Errno::ECONNREFUSED)
|
|
38
50
|
|
|
39
51
|
assert_raises(Slimmer::CouldNotRetrieveTemplate) do
|
|
40
|
-
skin.
|
|
52
|
+
skin.template 'example'
|
|
41
53
|
end
|
|
42
54
|
end
|
|
43
55
|
|
|
@@ -47,7 +59,7 @@ class SkinTest < MiniTest::Unit::TestCase
|
|
|
47
59
|
stub_request(:get, expected_url).to_raise(SocketError)
|
|
48
60
|
|
|
49
61
|
assert_raises(Slimmer::CouldNotRetrieveTemplate) do
|
|
50
|
-
skin.
|
|
62
|
+
skin.template 'example'
|
|
51
63
|
end
|
|
52
64
|
end
|
|
53
65
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -30,7 +30,7 @@ class SlimmerIntegrationTest < MiniTest::Unit::TestCase
|
|
|
30
30
|
def self.given_response(code, body, headers={}, app_options={})
|
|
31
31
|
define_method(:app) do
|
|
32
32
|
inner_app = proc { |env|
|
|
33
|
-
[code,
|
|
33
|
+
[code, {"Content-Type" => "text/html"}.merge(headers), body]
|
|
34
34
|
}
|
|
35
35
|
Slimmer::App.new inner_app, {asset_host: "http://template.local"}.merge(app_options)
|
|
36
36
|
end
|
|
@@ -55,17 +55,17 @@ class SlimmerIntegrationTest < MiniTest::Unit::TestCase
|
|
|
55
55
|
setup_template if respond_to? :setup_template
|
|
56
56
|
fetch_page
|
|
57
57
|
end
|
|
58
|
-
|
|
58
|
+
|
|
59
59
|
def fetch_page
|
|
60
60
|
get "/"
|
|
61
61
|
end
|
|
62
|
-
|
|
62
|
+
|
|
63
63
|
def use_template(template_name)
|
|
64
64
|
template = File.read File.dirname(__FILE__) + "/fixtures/#{template_name}.html.erb"
|
|
65
65
|
stub_request(:get, "http://template.local/templates/#{template_name}.html.erb").
|
|
66
66
|
to_return(:body => template)
|
|
67
67
|
end
|
|
68
|
-
|
|
68
|
+
|
|
69
69
|
private
|
|
70
70
|
|
|
71
71
|
def assert_not_rendered_in_template(content)
|
data/test/typical_usage_test.rb
CHANGED
|
@@ -10,10 +10,10 @@ module TypicalUsage
|
|
|
10
10
|
assert_equal "Don't template me", last_response.body
|
|
11
11
|
end
|
|
12
12
|
end
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
class SkipUsingQueryParamTest < SlimmerIntegrationTest
|
|
15
15
|
given_response 200, %{<html><body><div id='unskinned'>Unskinned</div><div id="wrapper">Don't template me</div></body></html>}, {}
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
def fetch_page; end
|
|
18
18
|
|
|
19
19
|
def with_rails_env(new_env, &block)
|
|
@@ -24,7 +24,7 @@ module TypicalUsage
|
|
|
24
24
|
ENV['RAILS_ENV'] = old_env
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
|
-
|
|
27
|
+
|
|
28
28
|
def test_should_return_the_response_as_is_in_development
|
|
29
29
|
with_rails_env('development') do
|
|
30
30
|
get "/some-slug?skip_slimmer=1"
|
|
@@ -40,6 +40,14 @@ module TypicalUsage
|
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
+
class SkipNonHtmlResponse < SlimmerIntegrationTest
|
|
44
|
+
given_response 200, '{ "json" : "document" }', {'Content-Type' => 'application/json'}
|
|
45
|
+
|
|
46
|
+
def test_should_pass_through_non_html_response
|
|
47
|
+
assert_equal '{ "json" : "document" }', last_response.body
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
43
51
|
class ContentLengthTest < SlimmerIntegrationTest
|
|
44
52
|
given_response 200, %{
|
|
45
53
|
<html>
|
|
@@ -105,16 +113,16 @@ module TypicalUsage
|
|
|
105
113
|
def test_should_insert_meta_navigation_links_into_the_navigation
|
|
106
114
|
assert_rendered_in_template "nav[role=navigation] li a[href='/this_section']", "This section"
|
|
107
115
|
end
|
|
108
|
-
|
|
116
|
+
|
|
109
117
|
end
|
|
110
|
-
|
|
118
|
+
|
|
111
119
|
class ResponseWithRelatedItemsTest < SlimmerIntegrationTest
|
|
112
120
|
include GdsApi::TestHelpers::Panopticon
|
|
113
121
|
|
|
114
122
|
def setup
|
|
115
123
|
panopticon_has_metadata(
|
|
116
|
-
'slug' => 'some-slug',
|
|
117
|
-
'title' => 'Example document',
|
|
124
|
+
'slug' => 'some-slug',
|
|
125
|
+
'title' => 'Example document',
|
|
118
126
|
'related_items' => [
|
|
119
127
|
{
|
|
120
128
|
'artefact' => {
|
|
@@ -128,7 +136,7 @@ module TypicalUsage
|
|
|
128
136
|
super
|
|
129
137
|
end
|
|
130
138
|
end
|
|
131
|
-
|
|
139
|
+
|
|
132
140
|
class CitizenRelatedItemsTest < ResponseWithRelatedItemsTest
|
|
133
141
|
given_response 200, %{
|
|
134
142
|
<html>
|
|
@@ -137,11 +145,11 @@ module TypicalUsage
|
|
|
137
145
|
</body>
|
|
138
146
|
</html>
|
|
139
147
|
}, {}
|
|
140
|
-
|
|
148
|
+
|
|
141
149
|
def fetch_page
|
|
142
150
|
get "/some-slug"
|
|
143
151
|
end
|
|
144
|
-
|
|
152
|
+
|
|
145
153
|
def test_should_insert_related_items_block
|
|
146
154
|
assert_rendered_in_template "div.related nav li.guide a", "How to test computer software automatically & ensure that 2>1"
|
|
147
155
|
assert_rendered_in_template "div.related nav li.guide", %r{href="/how-to-test-computer-software-automatically"}
|
|
@@ -156,11 +164,11 @@ module TypicalUsage
|
|
|
156
164
|
</body>
|
|
157
165
|
</html>
|
|
158
166
|
}, {}
|
|
159
|
-
|
|
167
|
+
|
|
160
168
|
def fetch_page
|
|
161
169
|
get "/some-slug"
|
|
162
170
|
end
|
|
163
|
-
|
|
171
|
+
|
|
164
172
|
def test_should_not_insert_related_items_block
|
|
165
173
|
assert_rendered_in_template "div#related-items", ""
|
|
166
174
|
end
|
metadata
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
name: slimmer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease:
|
|
5
|
-
version: 1.1.
|
|
5
|
+
version: 1.1.44
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
8
8
|
- Ben Griffiths
|
|
@@ -11,7 +11,7 @@ autorequire:
|
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
13
|
|
|
14
|
-
date: 2012-07-
|
|
14
|
+
date: 2012-07-13 00:00:00 Z
|
|
15
15
|
dependencies:
|
|
16
16
|
- !ruby/object:Gem::Dependency
|
|
17
17
|
name: nokogiri
|
|
@@ -170,6 +170,7 @@ files:
|
|
|
170
170
|
- lib/slimmer/skin.rb
|
|
171
171
|
- lib/slimmer/section_inserter.rb
|
|
172
172
|
- lib/slimmer/title_inserter.rb
|
|
173
|
+
- lib/slimmer/search_path_setter.rb
|
|
173
174
|
- lib/slimmer/url_rewriter.rb
|
|
174
175
|
- lib/slimmer/body_class_copier.rb
|
|
175
176
|
- lib/slimmer/headers.rb
|
|
@@ -191,6 +192,7 @@ files:
|
|
|
191
192
|
- test/fixtures/related.raw.html.erb
|
|
192
193
|
- test/fixtures/404.html.erb
|
|
193
194
|
- test/fixtures/wrapper.html.erb
|
|
195
|
+
- test/search_path_setter_test.rb
|
|
194
196
|
- test/skin_test.rb
|
|
195
197
|
- test/google_analytics_test.rb
|
|
196
198
|
- bin/render_slimmer_error
|
|
@@ -207,7 +209,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
207
209
|
requirements:
|
|
208
210
|
- - ">="
|
|
209
211
|
- !ruby/object:Gem::Version
|
|
210
|
-
hash: -
|
|
212
|
+
hash: -3422349494229689063
|
|
211
213
|
segments:
|
|
212
214
|
- 0
|
|
213
215
|
version: "0"
|
|
@@ -216,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
216
218
|
requirements:
|
|
217
219
|
- - ">="
|
|
218
220
|
- !ruby/object:Gem::Version
|
|
219
|
-
hash: -
|
|
221
|
+
hash: -3422349494229689063
|
|
220
222
|
segments:
|
|
221
223
|
- 0
|
|
222
224
|
version: "0"
|
|
@@ -237,5 +239,6 @@ test_files:
|
|
|
237
239
|
- test/fixtures/related.raw.html.erb
|
|
238
240
|
- test/fixtures/404.html.erb
|
|
239
241
|
- test/fixtures/wrapper.html.erb
|
|
242
|
+
- test/search_path_setter_test.rb
|
|
240
243
|
- test/skin_test.rb
|
|
241
244
|
- test/google_analytics_test.rb
|