namespace :static_pages do
  desc "Generates static pages"
  task generate: 'assets:precompile' do
    pages = {
      'static/404' => '404.html',
      'static/500' => '500.html',
      'static/503' => '503.html',
      '/cookies' => 'cookies.html',
      '/cookies-disabled' => 'cookies-disabled.html',
      '/terms-and-conditions' => 'terms-and-conditions.html',
      '/unsubscribe' => 'unsubscribe.html'
    }

    # Silence a warning for the session key not being set.
    Rails.application.config.secret_key_base = SecureRandom.hex
    app = ActionDispatch::Integration::Session.new(Rails.application)

    pages.each do |route, output|
      puts "Generating #{output}..."
      outpath = File.join ([Rails.root, 'public', output])
      resp = app.get(route)
      if resp == 200
        File.delete(outpath) unless not File.exists?(outpath)
        File.open(outpath, 'w') do |f|
          f.write(app.response.body)
        end
      else
        puts "Error generating #{output}!"
      end
    end
  end
end

namespace :static_pages do
  desc "Generates static pages"
  task generate: 'assets:precompile' do
    pages = {
      '/static/404' => '404.html',
      '/static/500' => '500.html',
      '/static/503' => '503.html',
      '/cookies' => 'cookies.html',
      '/cookies-disabled' => 'cookies-disabled.html',
      '/terms-and-conditions' => 'terms-and-conditions.html',
      '/unsubscribe' => 'unsubscribe.html'
    }

    # Silence a warning for the session key not being set.
    Rails.application.config.secret_key_base = SecureRandom.hex
    app = ActionDispatch::Integration::Session.new(Rails.application)

    pages.each do |route, output|
      puts "Generating #{output}..."
      outpath = File.join ([Rails.root, 'public', output])
      resp = app.get(route)
      if resp == 200
        File.delete(outpath) unless not File.exists?(outpath)
        File.open(outpath, 'w') do |f|
          f.write(app.response.body)
        end
      else
        puts "Error generating #{output}!"
      end
    end
  end
end

Allow Rails test sessions to generate valid URIs
Allow Rails test sessions to generate valid URIs

It appears that `ActionDispatch::Testing::Integration#build_full_uri`
(in ActionPack 4.2.1 and possibly earlier) requires requested paths to
include leading slashes (which they probably should have done anyway).
Without valid paths, it builds invalid URIs like `www.example.com:80static`,
which cause other parts of Rails to fail when we attempt to generate our static pages.

Not having leading slashes caused generation of static pages to fail
(as described [on the pull
request](https://github.com/ministryofjustice/prison-visits/pull/216#issuecomment-107908617)).
