# Testing Routing Constraint =\> redirect example.com to www.example.com

**URL:** <https://www.ruby-forum.com/t/testing-routing-constraint-redirect-example-com-to-www-example-com/210188>\
**Category:** RSpec\
**Created:** [August 2, 2011, 4:52am UTC](https://www.ruby-forum.com/t/testing-routing-constraint-redirect-example-com-to-www-example-com/210188 "2011-08-02T04:52:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 2, 2011, 4:52am UTC](https://www.ruby-forum.com/t/testing-routing-constraint-redirect-example-com-to-www-example-com/210188/1 "2011-08-02T04:52:09Z")

</div>

Hi All

With the following route

constraints(:host =\> “[example.com](http://example.com)”) do  
match “(\*x)” =\> redirect { |params, request|  
URI.parse(request.url).tap { |x| x.host = “[www.example.com](http://www.example.com)” }.to\_s  
}  
end

I am wondering how or where I would spec this?

Ideally I would like to be able to write a routing spec, something like:

get(“[http://example.com](http://example.com)”).should route\_to(:controller =\> “home”, :action  
=\>  
“index”, :host =\> “[www.example.com](http://www.example.com)”)

Thanks in advance

Shane M.

---

<div class="post-metadata">

**Author:** ![codejoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@codejoy](https://www.ruby-forum.com/u/codejoy)\
**Post date:** [August 2, 2011, 12:04pm UTC](https://www.ruby-forum.com/t/testing-routing-constraint-redirect-example-com-to-www-example-com/210188/2 "2011-08-02T12:04:31Z")

</div>

On Mon, Aug 1, 2011 at 8:29 PM, Shane M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> * * *
> 
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

Hi, take a look at this:

> <https://github.com/rspec/rspec-rails/issues/416>
>
> Hi, I have this line of code in routes.rb
> 
> \`\`\` ruby
> get '/video' =\> redirect {'h…ttp://youtube.com/myvideo-id'}
> \`\`\`
> 
> How do I test this?
> 
> \`\`\` ruby
> it 'should redirect /video to http://youtube.com/myvideo-id' do 
> {:get =\> '/video'}.should route\_to 'http://youtube.com/myvideo-id' 
> end
> \`\`\`
> 
> Looks like route\_to matcher is only cheking for a hash char:
> 
> \`\`\` ruby
> if path.is\_a?(String)
> controller, action = path.split("#")
> options ||= {}
> options.merge!(:controller =\> controller, :action =\> action)
> else
> options = path
> end
> \`\`\`

---

<div class="post-metadata">

**Author:** ![codejoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@codejoy](https://www.ruby-forum.com/u/codejoy)\
**Post date:** [August 3, 2011, 4:53am UTC](https://www.ruby-forum.com/t/testing-routing-constraint-redirect-example-com-to-www-example-com/210188/3 "2011-08-03T04:53:00Z")

</div>

Thanks for that Justin!

I was not aware of request specs  
[http://relishapp.com/rspec/rspec-rails/v/2-6/dir/request-specs/request-spec](http://relishapp.com/rspec/rspec-rails/v/2-6/dir/request-specs/request-spec)…  
could not find any reference to them in the rspec book (maybe I missed  
them).

so adding spec/requests/routes\_spec.rb with the following did the trick!

require “spec\_helper”

describe “routes that redirect” do

it “redirects [http://example.com](http://example.com) to [http://www.example.com](http://www.example.com)” do  
get “[http://example.com](http://example.com)”  
response.should redirect\_to(‘[http://www.example.com](http://www.example.com)’)  
end

it “should not redirect [https://secure.example.com](https://secure.example.com)” do  
get “[https://secure.example.com](https://secure.example.com)”  
response.should\_not be\_redirect  
end

end

constraints(:host =\> /^[example.com/](http://example.com/)) do  
match “(\*x)” =\> redirect { |params, request|  
URI.parse(request.url).tap { |x| x.host = “[www.example.com](http://www.example.com)” }.to\_s  
}  
end

---

<div class="post-metadata">

**Author:** ![codejoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/74df32/32.png) [@codejoy](https://www.ruby-forum.com/u/codejoy)\
**Post date:** [August 3, 2011, 7:13am UTC](https://www.ruby-forum.com/t/testing-routing-constraint-redirect-example-com-to-www-example-com/210188/4 "2011-08-03T07:13:43Z")

</div>

On Tue, Aug 2, 2011 at 8:19 PM, Shane M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> > > [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)
> 
> * * *
> 
> rspec-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

Awesome, glad it worked out!
