DEV: Use a route constraint to check the parameter format (#8)

DEV: Use a route constraint to check the parameter format (#8)

diff --git a/plugin.rb b/plugin.rb
index e8e682a..6ee838a 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -68,13 +68,8 @@ after_initialize do
     def default
       raise ActionController::RoutingError.new('Not Found') unless SiteSetting.sitemap_enabled
       prepend_view_path "plugins/discourse-sitemap/app/views/"
-      raw_page = params[:page]
 
-      if raw_page.to_i < 1 || raw_page.to_i.to_s != raw_page
-        raise ActionController::RoutingError.new('Not Found')
-      end
-
-      page = Integer(raw_page)
+      page = Integer(params[:page])
       sitemap(page)
     end
 
@@ -141,7 +136,7 @@ after_initialize do
     get "sitemap.xml" => "sitemap#index"
     get "news.xml" => "sitemap#news"
     get "sitemap_recent.xml" => "sitemap#recent"
-    get "sitemap_:page.xml" => "sitemap#default"
+    get "sitemap_:page.xml" => "sitemap#default", page: /[1-9][0-9]*/
   end
 
 end
diff --git a/spec/requests/sitemap_controller_spec.rb b/spec/requests/sitemap_controller_spec.rb
index b60f726..bed7ac0 100644
--- a/spec/requests/sitemap_controller_spec.rb
+++ b/spec/requests/sitemap_controller_spec.rb
@@ -9,5 +9,17 @@ RSpec.describe DiscourseSitemap::SitemapController do
 
       expect(response.status).to eq(404)
     end
+
+    it 'does not fail when the page starts with a zero' do
+      get '/sitemap_0.xml'
+
+      expect(response.status).to eq(404)
+    end
+
+    it 'works' do
+      get '/sitemap_1.xml'
+
+      expect(response.status).to eq(200)
+    end
   end
 end

GitHub sha: 9ef2e4fd

This commit appears in #8 which was approved by eviltrout. It was merged by romanrizzi.