prototype: Allow WEBrick to simulate slow or dropped connections and to ease Ajax request inspection.
This commit is contained in:
parent
f0d54a48bd
commit
143b13d069
@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Allow WEBrick to simulate slow or dropped connections and to ease Ajax request inspection. [Tobie Langel]
|
||||
|
||||
* Ensure Ajax.Response#getHeader returns null for missing headers. [Tobie Langel]
|
||||
|
||||
* Ensure $A creates an empty array when its argument's length is undefined. Closes #10574. [henryju, Tobie Langel]
|
||||
|
@ -168,31 +168,103 @@ class ::WEBrick::HTTPServer
|
||||
# nop
|
||||
end
|
||||
end
|
||||
|
||||
class ::WEBrick::BasicLog
|
||||
def log(level, data)
|
||||
# nop
|
||||
end
|
||||
end
|
||||
|
||||
class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler
|
||||
def do_GET(req, res)
|
||||
super
|
||||
|
||||
res['Content-Type'] = case req.path
|
||||
when /\.js$/ then 'text/javascript'
|
||||
when /\.html$/ then 'text/html'
|
||||
when /\.css$/ then 'text/css'
|
||||
else 'text/plain'
|
||||
end
|
||||
|
||||
class WEBrick::HTTPResponse
|
||||
alias send send_response
|
||||
def send_response(socket)
|
||||
send(socket) unless fail_silently?
|
||||
end
|
||||
|
||||
def fail_silently?
|
||||
@fail_silently
|
||||
end
|
||||
|
||||
def fail_silently
|
||||
@fail_silently = true
|
||||
end
|
||||
end
|
||||
|
||||
class WEBrick::HTTPRequest
|
||||
def to_json
|
||||
headers = []
|
||||
each { |k, v| headers.push "#{k.inspect}: #{v.inspect}" }
|
||||
headers = "{" << headers.join(', ') << "}"
|
||||
%({ "headers": #{headers}, "body": #{body.inspect}, "method": #{request_method.inspect} })
|
||||
end
|
||||
end
|
||||
|
||||
class WEBrick::HTTPServlet::AbstractServlet
|
||||
def prevent_caching(res)
|
||||
res['ETag'] = nil
|
||||
res['Last-Modified'] = Time.now + 100**4
|
||||
res['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
|
||||
res['Pragma'] = 'no-cache'
|
||||
res['Expires'] = Time.now - 100**4
|
||||
end
|
||||
|
||||
def set_default_content_type(res, path)
|
||||
res['Content-Type'] = case path
|
||||
when /\.js$/ then 'text/javascript'
|
||||
when /\.html$/ then 'text/html'
|
||||
when /\.css$/ then 'text/css'
|
||||
else 'text/plain'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class BasicServlet < WEBrick::HTTPServlet::AbstractServlet
|
||||
def do_GET(req, res)
|
||||
set_default_content_type(res, req.path)
|
||||
prevent_caching(res)
|
||||
|
||||
req.query.each do |k, v|
|
||||
res[k] = v unless k == 'responseBody'
|
||||
end
|
||||
res.body = req.query["responseBody"]
|
||||
|
||||
raise WEBrick::HTTPStatus::OK
|
||||
end
|
||||
|
||||
def do_POST(req, res)
|
||||
do_GET(req, res)
|
||||
end
|
||||
end
|
||||
|
||||
class SlowServlet < BasicServlet
|
||||
def do_GET(req, res)
|
||||
sleep(2)
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
class DownServlet < BasicServlet
|
||||
def do_GET(req, res)
|
||||
res.fail_silently
|
||||
end
|
||||
end
|
||||
|
||||
class InspectionServlet < BasicServlet
|
||||
def do_GET(req, res)
|
||||
prevent_caching(res)
|
||||
res['Content-Type'] = "application/json"
|
||||
res.body = req.to_json
|
||||
raise WEBrick::HTTPStatus::OK
|
||||
end
|
||||
end
|
||||
|
||||
class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler
|
||||
def do_GET(req, res)
|
||||
super
|
||||
set_default_content_type(res, req.path)
|
||||
prevent_caching(res)
|
||||
end
|
||||
end
|
||||
|
||||
class JavaScriptTestTask < ::Rake::TaskLib
|
||||
|
||||
@ -213,13 +285,10 @@ class JavaScriptTestTask < ::Rake::TaskLib
|
||||
})
|
||||
res.body = "OK"
|
||||
end
|
||||
@server.mount_proc("/content-type") do |req, res|
|
||||
res.body = req["content-type"]
|
||||
end
|
||||
@server.mount_proc("/response") do |req, res|
|
||||
req.query.each {|k, v| res[k] = v unless k == 'responseBody'}
|
||||
res.body = req.query["responseBody"]
|
||||
end
|
||||
@server.mount("/response", BasicServlet)
|
||||
@server.mount("/slow", SlowServlet)
|
||||
@server.mount("/down", DownServlet)
|
||||
@server.mount("/inspect", InspectionServlet)
|
||||
yield self if block_given?
|
||||
define
|
||||
end
|
||||
|
@ -230,11 +230,11 @@
|
||||
|
||||
testContentTypeSetForSimulatedVerbs: function() {with(this) {
|
||||
if (isRunningFromRake) {
|
||||
new Ajax.Request('/content-type', extendDefault({
|
||||
new Ajax.Request('/inspect', extendDefault({
|
||||
method: 'put',
|
||||
contentType: 'application/bogus',
|
||||
onComplete: function(response) {
|
||||
assertEqual('application/bogus; charset=UTF-8', response.responseText);
|
||||
assertEqual('application/bogus; charset=UTF-8', response.responseJSON.headers['content-type']);
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user