prefix_generation_test.rb 9.6 KB
Newer Older
1
require 'abstract_unit'
P
Piotr Sarnacki 已提交
2
require 'rack/test'
3 4

module TestGenerationPrefix
P
Piotr Sarnacki 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  class Post
    extend ActiveModel::Naming

    def to_param
      "1"
    end

    def self.model_name
      klass = "Post"
      def klass.name; self end

      ActiveModel::Name.new(klass)
    end
  end

20
  class WithMountedEngine < ActionDispatch::IntegrationTest
21 22
    include Rack::Test::Methods

23 24 25 26 27
    class BlogEngine
      def self.routes
        @routes ||= begin
          routes = ActionDispatch::Routing::RouteSet.new
          routes.draw do
P
Piotr Sarnacki 已提交
28 29
            match "/posts/:id", :to => "inside_engine_generating#show", :as => :post
            match "/posts", :to => "inside_engine_generating#index", :as => :posts
30
            match "/url_to_application", :to => "inside_engine_generating#url_to_application"
P
Piotr Sarnacki 已提交
31
            match "/polymorphic_path_for_engine", :to => "inside_engine_generating#polymorphic_path_for_engine"
32
            match "/conflicting_url", :to => "inside_engine_generating#conflicting"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
          end

          routes
        end
      end

      def self.call(env)
        env['action_dispatch.routes'] = routes
        routes.call(env)
      end
    end

    class RailsApplication
      def self.routes
        @routes ||= begin
          routes = ActionDispatch::Routing::RouteSet.new
          routes.draw do
            scope "/:omg", :omg => "awesome" do
P
Piotr Sarnacki 已提交
51
              mount BlogEngine => "/blog", :as => "blog_engine"
52 53
            end
            match "/generate", :to => "outside_engine_generating#index"
P
Piotr Sarnacki 已提交
54 55
            match "/polymorphic_path_for_engine", :to => "outside_engine_generating#polymorphic_path_for_engine"
            match "/polymorphic_with_url_for", :to => "outside_engine_generating#polymorphic_with_url_for"
56
            match "/conflicting_url", :to => "outside_engine_generating#conflicting"
57
            root :to => "outside_engine_generating#index"
58 59 60 61 62 63 64 65 66 67 68 69
          end

          routes
        end
      end

      def self.call(env)
        env['action_dispatch.routes'] = routes
        routes.call(env)
      end
    end

P
Piotr Sarnacki 已提交
70 71 72
    # force draw
    RailsApplication.routes

73
    class ::InsideEngineGeneratingController < ActionController::Base
P
Piotr Sarnacki 已提交
74
      include BlogEngine.routes.url_helpers
75
      include RailsApplication.routes.mounted_helpers
P
Piotr Sarnacki 已提交
76

77
      def index
P
Piotr Sarnacki 已提交
78 79 80 81 82
        render :text => posts_path
      end

      def show
        render :text => post_path(:id => params[:id])
83
      end
84

85
      def url_to_application
86 87 88
        path = main_app.url_for(:controller => "outside_engine_generating",
                                :action => "index",
                                :only_path => true)
89 90
        render :text => path
      end
P
Piotr Sarnacki 已提交
91 92 93 94

      def polymorphic_path_for_engine
        render :text => polymorphic_path(Post.new)
      end
95 96 97 98

      def conflicting
        render :text => "engine"
      end
99 100 101
    end

    class ::OutsideEngineGeneratingController < ActionController::Base
P
Piotr Sarnacki 已提交
102 103
      include BlogEngine.routes.mounted_helpers

104
      def index
P
Piotr Sarnacki 已提交
105 106 107 108 109 110 111 112 113
        render :text => blog_engine.post_path(:id => 1)
      end

      def polymorphic_path_for_engine
        render :text => blog_engine.polymorphic_path(Post.new)
      end

      def polymorphic_with_url_for
        render :text => blog_engine.url_for(Post.new)
114
      end
115 116 117 118

      def conflicting
        render :text => "application"
      end
119 120
    end

P
Piotr Sarnacki 已提交
121
    class EngineObject
122 123 124 125
      include ActionDispatch::Routing::UrlFor
      include BlogEngine.routes.url_helpers
    end

P
Piotr Sarnacki 已提交
126
    class AppObject
127 128 129
      include ActionDispatch::Routing::UrlFor
      include RailsApplication.routes.url_helpers
    end
130

131 132 133 134
    def app
      RailsApplication
    end

P
Piotr Sarnacki 已提交
135 136 137 138 139 140 141 142
    def engine_object
      @engine_object ||= EngineObject.new
    end

    def app_object
      @app_object ||= AppObject.new
    end

143 144 145 146
    def setup
      RailsApplication.routes.default_url_options = {}
    end

P
Piotr Sarnacki 已提交
147 148 149 150
    # Inside Engine
    test "[ENGINE] generating engine's url use SCRIPT_NAME from request" do
      get "/pure-awesomeness/blog/posts/1"
      assert_equal "/pure-awesomeness/blog/posts/1", last_response.body
151 152
    end

P
Piotr Sarnacki 已提交
153 154 155
    test "[ENGINE] generating application's url never uses SCRIPT_NAME from request" do
      get "/pure-awesomeness/blog/url_to_application"
      assert_equal "/generate", last_response.body
156 157
    end

P
Piotr Sarnacki 已提交
158 159 160 161 162 163 164 165 166 167 168
    test "[ENGINE] generating application's url includes default_url_options[:script_name]" do
      RailsApplication.routes.default_url_options = {:script_name => "/something"}
      get "/pure-awesomeness/blog/url_to_application"
      assert_equal "/something/generate", last_response.body
    end

    test "[ENGINE] generating application's url should give higher priority to default_url_options[:script_name]" do
      RailsApplication.routes.default_url_options = {:script_name => "/something"}
      get "/pure-awesomeness/blog/url_to_application", {}, 'SCRIPT_NAME' => '/foo'
      assert_equal "/something/generate", last_response.body
    end
P
Piotr Sarnacki 已提交
169 170 171 172 173 174

    test "[ENGINE] generating engine's url with polymorphic path" do
      get "/pure-awesomeness/blog/polymorphic_path_for_engine"
      assert_equal "/pure-awesomeness/blog/posts/1", last_response.body
    end

175 176 177 178 179
    test "[ENGINE] url_helpers from engine have higher priotity than application's url_helpers" do
      get "/awesome/blog/conflicting_url"
      assert_equal "engine", last_response.body
    end

P
Piotr Sarnacki 已提交
180 181
    # Inside Application
    test "[APP] generating engine's route includes prefix" do
182 183 184 185
      get "/generate"
      assert_equal "/awesome/blog/posts/1", last_response.body
    end

P
Piotr Sarnacki 已提交
186
    test "[APP] generating engine's route includes default_url_options[:script_name]" do
187 188 189 190 191
      RailsApplication.routes.default_url_options = {:script_name => "/something"}
      get "/generate"
      assert_equal "/something/awesome/blog/posts/1", last_response.body
    end

P
Piotr Sarnacki 已提交
192
    test "[APP] generating engine's route should give higher priority to default_url_options[:script_name]" do
193
      RailsApplication.routes.default_url_options = {:script_name => "/something"}
194
      get "/generate", {}, 'SCRIPT_NAME' => "/foo"
195
      assert_equal "/something/awesome/blog/posts/1", last_response.body
196 197
    end

P
Piotr Sarnacki 已提交
198 199 200 201 202 203 204 205 206 207
    test "[APP] generating engine's url with polymorphic path" do
      get "/polymorphic_path_for_engine"
      assert_equal "/awesome/blog/posts/1", last_response.body
    end

    test "[APP] generating engine's url with url_for(@post)" do
      get "/polymorphic_with_url_for"
      assert_equal "http://example.org/awesome/blog/posts/1", last_response.body
    end

P
Piotr Sarnacki 已提交
208 209 210
    # Inside any Object
    test "[OBJECT] generating engine's route includes prefix" do
      assert_equal "/awesome/blog/posts/1", engine_object.post_path(:id => 1)
211 212
    end

P
Piotr Sarnacki 已提交
213 214
    test "[OBJECT] generating engine's route includes dynamic prefix" do
      assert_equal "/pure-awesomeness/blog/posts/3", engine_object.post_path(:id => 3, :omg => "pure-awesomeness")
215 216
    end

P
Piotr Sarnacki 已提交
217 218 219
    test "[OBJECT] generating engine's route includes default_url_options[:script_name]" do
      RailsApplication.routes.default_url_options = {:script_name => "/something"}
      assert_equal "/something/pure-awesomeness/blog/posts/3", engine_object.post_path(:id => 3, :omg => "pure-awesomeness")
220
    end
221

P
Piotr Sarnacki 已提交
222 223
    test "[OBJECT] generating application's route" do
      assert_equal "/", app_object.root_path
224 225
    end

P
Piotr Sarnacki 已提交
226
    test "[OBJECT] generating application's route includes default_url_options[:script_name]" do
227
      RailsApplication.routes.default_url_options = {:script_name => "/something"}
P
Piotr Sarnacki 已提交
228
      assert_equal "/something/", app_object.root_path
229 230
    end

P
Piotr Sarnacki 已提交
231
    test "[OBJECT] generating engine's route with url_for" do
P
Piotr Sarnacki 已提交
232
      path = engine_object.url_for(:controller => "inside_engine_generating",
P
Piotr Sarnacki 已提交
233 234 235 236 237
                                   :action => "show",
                                   :only_path => true,
                                   :omg => "omg",
                                   :id => 1)
      assert_equal "/omg/blog/posts/1", path
P
Piotr Sarnacki 已提交
238
    end
239

P
Piotr Sarnacki 已提交
240 241
    test "[OBJECT] generating engine's route with named helpers" do
      path = engine_object.posts_path
P
Piotr Sarnacki 已提交
242 243
      assert_equal "/awesome/blog/posts", path

P
Piotr Sarnacki 已提交
244
      path = engine_object.posts_url(:host => "example.com")
P
Piotr Sarnacki 已提交
245
      assert_equal "http://example.com/awesome/blog/posts", path
246
    end
P
Piotr Sarnacki 已提交
247 248 249 250 251 252 253 254

    test "[OBJECT] generating engine's route with polymorphic_url" do
      path = engine_object.polymorphic_path(Post.new)
      assert_equal "/awesome/blog/posts/1", path

      path = engine_object.polymorphic_url(Post.new, :host => "www.example.com")
      assert_equal "http://www.example.com/awesome/blog/posts/1", path
    end
255
  end
P
Piotr Sarnacki 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316

  class EngineMountedAtRoot < ActionDispatch::IntegrationTest
    include Rack::Test::Methods

    class BlogEngine
      def self.routes
        @routes ||= begin
          routes = ActionDispatch::Routing::RouteSet.new
          routes.draw do
            match "/posts/:id", :to => "posts#show", :as => :post
          end

          routes
        end
      end

      def self.call(env)
        env['action_dispatch.routes'] = routes
        routes.call(env)
      end
    end

    class RailsApplication
      def self.routes
        @routes ||= begin
          routes = ActionDispatch::Routing::RouteSet.new
          routes.draw do
            mount BlogEngine => "/"
          end

          routes
        end
      end

      def self.call(env)
        env['action_dispatch.routes'] = routes
        routes.call(env)
      end
    end

    # force draw
    RailsApplication.routes

    class ::PostsController < ActionController::Base
      include BlogEngine.routes.url_helpers
      include RailsApplication.routes.mounted_helpers

      def show
        render :text => post_path(:id => params[:id])
      end
    end

    def app
      RailsApplication
    end

    test "generating path inside engine" do
      get "/posts/1"
      assert_equal "/posts/1", last_response.body
    end
  end
317
end