2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-07-05 04:19:59 +08:00
|
|
|
require 'open-uri'
|
|
|
|
|
|
|
|
desc 'Creates the integration fixtures. Requires a development instance running.'
|
|
|
|
task 'integration:create_fixtures' => :environment do
|
|
|
|
|
|
|
|
fixtures = {
|
2015-01-07 07:56:03 +08:00
|
|
|
discovery: ["/latest.json", "/categories.json", "/c/bug/l/latest.json"],
|
2013-07-05 04:19:59 +08:00
|
|
|
topic: ["/t/280.json"],
|
2017-03-29 02:27:54 +08:00
|
|
|
user: ["/u/eviltrout.json",
|
2014-08-02 05:26:43 +08:00
|
|
|
"/user_actions.json",
|
2014-08-02 05:29:20 +08:00
|
|
|
"/topics/created-by/eviltrout.json"],
|
2014-04-30 03:29:01 +08:00
|
|
|
static: ["/faq", '/tos', '/privacy'],
|
|
|
|
unknown: ['/404-body']
|
2013-07-05 04:19:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fixtures.each do |type, urls|
|
|
|
|
|
|
|
|
filename = "#{Rails.root}/test/javascripts/fixtures/#{type}_fixtures.js"
|
|
|
|
|
2021-11-08 09:12:37 +08:00
|
|
|
content = "export default {\n"
|
2013-07-05 04:19:59 +08:00
|
|
|
urls.each do |url|
|
|
|
|
|
|
|
|
http_result = fake_xhr("http://localhost:3000#{url}")
|
|
|
|
|
|
|
|
# If the result is not JSON, convert it to JSON
|
|
|
|
begin
|
|
|
|
parsed = ::JSON.parse(http_result)
|
|
|
|
rescue
|
|
|
|
http_result = http_result.to_json
|
|
|
|
end
|
2014-08-02 05:26:43 +08:00
|
|
|
content << "\"#{url}\": #{http_result},\n"
|
2013-07-05 04:19:59 +08:00
|
|
|
|
|
|
|
end
|
2014-08-02 05:26:43 +08:00
|
|
|
content << "};\n"
|
2013-07-05 04:19:59 +08:00
|
|
|
|
|
|
|
File.write(filename, content)
|
|
|
|
end
|
2014-01-15 01:48:57 +08:00
|
|
|
|
2013-07-05 04:19:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def fake_xhr(url)
|
|
|
|
uri = URI(url)
|
|
|
|
|
|
|
|
result = nil
|
|
|
|
Net::HTTP.start(uri.host, uri.port) do |http|
|
|
|
|
request = Net::HTTP::Get.new uri
|
|
|
|
request.add_field "X-Requested-With", "XMLHttpRequest"
|
|
|
|
response = http.request(request)
|
|
|
|
result = response.body.force_encoding("UTF-8")
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
|
2014-01-15 01:48:57 +08:00
|
|
|
end
|