How to test for subdomains in Merb

I’ve done a couple subdomain applications now in merb. One of the things that continually bothered me was that I was unable to figure out how to test for subdomains. Thanks to Shalon Wood in the Merb google group, I now am able to do so. I don’t think Rails can even do this.

requests/payment_spec.rb

require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
      describe "Authenticated user logged in", :given => "authenticated user" do
        describe "/payment", :given => 'current_site' do
                before(:each) do
                  @response = request("http://#{valid_site_attributes[:subdomain]}.example.org/payment")
                end
          it "should respond successfully" do
            @response.should be_successful
          end
        end
      end
      

require File.join(File.dirname(FILE), .., spec_helper.rb)
describe "Authenticated user logged in", :given => "authenticated user" do
describe "/payment", :given => current_site do
before(:each) do
response</span> = request(<span class="s"><span class="dl">&quot;</span><span class="k">http://</span><span class="il"><span class="idl">#{</span>valid_site_attributes[<span class="sy">:subdomain</span>]<span class="idl">}</span></span><span class="k">.example.org/payment</span><span class="dl">&quot;</span></span>) <span class="r">end</span> it <span class="s"><span class="dl">&quot;</span><span class="k">should respond successfully</span><span class="dl">&quot;</span></span> <span class="r">do</span> <span class="iv">response.should be_successful
end
end
end

spec_helper.rb

def valid_site_attributes(options = {})
        {
          :domain => 'http://www.example.org',
          :folder => 'exampleorg',
          :subdomain => 'example',
          :active => true,
          :id => 1
        }.merge(options)
      end
      given "current_site" do
        Site.all.destroy!
        @current_site = Site.create(valid_site_attributes)
      end
      

def valid_site_attributes(options = {})
{
:domain => http://www.example.org,
:folder => exampleorg,
:subdomain => example,
:active => true,
:id => 1
}.merge(options)
end
given "current_site" do
Site.all.destroy!
@current_site = Site.create(valid_site_attributes)
end

app/controllers/application.rb

def get_site
        # uses @current_site to create pages under appropriate site like @current_site.pages.new
        @current_site = Site.first(:subdomain => request.first_subdomain)
        raise NotFound unless @current_site
      end
      

def get_site
# uses current_site to create pages under appropriate site like @current_site.pages.new</span> <span class="iv">current_site = Site.first(:subdomain => request.first_subdomain)
raise NotFound unless @current_site
end

Picture of Scott Motte

delicious facebook rss twitter

Spitfire Sky | github | archives | resume