function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
the_wolfthe_wolf 

URL Rewriter - How to test it?!

Hi all,

 

I tried in my sandbox the new feature of summer '10, the url rewriter, illustrated in the following blog:

http://blog.sforce.com/sforce/2010/05/url-rewriting-for-your-customizing-your-site-urls.html

 

Now I want to copy it on production, but needs testing.

 

So, I create a class similar to that shown in the blog, and my initial test code is:

 

 static testMethod void testURLrewriter(){
  
  // I try to create the internal saleforce pageReference

      pageReference myPage = new pageReference('/apex/myPage');
      Test.setCurrentPage(myPage);
      myPage.setRedirect(true);
  
  List<PageReference> res = new List<PageReference>();
      res.add(myPage);
      res.add(new PageReference('/apex/myPage2' ));         
             
      // now call the methods

  ClassUrlRewriter urlClass = new ClassUrlRewriter();

  urlClass.mapRequestUrl(myPage);
  urlClass.generateUrlFor(res);
  }
 

Calling methods, the code coverage stops at the first if (where it controls the site.getUrl) How can I test a getUrl of a site, knowing that this class will be moved to the production ?

 

 

Thanks in advance!

 

Best Answer chosen by Admin (Salesforce Developers) 
the_wolfthe_wolf

Thanks Ryan for reply !

 

I used the method System.assert illustrated in your post, and I completed the test with 90% of code coverage!

 

My problem was in string of PageReference ( I deleted the string " /apex/ " and I used getUrl() correctly ).

 

Thanks again.

All Answers

RyanGuestRyanGuest

Can't you just test with different types of PageReferences (URLs) and make sure that they get transformed correctly? In my tests I have been doing something like the following:

 

 

ARewriter rewriter = new ARewriter();
System.debug(rewriter.mapRequestUrl(new PageReference('a')).getUrl());
System.debug(rewriter.mapRequestUrl(new PageReference('a')).getUrl() == 'a');

 

 

ARewriter rewriter = new ARewriter();
System.assert(rewriter.mapRequestUrl(new PageReference('before')).getUrl() == 'after');

 

 

 

and then I usually add a test case for when the URL shouldn't get rewritten as well:

 

 

System.assert(rewriter.mapRequestUrl(new PageReference('ignore')).getUrl() == 'ignore');

 

 

 

the_wolfthe_wolf

Thanks Ryan for reply !

 

I used the method System.assert illustrated in your post, and I completed the test with 90% of code coverage!

 

My problem was in string of PageReference ( I deleted the string " /apex/ " and I used getUrl() correctly ).

 

Thanks again.

This was selected as the best answer